--- /dev/null
+///////////////////////////////////////////////////////////////////////////////\r
+// Name: os2/snglinst.cpp\r
+// Purpose: implements wxSingleInstanceChecker class for OS/2 using\r
+// named mutexes\r
+// Author: Vadim Zeitlin\r
+// Modified by: Lauri Nurmi (modified for OS/2)\r
+// Created: 08.02.2009\r
+// RCS-ID: $Id$\r
+// Copyright: (c) 2001 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>\r
+// License: wxWindows licence\r
+///////////////////////////////////////////////////////////////////////////////\r
+\r
+// ============================================================================\r
+// declarations\r
+// ============================================================================\r
+\r
+// ----------------------------------------------------------------------------\r
+// headers\r
+// ----------------------------------------------------------------------------\r
+\r
+// For compilers that support precompilation, includes "wx.h".\r
+#include "wx/wxprec.h"\r
+\r
+#ifdef __BORLANDC__\r
+ #pragma hdrstop\r
+#endif\r
+\r
+#if wxUSE_SNGLINST_CHECKER\r
+\r
+#ifndef WX_PRECOMP\r
+ #include "wx/string.h"\r
+ #include "wx/log.h"\r
+#endif //WX_PRECOMP\r
+\r
+#include "wx/snglinst.h"\r
+\r
+#define INCL_DOSSEMAPHORES\r
+#define INCL_DOSERRORS\r
+#include <os2.h>\r
+\r
+#include "wx/os2/private.h"\r
+\r
+// ----------------------------------------------------------------------------\r
+// wxSingleInstanceCheckerImpl: the real implementation class\r
+// ----------------------------------------------------------------------------\r
+\r
+class WXDLLEXPORT wxSingleInstanceCheckerImpl\r
+{\r
+public:\r
+ wxSingleInstanceCheckerImpl()\r
+ {\r
+ m_hMutex = NULL;\r
+ m_anotherRunning = false;\r
+ }\r
+\r
+ bool Create(const wxString& name)\r
+ {\r
+ wxString semName;\r
+ semName.Printf(wxT("\\SEM32\\%s"), name.c_str());\r
+ int rc = DosCreateMutexSem(semName.c_str(), &m_hMutex, DC_SEM_SHARED, 1);\r
+\r
+ if ( rc == NO_ERROR ) {\r
+ m_anotherRunning = false;\r
+ return true;\r
+ } else if ( rc == ERROR_DUPLICATE_NAME ) {\r
+ m_anotherRunning = true;\r
+ return true;\r
+ } else {\r
+ m_anotherRunning = false; // we don't know for sure in this case\r
+ wxLogLastError(_T("DosCreateMutexSem"));\r
+ return false;\r
+ }\r
+ }\r
+\r
+ bool IsAnotherRunning() const\r
+ {\r
+ return m_anotherRunning;\r
+ }\r
+\r
+ ~wxSingleInstanceCheckerImpl()\r
+ {\r
+ if ( m_hMutex )\r
+ {\r
+ if ( !::DosCloseMutexSem(m_hMutex) )\r
+ {\r
+ wxLogLastError(_T("DosCloseMutexSem"));\r
+ }\r
+ }\r
+ }\r
+\r
+private:\r
+ // if true, creating the mutex either succeeded\r
+ // or we know it failed because another app is running.\r
+ bool m_anotherRunning;\r
+\r
+ // the mutex handle, may be NULL\r
+ HMTX m_hMutex;\r
+\r
+ DECLARE_NO_COPY_CLASS(wxSingleInstanceCheckerImpl)\r
+};\r
+\r
+// ============================================================================\r
+// wxSingleInstanceChecker implementation\r
+// ============================================================================\r
+\r
+bool wxSingleInstanceChecker::Create(const wxString& name,\r
+ const wxString& WXUNUSED(path))\r
+{\r
+ wxASSERT_MSG( !m_impl,\r
+ _T("calling wxSingleInstanceChecker::Create() twice?") );\r
+\r
+ // creating unnamed mutex doesn't have the same semantics!\r
+ wxASSERT_MSG( !name.empty(), _T("mutex name can't be empty") );\r
+\r
+ m_impl = new wxSingleInstanceCheckerImpl;\r
+\r
+ return m_impl->Create(name);\r
+}\r
+\r
+bool wxSingleInstanceChecker::IsAnotherRunning() const\r
+{\r
+ wxCHECK_MSG( m_impl, false, _T("must call Create() first") );\r
+\r
+ return m_impl->IsAnotherRunning();\r
+}\r
+\r
+wxSingleInstanceChecker::~wxSingleInstanceChecker()\r
+{\r
+ delete m_impl;\r
+}\r
+\r
+#endif // wxUSE_SNGLINST_CHECKER\r