Scopira  20080306
mutex_pthreads.h
1 
2 /*
3  * Copyright (c) 2001-2004 National Research Council
4  *
5  * All rights reserved.
6  *
7  * This material is confidential and proprietary information of
8  * National Research Council Canada ("Confidential Information").
9  * This Confidential Information may only be used and reproduced
10  * in accordance with the terms of the license agreement.
11  *
12  */
13 
14 #ifndef __INCLUDED_SCOPIRA_TOOL_MUTEX_PTHREADS_H__
15 #define __INCLUDED_SCOPIRA_TOOL_MUTEX_PTHREADS_H__
16 
17 #include <scopira/tool/platform.h>
18 #include <scopira/tool/export.h>
19 
20 #include <pthread.h>
21 
22 #include <errno.h>
23 
24 namespace scopira
25 {
26  namespace tool
27  {
28  class mutex;
29  class locker;
30  }
31 }
32 
41 {
42  protected:
43  pthread_mutex_t dm_mut;
44 
45  public:
47  SCOPIRA_EXPORT mutex(void);
48  SCOPIRA_EXPORT ~mutex(void);
49 
55  SCOPIRA_EXPORT void lock(void) { pthread_mutex_lock(&dm_mut); }
56 
63  SCOPIRA_EXPORT void unlock(void) { pthread_mutex_unlock(&dm_mut); }
64 
75  SCOPIRA_EXPORT bool try_lock(void) { return EBUSY != pthread_mutex_trylock(&dm_mut); }
76 
81  SCOPIRA_EXPORT inline pthread_mutex_t * get_os_mutex(void) { return &dm_mut; }
82 };
83 
93 {
94  private:
95  mutex &dm_mut;
96  public:
101  locker(mutex &mut) : dm_mut(mut) { dm_mut.lock(); }
106  ~locker() { dm_mut.unlock(); }
107 };
108 
109 #endif
110 
locker(mutex &mut)
Definition: mutex_pthreads.h:101
Definition: mutex_pthreads.h:40
Definition: archiveflow.h:20
pthread_mutex_t * get_os_mutex(void)
Definition: mutex_pthreads.h:81
void lock(void)
Definition: mutex_pthreads.h:55
void unlock(void)
Definition: mutex_pthreads.h:63
Definition: mutex_pthreads.h:92
~locker()
Definition: mutex_pthreads.h:106
bool try_lock(void)
Definition: mutex_pthreads.h:75