1+
2+ #include " GlobalMutex.h"
3+
4+ #ifdef WIN32
5+ #include < Windows.h>
6+ #elif POSIX
7+ #include < fcntl.h>
8+ #endif
9+
10+ namespace HttpServer
11+ {
12+ GlobalMutex::GlobalMutex () : mtx_desc(nullptr )
13+ {
14+
15+ }
16+
17+ GlobalMutex::~GlobalMutex ()
18+ {
19+ this ->close ();
20+ }
21+
22+ bool GlobalMutex::create (const std::string &mutexName)
23+ {
24+ this ->close ();
25+
26+ #ifdef WIN32
27+ this ->mtx_desc = ::CreateMutex (nullptr , false , mutexName.c_str () );
28+
29+ if (nullptr == this ->mtx_desc )
30+ {
31+ return false ;
32+ }
33+ #elif POSIX
34+ ::sem_t *sem = ::sem_open (mutexName.c_str (), O_CREAT, 0666 , 1 );
35+
36+ if (SEM_FAILED == sem)
37+ {
38+ return false ;
39+ }
40+
41+ this ->mtx_desc = sem;
42+ #else
43+ #error "Undefine platform"
44+ #endif
45+
46+ this ->mtx_name = mutexName;
47+
48+ return true ;
49+ }
50+
51+ bool GlobalMutex::destory (const std::string &mutexName)
52+ {
53+ #ifdef WIN32
54+ return true ;
55+ #elif POSIX
56+ return 0 == ::sem_unlink (mutexName.c_str () );
57+ #else
58+ #error "Undefine platform"
59+ #endif
60+ }
61+
62+ bool GlobalMutex::destory ()
63+ {
64+ #ifdef WIN32
65+ const bool ret = ::CloseHandle (this ->mtx_desc );
66+
67+ this ->close ();
68+
69+ return ret;
70+ #elif POSIX
71+ const int ret = ::sem_unlink (this ->mtx_name .c_str () );
72+
73+ this ->close ();
74+
75+ return 0 == ret;
76+ #else
77+ #error "Undefine platform"
78+ #endif
79+ }
80+
81+ bool GlobalMutex::open (const std::string &mutexName)
82+ {
83+ this ->close ();
84+
85+ #ifdef WIN32
86+ this ->mtx_desc = ::OpenMutex (SYNCHRONIZE, false , mutexName.c_str () );
87+
88+ if (nullptr == this ->mtx_desc )
89+ {
90+ return false ;
91+ }
92+ #elif POSIX
93+ ::sem_t *sem = ::sem_open (mutexName.c_str (), 0 );
94+
95+ if (SEM_FAILED == sem)
96+ {
97+ return false ;
98+ }
99+
100+ this ->mtx_desc = sem;
101+ #else
102+ #error "Undefine platform"
103+ #endif
104+
105+ this ->mtx_name = mutexName;
106+
107+ return true ;
108+ }
109+
110+ bool GlobalMutex::close ()
111+ {
112+ if (this ->mtx_desc )
113+ {
114+ #ifdef WIN32
115+ ::CloseHandle (this ->mtx_desc);
116+ this ->mtx_desc = nullptr ;
117+ #elif POSIX
118+ ::sem_close (this ->mtx_desc);
119+ this ->mtx_desc = nullptr ;
120+ #else
121+ #error "Undefine platform"
122+ #endif
123+
124+ this ->mtx_name .clear ();
125+
126+ return true ;
127+ }
128+
129+ return false ;
130+ }
131+
132+ bool GlobalMutex::is_open () const
133+ {
134+ #ifdef WIN32
135+ return nullptr != this ->mtx_desc ;
136+ #elif POSIX
137+ return nullptr != this ->mtx_desc ;
138+ #else
139+ #error "Undefine platform"
140+ #endif
141+ }
142+
143+ bool GlobalMutex::lock () const
144+ {
145+ #ifdef WIN32
146+ return WAIT_OBJECT_0 == ::WaitForSingleObject (this ->mtx_desc , INFINITE);
147+ #elif POSIX
148+ return 0 == ::sem_wait (this ->mtx_desc );
149+ #else
150+ #error "Undefine platform"
151+ #endif
152+ }
153+
154+ bool GlobalMutex::try_lock () const
155+ {
156+ #ifdef WIN32
157+ return WAIT_OBJECT_0 == ::WaitForSingleObject (this ->mtx_desc , 0 );
158+ #elif POSIX
159+ return 0 == ::sem_trywait (this ->mtx_desc );
160+ #else
161+ #error "Undefine platform"
162+ #endif
163+ }
164+
165+ bool GlobalMutex::unlock () const
166+ {
167+ #ifdef WIN32
168+ return ::ReleaseMutex (this ->mtx_desc );
169+ #elif POSIX
170+ return 0 == ::sem_post (this ->mtx_desc );
171+ #else
172+ #error "Undefine platform"
173+ #endif
174+ }
175+ };
0 commit comments