FRI-OS
mutex.hpp
Go to the documentation of this file.
1 
4 #ifndef __FRIOS_POSIX_THREADS_MUTEX_HPP__
5 #define __FRIOS_POSIX_THREADS_MUTEX_HPP__
6 
7 #include <frios/posix/error.hpp>
9 
10 #include <pthread.h>
11 
12 namespace frios {
13 namespace posix {
14 
15 class cond;
16 
18 
21 class mutex
22 {
23 private:
25 
26  friend class cond;
27 
28  mutex(const mutex&) = delete;
29 public:
31 
33  struct init_error : error
34  {
35  init_error(int error_code)
36  : error(error_code)
37  { }
38  };
39 
41 
44  mutex(void)
45  {
46  int fail = pthread_mutex_init(&_mutex, 0);
47  if(fail) throw init_error(fail);
48  }
49 
51 
54  mutex(const mutex_attr& attributes)
55  {
56  int fail = pthread_mutex_init(&_mutex, &attributes._attr);
57  if(fail) throw init_error(fail);
58  }
59 
61  mutex(mutex&& tmp)
62  : _mutex(std::move(tmp._mutex))
63  { }
64 
66  ~mutex(void)
67  {
68  if(_mutex.valid())
69  pthread_mutex_destroy(&_mutex);
70  }
71 
73 
76  struct lock_error : error
77  {
78  lock_error(int error_code)
79  : error(error_code)
80  { }
81  };
82 
84 
88  {
89  unlock_error(int error_code)
90  : lock_error(error_code)
91  { }
92  };
93 
94 
96 
99  struct locked : error
100  {
101  locked(int error_code)
102  : error(error_code)
103  { }
104  };
105 
107 
133  class lock
134  {
135  private:
136  pthread_mutex_t* _p_mutex;
137 
138  friend class cond;
139  public:
141 
145  : _p_mutex(&mutex._mutex)
146  {
147  int fail = pthread_mutex_lock(_p_mutex);
148  if(fail) throw lock_error(fail);
149  }
150 
151  lock(mutex& mutex, bool)
152  : _p_mutex(&mutex._mutex)
153  {
154  int fail = pthread_mutex_trylock(_p_mutex);
155  if(fail == EBUSY) throw locked(EBUSY);
156  else if(fail) throw lock_error(fail);
157  }
158 
160  ~lock(void)
161  {
162  int fail = pthread_mutex_unlock(_p_mutex);
163  assert(!fail);
164  }
165  };
166 
167 
169 
172  class unlock
173  {
174  private:
175  pthread_mutex_t* _p_mutex;
176  public:
178 
181  unlock(mutex& mutex)
182  : _p_mutex(&mutex._mutex)
183  {
184  int fail = pthread_mutex_unlock(_p_mutex);
185  if(fail) throw unlock_error(fail);
186  }
187 
189  ~unlock(void)
190  {
191  int fail = pthread_mutex_lock(_p_mutex);
192  assert(!fail);
193  }
194  };
195 
197 
201  struct try_lock : lock
202  {
204 
208  try_lock(mutex& mutex)
209  : lock(mutex, true)
210  { }
211  };
212 };
213 
214 } // namespace posix
215 } // namespace frios
216 
217 #endif // include guard
~lock(void)
Unlocks the mutex locked in the constructor.
Definition: mutex.hpp:160
mutex(void)
Default construction.
Definition: mutex.hpp:44
Wraper around POSIX condition variables.
Definition: cond.hpp:18
Exception class for unlocking operation errors.
Definition: mutex.hpp:87
mutex(const mutex_attr &attributes)
Construction from mutex attributes.
Definition: mutex.hpp:54
mutex(mutex &&tmp)
Mutexes are movable.
Definition: mutex.hpp:61
Base class for all POSIX exceptions.
Definition: error.hpp:14
unlock(mutex &mutex)
Unlocks the specified mutex.
Definition: mutex.hpp:181
~mutex(void)
Destructor frees any resources related to the mutex.
Definition: mutex.hpp:66
Class optionally locking a mutex if it is free.
Definition: mutex.hpp:201
Wrapper around POSIX mutexes.
Definition: mutex.hpp:21
Exception class indicating that a mutex is already locked.
Definition: mutex.hpp:99
Class locking a mutex on construction and unlocking on destruction.
Definition: mutex.hpp:133
Declaration of exception classes for POSIX errors.
Exception class for locking operation errors.
Definition: mutex.hpp:76
try_lock(mutex &mutex)
Tries to lock the specified mutex throws if the mutex is locked.
Definition: mutex.hpp:208
Wrapper around POSIX mutex attributes.
~unlock(void)
Locks the mutex unlocked in the constructor.
Definition: mutex.hpp:189
Exception class for mutex initialization errors.
Definition: mutex.hpp:33
error(int error_code)
Construction from POSIX error code.
Definition: error.hpp:26
Class unlocking a mutex on construction and locking on destruction.
Definition: mutex.hpp:172
Wrapper around POSIX condition variable attributes.
Definition: mutex_attr.hpp:18
lock(mutex &mutex)
Locks the specified mutex.
Definition: mutex.hpp:144