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.4.2.1 #include <Pegasus/Common/IPCExceptions.h>
|
40 mike 1.4 #include <Pegasus/Common/Magic.h>
|
41 mike 1.4.2.1 #include <Pegasus/Common/Threads.h>
|
42 mike 1.2
43 PEGASUS_NAMESPACE_BEGIN
44
|
45 mike 1.4.2.1 //==============================================================================
46 //
47 // MutexRep
48 //
49 //==============================================================================
50
51 #if defined(PEGASUS_HAVE_PTHREADS)
|
52 mike 1.4.2.2 typedef pthread_mutex_t MutexType;
|
53 mike 1.4.2.7 inline void mutex_lock(MutexType* mutex) { pthread_mutex_lock(mutex); }
54 inline void mutex_unlock(MutexType* mutex) { pthread_mutex_unlock(mutex); }
|
55 mike 1.4.2.1 struct MutexRep
56 {
|
57 mike 1.4.2.7 pthread_mutex_t mutex;
|
58 mike 1.4.2.8 int count;
|
59 mike 1.4.2.1 };
|
60 mike 1.4.2.2 # define PEGASUS_MUTEX_INITIALIZER PTHREAD_MUTEX_INITIALIZER
|
61 mike 1.4.2.1 #endif
62
63 #if defined(PEGASUS_HAVE_WINDOWS_THREADS)
|
64 mike 1.4.2.2 typedef HANDLE MutexType;
|
65 mike 1.4.2.7 inline void mutex_lock(MutexType* m) { WaitForSingleObject(*m, INFINITE); }
66 inline void mutex_unlock(MutexType* m) { ReleaseMutex(*m); }
|
67 mike 1.4.2.1 struct MutexRep
68 {
|
69 mike 1.4.2.3 MutexType handle;
|
70 mike 1.4.2.7 size_t count;
|
71 mike 1.4.2.1 };
|
72 mike 1.4.2.3 # define PEGASUS_MUTEX_INITIALIZER (CreateMutex(NULL, FALSE, NULL))
|
73 mike 1.4.2.1 #endif
74
75 //==============================================================================
76 //
77 // Mutex
78 //
79 //==============================================================================
80
|
81 mike 1.2 class PEGASUS_COMMON_LINKAGE Mutex
82 {
83 public:
84
85 Mutex();
86
|
87 mike 1.4.2.6 ~Mutex();
|
88 mike 1.2
|
89 mike 1.4.2.7 void lock();
|
90 mike 1.4.2.1
|
91 mike 1.4.2.4 void try_lock();
|
92 mike 1.4.2.1
|
93 mike 1.4.2.4 void timed_lock(Uint32 milliseconds);
|
94 mike 1.2
|
95 mike 1.4.2.7 void unlock();
|
96 mike 1.2
97 private:
|
98 mike 1.4.2.1 Mutex(const Mutex&);
99 Mutex& operator=(const Mutex&);
100
101 MutexRep _rep;
102 Magic<0x57D11485> _magic;
103
104 friend class Condition;
105 };
106
107 //==============================================================================
108 //
109 // AutoMutex
110 //
111 //==============================================================================
112
113 class PEGASUS_COMMON_LINKAGE AutoMutex
114 {
115 public:
116
|
117 mike 1.4.2.8 AutoMutex(Mutex& mutex) : _mutex(mutex)
|
118 mike 1.4.2.1 {
|
119 mike 1.4.2.8 _mutex.lock();
|
120 mike 1.4.2.1 }
121
122 ~AutoMutex()
|
123 mike 1.2 {
|
124 mike 1.4.2.8 _mutex.unlock();
|
125 mike 1.2 }
126
|
127 mike 1.4.2.1 private:
|
128 mike 1.4.2.7 AutoMutex(); // Unimplemented
129 AutoMutex(const AutoMutex& x); // Unimplemented
130 AutoMutex& operator=(const AutoMutex& x); // Unimplemented
|
131 mike 1.4.2.1
132 Mutex& _mutex;
|
133 mike 1.2 };
134
135 PEGASUS_NAMESPACE_END
136
137 #endif /* Pegasus_Mutex_h */
|