join 1.0
lightweight network framework library
Loading...
Searching...
No Matches
condition.hpp
Go to the documentation of this file.
1
25#ifndef __JOIN_CONDITION_HPP__
26#define __JOIN_CONDITION_HPP__
27
28// libjoin.
29#include <join/mutex.hpp>
30#include <join/error.hpp>
31#include <join/utils.hpp>
32
33// C++.
34#include <chrono>
35
36namespace join
37{
42 {
43 public:
47 Condition ();
48
53 Condition (const Condition& other) = delete;
54
60 Condition& operator= (const Condition& other) = delete;
61
66 Condition (Condition&& other) = delete;
67
73 Condition& operator= (Condition&& other) = delete;
74
78 ~Condition ();
79
83 void signal () noexcept;
84
88 void broadcast () noexcept;
89
94 template <class Lock>
95 void wait (Lock& lock)
96 {
97 pthread_cond_wait (&_handle, lock.mutex ()->handle ());
98 }
99
105 template <class Lock, class Predicate>
106 void wait (Lock& lock, Predicate pred)
107 {
108 while (!pred ())
109 {
110 wait (lock);
111 }
112 }
113
120 template <class Lock, class Rep, class Period>
121 bool timedWait (Lock& lock, std::chrono::duration <Rep, Period> timeout)
122 {
123 struct timespec ts = toTimespec (std::chrono::steady_clock::now () + timeout);
124 int err = pthread_cond_timedwait (&_handle, lock.mutex ()->handle (), &ts);
125 if (err != 0)
126 {
127 lastError = std::make_error_code (static_cast <std::errc> (err));
128 return false;
129 }
130 return true;
131 }
132
140 template <class Lock, class Rep, class Period, class Predicate>
141 bool timedWait (Lock& lock, std::chrono::duration <Rep, Period> timeout, Predicate pred)
142 {
143 struct timespec ts = toTimespec (std::chrono::steady_clock::now () + timeout);
144 while (!pred ())
145 {
146 int err = pthread_cond_timedwait (&_handle, lock.mutex ()->handle (), &ts);
147 if (err != 0)
148 {
149 lastError = std::make_error_code (static_cast <std::errc> (err));
150 return pred ();
151 }
152 }
153
154 return true;
155 }
156
157 private:
159 pthread_cond_t _handle;
160 };
161
166 {
167 public:
172
177 SharedCondition (const SharedCondition& other) = delete;
178
185
191
198
203
207 void signal () noexcept;
208
212 void broadcast () noexcept;
213
218 void wait (ScopedLock <SharedMutex>& lock);
219
225 template <class Predicate>
226 void wait (ScopedLock <SharedMutex>& lock, Predicate pred)
227 {
228 while (!pred ())
229 {
230 wait (lock);
231 }
232 }
233
240 template <class Rep, class Period>
241 bool timedWait (ScopedLock <SharedMutex>& lock, std::chrono::duration <Rep, Period> timeout)
242 {
243 struct timespec ts = toTimespec (std::chrono::steady_clock::now () + timeout);
244 int err = pthread_cond_timedwait (&_handle, lock.mutex ()->handle (), &ts);
245 if (err != 0)
246 {
247 lastError = std::make_error_code (static_cast <std::errc> (err));
248 return false;
249 }
250
251 return true;
252 }
253
261 template <class Rep, class Period, class Predicate>
262 bool timedWait (ScopedLock <SharedMutex>& lock, std::chrono::duration <Rep, Period> timeout, Predicate pred)
263 {
264 struct timespec ts = toTimespec (std::chrono::steady_clock::now () + timeout);
265 while (!pred ())
266 {
267 int err = pthread_cond_timedwait (&_handle, lock.mutex ()->handle (), &ts);
268 if (err != 0)
269 {
270 lastError = std::make_error_code (static_cast <std::errc> (err));
271 return pred ();
272 }
273 }
274
275 return true;
276 }
277
278 private:
280 pthread_cond_t _handle;
281 };
282}
283
284#endif
condition variable class.
Definition condition.hpp:42
void wait(Lock &lock, Predicate pred)
wait on a condition with predicate.
Definition condition.hpp:106
Condition & operator=(const Condition &other)=delete
copy assignment.
void wait(Lock &lock)
wait on a condition.
Definition condition.hpp:95
Condition(const Condition &other)=delete
copy constructor.
void broadcast() noexcept
unblocks all threads currently waiting.
Definition condition.cpp:69
bool timedWait(Lock &lock, std::chrono::duration< Rep, Period > timeout)
wait on a condition until timeout expire.
Definition condition.hpp:121
~Condition()
destroys the mutex object.
Definition condition.cpp:51
bool timedWait(Lock &lock, std::chrono::duration< Rep, Period > timeout, Predicate pred)
wait on a condition with predicate until timeout expire.
Definition condition.hpp:141
Condition(Condition &&other)=delete
move constructor.
Condition()
default constructor.
Definition condition.cpp:38
void signal() noexcept
unblocks one of the waiting threads.
Definition condition.cpp:60
class owning a mutex for the duration of a scoped block.
Definition mutex.hpp:245
Lockable * mutex() const
get associated mutex.
Definition mutex.hpp:300
condition variable class for shared memory.
Definition condition.hpp:166
SharedCondition & operator=(const SharedCondition &other)=delete
copy assignment.
~SharedCondition()
destroys the mutex object.
Definition condition.cpp:92
bool timedWait(ScopedLock< SharedMutex > &lock, std::chrono::duration< Rep, Period > timeout, Predicate pred)
wait on a condition with predicate until timeout expire.
Definition condition.hpp:262
bool timedWait(ScopedLock< SharedMutex > &lock, std::chrono::duration< Rep, Period > timeout)
wait on a condition until timeout expire.
Definition condition.hpp:241
void wait(ScopedLock< SharedMutex > &lock)
wait on a condition.
Definition condition.cpp:119
SharedCondition(const SharedCondition &other)=delete
copy constructor.
void signal() noexcept
unblocks one of the waiting threads.
Definition condition.cpp:101
SharedCondition()
default constructor.
Definition condition.cpp:78
SharedCondition(SharedCondition &&other)=delete
move constructor.
void broadcast() noexcept
unblocks all threads currently waiting.
Definition condition.cpp:110
class used to protect shared data from being simultaneously accessed by multiple process via a shared...
Definition mutex.hpp:175
Definition acceptor.hpp:32
struct timespec toTimespec(std::chrono::time_point< Clock, Duration > timePoint)
converts time_point to timespec.
Definition utils.hpp:423