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 // Author: Mike Day (mdday@us.ibm.com)
33 //
34 // Modified By: Markus Mueller
35 // Ramnath Ravindran (Ramnath.Ravindran@compaq.com)
36 // David Eger (dteger@us.ibm.com)
37 // Amit K Arora, IBM (amita@in.ibm.com) for PEP#101
38 // Sean Keenan, Hewlett-Packard Company (sean.keenan@hp.com)
39 // Roger Kumpf, Hewlett-Packard Company (roger_kumpf@hp.com)
40 // David Dillard, VERITAS Software Corp.
41 // (david.dillard@veritas.com)
42 mike 1.2 // Aruran, IBM (ashanmug@in.ibm.com) for BUG# 3518
43 //
|
44 mike 1.4.2.1 // Reworked By: Mike Brasher (m.brasher@inovadevelopment.com)
45 //
|
46 mike 1.2 //%/////////////////////////////////////////////////////////////////////////////
47
48 #ifndef Pegasus_Mutex_h
49 #define Pegasus_Mutex_h
50
51 #include <Pegasus/Common/Config.h>
52 #include <Pegasus/Common/Linkage.h>
|
53 mike 1.4.2.1 #include <Pegasus/Common/IPCExceptions.h>
|
54 mike 1.4 #include <Pegasus/Common/Magic.h>
|
55 mike 1.4.2.1 #include <Pegasus/Common/Threads.h>
|
56 mike 1.2
57 PEGASUS_NAMESPACE_BEGIN
58
|
59 mike 1.4.2.1 //==============================================================================
60 //
61 // MutexRep
62 //
63 //==============================================================================
64
65 #if defined(PEGASUS_HAVE_PTHREADS)
|
66 mike 1.4.2.2 typedef pthread_mutex_t MutexType;
|
67 mike 1.4.2.1 struct MutexRep
68 {
69 pthread_mutex_t mutex;
70 pthread_mutexattr_t attr;
|
71 mike 1.4.2.2 ThreadType owner;
|
72 mike 1.4.2.1 };
|
73 mike 1.4.2.2 inline void mutex_lock(MutexType* mutex) { pthread_mutex_lock(mutex); }
74 inline void mutex_unlock(MutexType* mutex) { pthread_mutex_unlock(mutex); }
75 # define PEGASUS_MUTEX_INITIALIZER PTHREAD_MUTEX_INITIALIZER
|
76 mike 1.4.2.1 #endif
77
78 #if defined(PEGASUS_HAVE_WINDOWS_THREADS)
|
79 mike 1.4.2.2 typedef HANDLE MutexType;
|
80 mike 1.4.2.1 struct MutexRep
81 {
|
82 mike 1.4.2.3 MutexType handle;
83 ThreadType owner;
|
84 mike 1.4.2.1 size_t count;
85 };
|
86 mike 1.4.2.3 inline void mutex_lock(MutexType* m) { WaitForSingleObject(*m, INFINITE); }
87 inline void mutex_unlock(MutexType* m) { ReleaseMutex(*m); }
88 # define PEGASUS_MUTEX_INITIALIZER (CreateMutex(NULL, FALSE, NULL))
|
89 mike 1.4.2.1 #endif
90
91 //==============================================================================
92 //
93 // Mutex
94 //
95 //==============================================================================
96
|
97 mike 1.2 class PEGASUS_COMMON_LINKAGE Mutex
98 {
99 public:
100
101 Mutex();
102
103 ~Mutex();
104
|
105 mike 1.4.2.1 void lock(ThreadType caller = Threads::self());
106
107 void try_lock(ThreadType caller = Threads::self());
108
109 void timed_lock(Uint32 milliseconds, ThreadType caller = Threads::self());
|
110 mike 1.2
111 void unlock();
112
|
113 mike 1.4.2.1 ThreadType get_owner() { return _rep.owner; }
114
115 void set_owner(ThreadType caller) { _rep.owner = caller; }
|
116 mike 1.2
117 private:
|
118 mike 1.4.2.1 Mutex(const Mutex&);
119 Mutex& operator=(const Mutex&);
120
121
122 MutexRep _rep;
123 Magic<0x57D11485> _magic;
124
125 friend class Condition;
126 };
127
128 //==============================================================================
129 //
130 // AutoMutex
131 //
132 //==============================================================================
133
134 class PEGASUS_COMMON_LINKAGE AutoMutex
135 {
136 public:
137
138 AutoMutex(Mutex& mutex, Boolean autoLock = true) :
139 mike 1.4.2.1 _mutex(mutex), _locked(autoLock)
140 {
141 if (autoLock)
142 _mutex.lock();
143 }
144
145 ~AutoMutex()
|
146 mike 1.2 {
|
147 mike 1.4.2.1 try
148 {
149 if (_locked)
150 unlock();
151 }
152 catch (...)
153 {
154 // Do not propagate exception from destructor
155 }
|
156 mike 1.2 }
157
|
158 mike 1.4.2.1 void lock()
159 {
160 if (_locked)
161 throw AlreadyLocked(Threads::self());
|
162 mike 1.2
|
163 mike 1.4.2.1 _mutex.lock();
164 _locked = true;
165 }
|
166 mike 1.4
|
167 mike 1.4.2.1 void unlock()
168 {
169 if (!_locked)
170 throw Permission(Threads::self());
171
172 _mutex.unlock();
173 _locked = false;
174 }
175
176 Boolean isLocked() const
177 {
178 return _locked;
179 }
180
181 private:
182 AutoMutex(); // Unimplemented
183 AutoMutex(const AutoMutex& x); // Unimplemented
184 AutoMutex& operator=(const AutoMutex& x); // Unimplemented
185
186 Mutex& _mutex;
187 Boolean _locked;
|
188 mike 1.2 };
189
190 PEGASUS_NAMESPACE_END
191
192 #endif /* Pegasus_Mutex_h */
|