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