FRI-OS
cond.hpp
Go to the documentation of this file.
1 
4 #ifndef __FRIOS_POSIX_THREADS_COND_HPP__
5 #define __FRIOS_POSIX_THREADS_COND_HPP__
6 
7 #include <frios/posix/error.hpp>
10 
11 #include <pthread.h>
12 
13 namespace frios {
14 namespace posix {
15 
16 
18 class cond
19 {
20 private:
22 
24  cond(const cond&) = delete;
25 public:
27  struct init_error : error
28  {
29  init_error(int error_code)
30  : error(error_code)
31  { }
32  };
33 
35 
38  cond(void)
39  {
40  int fail = pthread_cond_init(&_cond, 0);
41  if(fail) throw init_error(fail);
42  }
43 
45 
48  cond(const cond_attr& attributes)
49  {
50  int fail = pthread_cond_init(&_cond, &attributes._attr);
51  if(fail) throw init_error(fail);
52  }
53 
55  cond(cond&& tmp)
56  : _cond(std::move(tmp._cond))
57  { }
58 
60  ~cond(void)
61  {
62  if(_cond.valid())
63  pthread_cond_destroy(&_cond);
64  }
65 
67  void wait(mutex::lock& lock)
68  {
69  pthread_cond_wait(&_cond, lock._p_mutex);
70  }
71 
73  void signal(void)
74  {
75  pthread_cond_signal(&_cond);
76  }
77 
79  void broadcast(void)
80  {
81  pthread_cond_broadcast(&_cond);
82  }
83 };
84 
85 } // namespace posix
86 } // namespace frios
87 
88 #endif // include guard
Wrapper around POSIX condition variable attributes.
Definition: cond_attr.hpp:18
Wraper around POSIX condition variables.
Definition: cond.hpp:18
void wait(mutex::lock &lock)
Wait on the condition variable.
Definition: cond.hpp:67
Wrapper around POSIX mutexes.
cond(void)
Default constructor of the condition variable.
Definition: cond.hpp:38
Base class for all POSIX exceptions.
Definition: error.hpp:14
void signal(void)
Signal on the condition variable.
Definition: cond.hpp:73
~cond(void)
Destructor frees the resources asociated with the condition variable.
Definition: cond.hpp:60
Class locking a mutex on construction and unlocking on destruction.
Definition: mutex.hpp:133
Wrapper around POSIX condition variable attributes.
Declaration of exception classes for POSIX errors.
cond(cond &&tmp)
Condition variables are movable.
Definition: cond.hpp:55
cond(const cond_attr &attributes)
Construction of a condition variable with the specified attributes.
Definition: cond.hpp:48
void broadcast(void)
Broadcast on the condition variable.
Definition: cond.hpp:79
Exception class for condition variable initialization error.
Definition: cond.hpp:27
error(int error_code)
Construction from POSIX error code.
Definition: error.hpp:26