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 mike 1.4.2.4 // Author: Mike Brasher (m.brasher@inovadevelopment.com)
|
33 mike 1.4.2.1 //
|
34 mike 1.2 //%/////////////////////////////////////////////////////////////////////////////
35
36 #ifndef Pegasus_Mutex_h
37 #define Pegasus_Mutex_h
38
39 #include <Pegasus/Common/Config.h>
40 #include <Pegasus/Common/Linkage.h>
|
41 mike 1.4.2.1 #include <Pegasus/Common/IPCExceptions.h>
|
42 mike 1.4 #include <Pegasus/Common/Magic.h>
|
43 mike 1.4.2.1 #include <Pegasus/Common/Threads.h>
|
44 mike 1.2
45 PEGASUS_NAMESPACE_BEGIN
46
|
47 mike 1.4.2.1 //==============================================================================
48 //
49 // MutexRep
50 //
51 //==============================================================================
52
53 #if defined(PEGASUS_HAVE_PTHREADS)
|
54 mike 1.4.2.2 typedef pthread_mutex_t MutexType;
|
55 mike 1.4.2.1 struct MutexRep
56 {
|
57 mike 1.4.2.5 MutexType mutex;
|
58 mike 1.4.2.1 };
|
59 mike 1.4.2.2 inline void mutex_lock(MutexType* mutex) { pthread_mutex_lock(mutex); }
60 inline void mutex_unlock(MutexType* mutex) { pthread_mutex_unlock(mutex); }
61 # define PEGASUS_MUTEX_INITIALIZER PTHREAD_MUTEX_INITIALIZER
|
62 mike 1.4.2.1 #endif
63
64 #if defined(PEGASUS_HAVE_WINDOWS_THREADS)
|
65 mike 1.4.2.2 typedef HANDLE MutexType;
|
66 mike 1.4.2.1 struct MutexRep
67 {
|
68 mike 1.4.2.3 MutexType handle;
|
69 mike 1.4.2.1 };
|
70 mike 1.4.2.3 inline void mutex_lock(MutexType* m) { WaitForSingleObject(*m, INFINITE); }
71 inline void mutex_unlock(MutexType* m) { ReleaseMutex(*m); }
72 # 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.5 void lock()
90 {
91 mutex_lock(&_rep.mutex);
92 }
|
93 mike 1.4.2.1
|
94 mike 1.4.2.4 void try_lock();
|
95 mike 1.4.2.1
|
96 mike 1.4.2.4 void timed_lock(Uint32 milliseconds);
|
97 mike 1.2
|
98 mike 1.4.2.5 void unlock()
99 {
100 mutex_unlock(&_rep.mutex);
101 }
|
102 mike 1.2
103 private:
|
104 mike 1.4.2.1 Mutex(const Mutex&);
105 Mutex& operator=(const Mutex&);
106
107
108 MutexRep _rep;
109 Magic<0x57D11485> _magic;
110
111 friend class Condition;
112 };
113
114 //==============================================================================
115 //
116 // AutoMutex
117 //
118 //==============================================================================
119
120 class PEGASUS_COMMON_LINKAGE AutoMutex
121 {
122 public:
123
|
124 mike 1.4.2.5 AutoMutex(Mutex& mutex) : _mutex(mutex)
|
125 mike 1.4.2.1 {
|
126 mike 1.4.2.5 _mutex.lock();
|
127 mike 1.4.2.1 }
128
129 ~AutoMutex()
|
130 mike 1.2 {
|
131 mike 1.4.2.5 _mutex.unlock();
|
132 mike 1.2 }
133
|
134 mike 1.4.2.1 void lock()
135 {
136 _mutex.lock();
137 }
|
138 mike 1.4
|
139 mike 1.4.2.1 void unlock()
140 {
141 _mutex.unlock();
142 }
143
144 private:
|
145 mike 1.4.2.5 AutoMutex();
146 AutoMutex(const AutoMutex& x);
147 AutoMutex& operator=(const AutoMutex& x);
|
148 mike 1.4.2.1
149 Mutex& _mutex;
|
150 mike 1.2 };
151
152 PEGASUS_NAMESPACE_END
153
154 #endif /* Pegasus_Mutex_h */
|