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 mike 1.4.2.5 inline void mutex_destroy(MutexType* mutex) { pthread_mutex_destroy(mutex); }
|
62 mike 1.4.2.2 # define PEGASUS_MUTEX_INITIALIZER PTHREAD_MUTEX_INITIALIZER
|
63 mike 1.4.2.1 #endif
64
65 #if defined(PEGASUS_HAVE_WINDOWS_THREADS)
|
66 mike 1.4.2.2 typedef HANDLE MutexType;
|
67 mike 1.4.2.1 struct MutexRep
68 {
|
69 mike 1.4.2.3 MutexType handle;
|
70 mike 1.4.2.1 size_t count;
71 };
|
72 mike 1.4.2.3 inline void mutex_lock(MutexType* m) { WaitForSingleObject(*m, INFINITE); }
73 inline void mutex_unlock(MutexType* m) { ReleaseMutex(*m); }
74 # define PEGASUS_MUTEX_INITIALIZER (CreateMutex(NULL, FALSE, NULL))
|
75 mike 1.4.2.1 #endif
76
77 //==============================================================================
78 //
79 // Mutex
80 //
81 //==============================================================================
82
|
83 mike 1.2 class PEGASUS_COMMON_LINKAGE Mutex
84 {
85 public:
86
87 Mutex();
88
|
89 mike 1.4.2.5 ~Mutex()
90 {
91 mutex_destroy(&_rep.mutex);
92 }
|
93 mike 1.2
|
94 mike 1.4.2.5 void lock()
95 {
96 mutex_lock(&_rep.mutex);
97 }
|
98 mike 1.4.2.1
|
99 mike 1.4.2.4 void try_lock();
|
100 mike 1.4.2.1
|
101 mike 1.4.2.4 void timed_lock(Uint32 milliseconds);
|
102 mike 1.2
|
103 mike 1.4.2.5 void unlock()
104 {
105 mutex_unlock(&_rep.mutex);
106 }
|
107 mike 1.2
108 private:
|
109 mike 1.4.2.1 Mutex(const Mutex&);
110 Mutex& operator=(const Mutex&);
111
112
113 MutexRep _rep;
114 Magic<0x57D11485> _magic;
115
116 friend class Condition;
117 };
118
119 //==============================================================================
120 //
121 // AutoMutex
122 //
123 //==============================================================================
124
125 class PEGASUS_COMMON_LINKAGE AutoMutex
126 {
127 public:
128
|
129 mike 1.4.2.5 AutoMutex(Mutex& mutex) : _mutex(mutex)
|
130 mike 1.4.2.1 {
|
131 mike 1.4.2.5 _mutex.lock();
|
132 mike 1.4.2.1 }
133
134 ~AutoMutex()
|
135 mike 1.2 {
|
136 mike 1.4.2.5 _mutex.unlock();
|
137 mike 1.2 }
138
|
139 mike 1.4.2.1 void lock()
140 {
141 _mutex.lock();
142 }
|
143 mike 1.4
|
144 mike 1.4.2.1 void unlock()
145 {
146 _mutex.unlock();
147 }
148
149 private:
|
150 mike 1.4.2.5 AutoMutex();
151 AutoMutex(const AutoMutex& x);
152 AutoMutex& operator=(const AutoMutex& x);
|
153 mike 1.4.2.1
154 Mutex& _mutex;
|
155 mike 1.2 };
156
157 PEGASUS_NAMESPACE_END
158
159 #endif /* Pegasus_Mutex_h */
|