LRAUV  revA
Mutex.h
Go to the documentation of this file.
1 
9 #ifndef MUTEX_H_
10 #define MUTEX_H_
11 
12 #include <pthread.h>
13 #include "utils/Timestamp.h"
14 
20 class Mutex
21 {
22 
23 public:
24 
25  friend class ThreadCondition;
26 
28  Mutex();
29 
31  int lock();
32 
34  int unlock();
35 
37  bool tryLock();
38 
39 
40 private:
42  pthread_mutex_t mutex_;
43  static pthread_mutexattr_t MutexAttr_;
44  static bool MutexAttrInitialized_;
45 
46 };
47 
54 {
55 public:
56  ThreadCondition( Mutex& mutex );
57 
58  int wait();
59 
60  int timedWait( const Timespan& waitFor );
61 
62  int signal();
63 
64  int broadcast();
65 
66 private:
67  pthread_cond_t condition_;
69 };
70 
77 {
78 public:
80  MutexLocker( Mutex& mutex );
81 
83  ~MutexLocker();
84 
85 private:
86 
89 };
90 
91 
92 
93 #endif /*MUTEX_H_*/
static bool MutexAttrInitialized_
Definition: Mutex.h:44
An implementation of RAII for the Mutex.
Definition: Mutex.h:76
static pthread_mutexattr_t MutexAttr_
Definition: Mutex.h:43
bool tryLock()
Returns true if able to lock the mutex, false if already locked.
Definition: Mutex.cpp:67
Mutex()
Constructor initializes the mutex.
Definition: Mutex.cpp:17
int broadcast()
Definition: Mutex.cpp:101
int timedWait(const Timespan &waitFor)
Definition: Mutex.cpp:89
int signal()
Definition: Mutex.cpp:96
int unlock()
Unlock the mutex.
Definition: Mutex.cpp:51
A simple pthread Mutex abstraction.
Definition: Mutex.h:20
Timespan class, represents a delta of time.
Definition: Timestamp.h:248
pthread_mutex_t mutex_
The Mutex itself.
Definition: Mutex.h:42
Mutex & mutex_
Reference to keep track of the Mutex.
Definition: Mutex.h:88
ThreadCondition(Mutex &mutex)
Definition: Mutex.cpp:78
int wait()
Definition: Mutex.cpp:84
MutexLocker(Mutex &mutex)
Constructor "creates" a lock on the mutex.
Definition: Mutex.cpp:107
pthread_cond_t condition_
Definition: Mutex.h:67
A simple pthread thread condition wrapper.
Definition: Mutex.h:53
Contains the Timestamp and Timespan class declarations.
~MutexLocker()
Destructor "destroys" the lock on the mutex.
Definition: Mutex.cpp:114
int lock()
Lock the mutex.
Definition: Mutex.cpp:34
Mutex & mutex_
Definition: Mutex.h:68