1 karl 1.3 //%2006////////////////////////////////////////////////////////////////////////
|
2 mike 1.2 //
3 // Copyright (c) 2000, 2001, 2002 BMC Software; Hewlett-Packard Development
4 // Company, L.P.; IBM Corp.; The Open Group; Tivoli Systems.
5 // Copyright (c) 2003 BMC Software; Hewlett-Packard Development Company, L.P.;
6 // IBM Corp.; EMC Corporation, The Open Group.
7 // Copyright (c) 2004 BMC Software; Hewlett-Packard Development Company, L.P.;
8 // IBM Corp.; EMC Corporation; VERITAS Software Corporation; The Open Group.
9 // Copyright (c) 2005 Hewlett-Packard Development Company, L.P.; IBM Corp.;
10 // EMC Corporation; VERITAS Software Corporation; The Open Group.
|
11 karl 1.3 // Copyright (c) 2006 Hewlett-Packard Development Company, L.P.; IBM Corp.;
12 // EMC Corporation; Symantec Corporation; The Open Group.
|
13 mike 1.2 //
14 // Permission is hereby granted, free of charge, to any person obtaining a copy
15 // of this software and associated documentation files (the "Software"), to
16 // deal in the Software without restriction, including without limitation the
17 // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
18 // sell copies of the Software, and to permit persons to whom the Software is
19 // furnished to do so, subject to the following conditions:
|
20 karl 1.3 //
|
21 mike 1.2 // THE ABOVE COPYRIGHT NOTICE AND THIS PERMISSION NOTICE SHALL BE INCLUDED IN
22 // ALL COPIES OR SUBSTANTIAL PORTIONS OF THE SOFTWARE. THE SOFTWARE IS PROVIDED
23 // "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT
24 // LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
25 // PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
26 // HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
27 // ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29 //
30 //==============================================================================
31 //
32 //%/////////////////////////////////////////////////////////////////////////////
33
34 #ifndef Pegasus_Mutex_h
35 #define Pegasus_Mutex_h
36
37 #include <Pegasus/Common/Config.h>
38 #include <Pegasus/Common/Linkage.h>
|
39 mike 1.5 #include <Pegasus/Common/IPCExceptions.h>
|
40 mike 1.4 #include <Pegasus/Common/Magic.h>
|
41 mike 1.5 #include <Pegasus/Common/Threads.h>
|
42 mike 1.2
43 PEGASUS_NAMESPACE_BEGIN
44
|
45 mike 1.5 //==============================================================================
46 //
47 // MutexRep
48 //
49 //==============================================================================
50
51 #if defined(PEGASUS_HAVE_PTHREADS)
52 typedef pthread_mutex_t MutexType;
53 inline void mutex_lock(MutexType* mutex) { pthread_mutex_lock(mutex); }
54 inline void mutex_unlock(MutexType* mutex) { pthread_mutex_unlock(mutex); }
55 struct MutexRep
56 {
57 pthread_mutex_t mutex;
58 int count;
59 };
60 # define PEGASUS_MUTEX_INITIALIZER PTHREAD_MUTEX_INITIALIZER
61 #endif
62
63 #if defined(PEGASUS_HAVE_WINDOWS_THREADS)
64 typedef HANDLE MutexType;
65 inline void mutex_lock(MutexType* m) { WaitForSingleObject(*m, INFINITE); }
66 mike 1.5 inline void mutex_unlock(MutexType* m) { ReleaseMutex(*m); }
67 struct MutexRep
68 {
69 MutexType handle;
70 size_t count;
71 };
72 # define PEGASUS_MUTEX_INITIALIZER (CreateMutex(NULL, FALSE, NULL))
73 #endif
74
75 //==============================================================================
76 //
77 // Mutex
78 //
79 //==============================================================================
80
|
81 mike 1.2 class PEGASUS_COMMON_LINKAGE Mutex
82 {
83 public:
84
|
85 mike 1.6 enum RecursiveTag { RECURSIVE };
86 enum NonRecursiveTag { NON_RECURSIVE };
87
88 /** Default constructor creates a recursive mutex.
89 */
|
90 mike 1.2 Mutex();
91
|
92 mike 1.6 /** Call as Mutex(Mutex::RECURSIVE) to create a recursive mutex.
93 */
94 Mutex(RecursiveTag);
95
96 /** Call as Mutex(Mutex::NON_RECURSIVE) to create a non-recursive mutex.
97 */
98 Mutex(NonRecursiveTag);
99
|
100 mike 1.2 ~Mutex();
101
|
102 mike 1.5 void lock();
103
104 void try_lock();
105
106 void timed_lock(Uint32 milliseconds);
|
107 mike 1.2
108 void unlock();
109
|
110 marek 1.10.4.2 #if defined(PEGASUS_OS_LINUX)
|
111 kumpf 1.9 /**
112 This method must only be called after a fork() to reset the mutex
113 lock status in the new process. Any other use of this method is
114 unsafe.
115 */
116 void reinitialize();
117 #endif
118
|
119 mike 1.5 private:
120 Mutex(const Mutex&);
121 Mutex& operator=(const Mutex&);
122
123 MutexRep _rep;
124 Magic<0x57D11485> _magic;
125
126 friend class Condition;
127 };
|
128 mike 1.2
|
129 mike 1.5 //==============================================================================
130 //
131 // AutoMutex
132 //
133 //==============================================================================
134
135 class PEGASUS_COMMON_LINKAGE AutoMutex
136 {
137 public:
138
139 AutoMutex(Mutex& mutex) : _mutex(mutex)
|
140 mike 1.2 {
|
141 mike 1.5 _mutex.lock();
|
142 mike 1.2 }
143
|
144 mike 1.5 ~AutoMutex()
145 {
146 _mutex.unlock();
147 }
|
148 mike 1.2
|
149 mike 1.5 private:
150 AutoMutex(); // Unimplemented
151 AutoMutex(const AutoMutex& x); // Unimplemented
152 AutoMutex& operator=(const AutoMutex& x); // Unimplemented
|
153 mike 1.4
|
154 mike 1.5 Mutex& _mutex;
|
155 mike 1.2 };
156
|
157 kumpf 1.9 //==============================================================================
158 //
159 // PEGASUS_FORK_SAFE_MUTEX
160 //
161 //==============================================================================
162
163 // Use of this macro ensures that a static Mutex is not locked across a fork().
164
165 #if !defined(PEGASUS_HAVE_PTHREADS) || \
|
166 marek 1.10.4.2 (defined(PEGASUS_OS_ZOS) || \
|
167 kumpf 1.9 defined(PEGASUS_OS_VMS)
168
169 # define PEGASUS_FORK_SAFE_MUTEX(mutex)
170
|
171 marek 1.10.4.2 #elif defined(PEGASUS_OS_LINUX)
|
172 kumpf 1.9
173 # define PEGASUS_FORK_SAFE_MUTEX(mutex) \
174 class ForkSafeMutex ## mutex \
175 { \
176 public: \
177 ForkSafeMutex ## mutex() \
178 { \
179 pthread_atfork( \
180 0, \
181 0, \
182 _reinitializeMutex); \
183 } \
184 \
185 private: \
186 static void _reinitializeMutex() \
187 { \
188 mutex.reinitialize(); \
189 } \
190 }; \
191 \
192 static ForkSafeMutex ## mutex __forkSafeMutex ## mutex;
193 kumpf 1.9
194 #else
195
196 # define PEGASUS_FORK_SAFE_MUTEX(mutex) \
197 class ForkSafeMutex ## mutex \
198 { \
199 public: \
200 ForkSafeMutex ## mutex() \
201 { \
202 pthread_atfork( \
203 _lockMutex, \
204 _unlockMutex, \
205 _unlockMutex); \
206 } \
207 \
208 private: \
209 static void _lockMutex() \
210 { \
211 mutex.lock(); \
212 } \
213 \
214 kumpf 1.9 static void _unlockMutex() \
215 { \
216 mutex.unlock(); \
217 } \
218 }; \
219 \
220 static ForkSafeMutex ## mutex __forkSafeMutex ## mutex;
221
222 #endif
223
|
224 mike 1.2 PEGASUS_NAMESPACE_END
225
226 #endif /* Pegasus_Mutex_h */
|