]> git.saurik.com Git - wxWidgets.git/commitdiff
Added wxSingleInstanceChecker for OS/2 (patch #10491).
authorStefan Neis <Stefan.Neis@t-online.de>
Sat, 21 Feb 2009 13:52:20 +0000 (13:52 +0000)
committerStefan Neis <Stefan.Neis@t-online.de>
Sat, 21 Feb 2009 13:52:20 +0000 (13:52 +0000)
git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@59068 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775

build/bakefiles/files.bkl
src/os2/snglinst.cpp [new file with mode: 0644]

index ef2cc9efb17cdeae0ffff5ba69672a72a94f7e51..028f35933f8b153dc0ed5510d223bc66f38f8440 100644 (file)
@@ -235,6 +235,7 @@ IMPORTANT: please read docs/tech/tn0016.txt before modifying this file!
     src/unix/timerunx.cpp
     src/os2/dir.cpp
     src/os2/mimetype.cpp
+    src/os2/snglinst.cpp
     src/os2/stdpaths.cpp
     src/os2/thread.cpp
     src/os2/utils.cpp
diff --git a/src/os2/snglinst.cpp b/src/os2/snglinst.cpp
new file mode 100644 (file)
index 0000000..eb95aa3
--- /dev/null
@@ -0,0 +1,132 @@
+///////////////////////////////////////////////////////////////////////////////\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