CARMA C++
PthreadMutex.h
1 #ifndef CARMA_UTIL_PTHREAD_MUTEX_H
2 #define CARMA_UTIL_PTHREAD_MUTEX_H
3 
4 #include <pthread.h>
5 #include <cerrno>
6 
7 #include "carma/util/posixErrors.h"
8 
9 
10 namespace carma {
11 namespace util {
12 
13 
14 class PthreadMutexAttr;
15 class PthreadMutex;
16 
17 
18 void lockMutex( ::pthread_mutex_t & );
19 void lockMutex( PthreadMutex & );
20 
21 bool tryLockMutex( ::pthread_mutex_t & );
22 bool tryLockMutex( PthreadMutex & );
23 
24 void unlockMutex( ::pthread_mutex_t & );
25 void unlockMutex( PthreadMutex & );
26 
27 int unlockMutexNoThrow( ::pthread_mutex_t & );
28 int unlockMutexNoThrow( PthreadMutex & );
29 
30 
40 
41 class PthreadMutex {
42  public:
43 
49 
50  explicit PthreadMutex( );
51 
52 
58 
59  explicit PthreadMutex( int type );
60 
61 
67 
68  explicit PthreadMutex( const PthreadMutexAttr & attr );
69 
70 
76 
77  explicit PthreadMutex( const ::pthread_mutexattr_t & attr );
78 
79 
87 
88  virtual ~PthreadMutex( );
89 
90 
105 
106  void Lock( );
107 
108 
127 
128  bool TryLock( );
129 
130 
141 
142  void Unlock( );
143 
144 
145  int UnlockNoThrow( );
146 
147 
156 
157  const ::pthread_mutex_t & InternalPthreadMutex( ) const;
158 
159 
168 
169  ::pthread_mutex_t & InternalPthreadMutex( );
170 
171 
172  private:
173  // no copying
174  PthreadMutex( const PthreadMutex & rhs );
175  PthreadMutex & operator=( const PthreadMutex & rhs );
176 
177  ::pthread_mutex_t mutex_;
178 };
179 
180 
181 } // namespace carma::util
182 } // namespace carma
183 
184 
185 inline void
186 carma::util::lockMutex( ::pthread_mutex_t & m )
187 {
188  failIfPosixError( ::pthread_mutex_lock( &m ),
189  "::pthread_mutex_lock error" );
190 }
191 
192 
193 inline bool
194 carma::util::tryLockMutex( ::pthread_mutex_t & m )
195 {
196  bool result = false;
197 
198  const int posixRetVal = ::pthread_mutex_trylock( &m );
199 
200  switch ( posixRetVal ) {
201  case 0:
202  result = true;
203  break;
204 
205  case EBUSY:
206  result = false;
207  break;
208 
209  default:
210  throwPosixError( posixRetVal, "::pthread_mutex_lock error" );
211  }
212 
213  return result;
214 }
215 
216 
217 inline void
218 carma::util::unlockMutex( ::pthread_mutex_t & m )
219 {
220  failIfPosixError( ::pthread_mutex_unlock( &m ),
221  "::pthread_mutex_unlock error" );
222 }
223 
224 
225 inline int
226 carma::util::unlockMutexNoThrow( ::pthread_mutex_t & m )
227 {
228  return ::pthread_mutex_unlock( &m );
229 }
230 
231 
232 inline void
234 {
235  lockMutex( mutex_ );
236 }
237 
238 
239 inline bool
241 {
242  return tryLockMutex( mutex_ );
243 }
244 
245 
246 inline void
248 {
249  unlockMutex( mutex_ );
250 }
251 
252 
253 inline int
254 carma::util::PthreadMutex::UnlockNoThrow( )
255 {
256  return unlockMutexNoThrow( mutex_ );
257 }
258 
259 
260 inline const ::pthread_mutex_t &
262 {
263  return mutex_;
264 }
265 
266 
267 inline ::pthread_mutex_t &
269 {
270  return mutex_;
271 }
272 
273 
274 inline void
275 carma::util::lockMutex( PthreadMutex & m )
276 {
277  m.Lock();
278 }
279 
280 
281 inline bool
282 carma::util::tryLockMutex( PthreadMutex & m )
283 {
284  return m.TryLock();
285 }
286 
287 
288 inline void
289 carma::util::unlockMutex( PthreadMutex & m )
290 {
291  m.Unlock();
292 }
293 
294 
295 inline int
296 carma::util::unlockMutexNoThrow( PthreadMutex & m )
297 {
298  return m.UnlockNoThrow();
299 }
300 
301 
302 #endif
PthreadMutex()
Construct a mutex with the CARMA defaults.
virtual ~PthreadMutex()
Destruct a mutex.
bool TryLock()
Obtain a lock on the instance for the caller&#39;s thread and return true if it is possible to do so imme...
Definition: PthreadMutex.h:240
void Lock()
Obtain a lock on the instance for the caller&#39;s thread.
Definition: PthreadMutex.h:233
A simple wrapper class that makes use of ::pthread_mutexattr_t easier in a C++ world.
A simple wrapper class that makes use of ::pthread_mutex_t easier in a C++ world. ...
Definition: PthreadMutex.h:41
const ::pthread_mutex_t & InternalPthreadMutex() const
Obtain a reference to the internal ::pthread_mutex_t.
Definition: PthreadMutex.h:261
void Unlock()
Release a lock that is held on the instance by the caller&#39;s thread.
Definition: PthreadMutex.h:247