| 1 | ///////////////////////////////////////////////////////////////////////////////\r |
| 2 | // Name: os2/snglinst.cpp\r |
| 3 | // Purpose: implements wxSingleInstanceChecker class for OS/2 using\r |
| 4 | // named mutexes\r |
| 5 | // Author: Vadim Zeitlin\r |
| 6 | // Modified by: Lauri Nurmi (modified for OS/2)\r |
| 7 | // Created: 08.02.2009\r |
| 8 | // RCS-ID: $Id$\r |
| 9 | // Copyright: (c) 2001 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>\r |
| 10 | // License: wxWindows licence\r |
| 11 | ///////////////////////////////////////////////////////////////////////////////\r |
| 12 | \r |
| 13 | // ============================================================================\r |
| 14 | // declarations\r |
| 15 | // ============================================================================\r |
| 16 | \r |
| 17 | // ----------------------------------------------------------------------------\r |
| 18 | // headers\r |
| 19 | // ----------------------------------------------------------------------------\r |
| 20 | \r |
| 21 | // For compilers that support precompilation, includes "wx.h".\r |
| 22 | #include "wx/wxprec.h"\r |
| 23 | \r |
| 24 | #ifdef __BORLANDC__\r |
| 25 | #pragma hdrstop\r |
| 26 | #endif\r |
| 27 | \r |
| 28 | #if wxUSE_SNGLINST_CHECKER\r |
| 29 | \r |
| 30 | #ifndef WX_PRECOMP\r |
| 31 | #include "wx/string.h"\r |
| 32 | #include "wx/log.h"\r |
| 33 | #endif //WX_PRECOMP\r |
| 34 | \r |
| 35 | #include "wx/snglinst.h"\r |
| 36 | \r |
| 37 | #define INCL_DOSSEMAPHORES\r |
| 38 | #define INCL_DOSERRORS\r |
| 39 | #include <os2.h>\r |
| 40 | \r |
| 41 | #include "wx/os2/private.h"\r |
| 42 | \r |
| 43 | // ----------------------------------------------------------------------------\r |
| 44 | // wxSingleInstanceCheckerImpl: the real implementation class\r |
| 45 | // ----------------------------------------------------------------------------\r |
| 46 | \r |
| 47 | class WXDLLEXPORT wxSingleInstanceCheckerImpl\r |
| 48 | {\r |
| 49 | public:\r |
| 50 | wxSingleInstanceCheckerImpl()\r |
| 51 | {\r |
| 52 | m_hMutex = NULL;\r |
| 53 | m_anotherRunning = false;\r |
| 54 | }\r |
| 55 | \r |
| 56 | bool Create(const wxString& name)\r |
| 57 | {\r |
| 58 | wxString semName;\r |
| 59 | semName.Printf(wxT("\\SEM32\\%s"), name.c_str());\r |
| 60 | int rc = DosCreateMutexSem(semName.c_str(), &m_hMutex, DC_SEM_SHARED, 1);\r |
| 61 | \r |
| 62 | if ( rc == NO_ERROR ) {\r |
| 63 | m_anotherRunning = false;\r |
| 64 | return true;\r |
| 65 | } else if ( rc == ERROR_DUPLICATE_NAME ) {\r |
| 66 | m_anotherRunning = true;\r |
| 67 | return true;\r |
| 68 | } else {\r |
| 69 | m_anotherRunning = false; // we don't know for sure in this case\r |
| 70 | wxLogLastError(wxT("DosCreateMutexSem"));\r |
| 71 | return false;\r |
| 72 | }\r |
| 73 | }\r |
| 74 | \r |
| 75 | bool IsAnotherRunning() const\r |
| 76 | {\r |
| 77 | return m_anotherRunning;\r |
| 78 | }\r |
| 79 | \r |
| 80 | ~wxSingleInstanceCheckerImpl()\r |
| 81 | {\r |
| 82 | if ( m_hMutex )\r |
| 83 | {\r |
| 84 | if ( !::DosCloseMutexSem(m_hMutex) )\r |
| 85 | {\r |
| 86 | wxLogLastError(wxT("DosCloseMutexSem"));\r |
| 87 | }\r |
| 88 | }\r |
| 89 | }\r |
| 90 | \r |
| 91 | private:\r |
| 92 | // if true, creating the mutex either succeeded\r |
| 93 | // or we know it failed because another app is running.\r |
| 94 | bool m_anotherRunning;\r |
| 95 | \r |
| 96 | // the mutex handle, may be NULL\r |
| 97 | HMTX m_hMutex;\r |
| 98 | \r |
| 99 | DECLARE_NO_COPY_CLASS(wxSingleInstanceCheckerImpl)\r |
| 100 | };\r |
| 101 | \r |
| 102 | // ============================================================================\r |
| 103 | // wxSingleInstanceChecker implementation\r |
| 104 | // ============================================================================\r |
| 105 | \r |
| 106 | bool wxSingleInstanceChecker::Create(const wxString& name,\r |
| 107 | const wxString& WXUNUSED(path))\r |
| 108 | {\r |
| 109 | wxASSERT_MSG( !m_impl,\r |
| 110 | wxT("calling wxSingleInstanceChecker::Create() twice?") );\r |
| 111 | \r |
| 112 | // creating unnamed mutex doesn't have the same semantics!\r |
| 113 | wxASSERT_MSG( !name.empty(), wxT("mutex name can't be empty") );\r |
| 114 | \r |
| 115 | m_impl = new wxSingleInstanceCheckerImpl;\r |
| 116 | \r |
| 117 | return m_impl->Create(name);\r |
| 118 | }\r |
| 119 | \r |
| 120 | bool wxSingleInstanceChecker::DoIsAnotherRunning() const |
| 121 | {\r |
| 122 | wxCHECK_MSG( m_impl, false, wxT("must call Create() first") );\r |
| 123 | \r |
| 124 | return m_impl->IsAnotherRunning();\r |
| 125 | }\r |
| 126 | \r |
| 127 | wxSingleInstanceChecker::~wxSingleInstanceChecker()\r |
| 128 | {\r |
| 129 | delete m_impl;\r |
| 130 | }\r |
| 131 | \r |
| 132 | #endif // wxUSE_SNGLINST_CHECKER\r |