distortos  v0.4.0
object-oriented C++ RTOS for microcontrollers
ConditionVariable.hpp
Go to the documentation of this file.
1 
12 #ifndef INCLUDE_DISTORTOS_CONDITIONVARIABLE_HPP_
13 #define INCLUDE_DISTORTOS_CONDITIONVARIABLE_HPP_
14 
16 
18 
19 #include "distortos/TickClock.hpp"
20 
21 namespace distortos
22 {
23 
24 class Mutex;
25 
36 {
37 public:
38 
47  constexpr ConditionVariable() :
48  blockedList_{}
49  {
50 
51  }
52 
65  void notifyAll();
66 
79  void notifyOne();
80 
99  int wait(Mutex& mutex);
100 
120  template<typename Predicate>
121  int wait(Mutex& mutex, Predicate predicate);
122 
145  int waitFor(Mutex& mutex, TickClock::duration duration);
146 
170  template<typename Rep, typename Period>
171  int waitFor(Mutex& mutex, const std::chrono::duration<Rep, Period> duration)
172  {
173  return waitFor(mutex, std::chrono::duration_cast<TickClock::duration>(duration));
174  }
175 
202  template<typename Rep, typename Period, typename Predicate>
203  int waitFor(Mutex& mutex, const std::chrono::duration<Rep, Period> duration, Predicate predicate)
204  {
205  return waitUntil(mutex, TickClock::now() + duration + TickClock::duration{1}, std::move(predicate));
206  }
207 
230  int waitUntil(Mutex& mutex, TickClock::time_point timePoint);
231 
254  template<typename Duration>
255  int waitUntil(Mutex& mutex, const std::chrono::time_point<TickClock, Duration> timePoint)
256  {
257  return waitUntil(mutex, std::chrono::time_point_cast<TickClock::duration>(timePoint));
258  }
259 
285  template<typename Duration, typename Predicate>
286  int waitUntil(Mutex& mutex, std::chrono::time_point<TickClock, Duration> timePoint, Predicate predicate);
287 
288 private:
289 
292 };
293 
294 template<typename Predicate>
295 int ConditionVariable::wait(Mutex& mutex, Predicate predicate)
296 {
298 
299  while (predicate() == false)
300  {
301  const auto ret = wait(mutex);
302  if (ret != 0)
303  return ret;
304  }
305 
306  return 0;
307 }
308 
309 template<typename Duration, typename Predicate>
310 int ConditionVariable::waitUntil(Mutex& mutex, const std::chrono::time_point<TickClock, Duration> timePoint, Predicate predicate)
311 {
313 
314  while (predicate() == false)
315  {
316  const auto ret = waitUntil(mutex, timePoint);
317  if (ret != 0)
318  return ret;
319  }
320 
321  return 0;
322 }
323 
324 } // namespace distortos
325 
326 #endif // INCLUDE_DISTORTOS_CONDITIONVARIABLE_HPP_
constexpr ConditionVariable()
ConditionVariable constructor.
Definition: ConditionVariable.hpp:47
int waitFor(Mutex &mutex, const std::chrono::duration< Rep, Period > duration)
Waits for notification for given duration of time.
Definition: ConditionVariable.hpp:171
int waitUntil(Mutex &mutex, const std::chrono::time_point< TickClock, Duration > timePoint)
Waits for notification until given time point.
Definition: ConditionVariable.hpp:255
ConditionVariable is an advanced synchronization primitive.
Definition: ConditionVariable.hpp:35
int waitFor(Mutex &mutex, const std::chrono::duration< Rep, Period > duration, Predicate predicate)
Waits for predicate to become true for given duration of time.
Definition: ConditionVariable.hpp:203
ThreadList class header.
int wait(Mutex &mutex)
Waits for notification.
Definition: ConditionVariable.cpp:45
void notifyOne()
Notifies one waiting thread.
Definition: ConditionVariable.cpp:37
void notifyAll()
Notifies all waiting threads.
Definition: ConditionVariable.cpp:29
std::chrono::time_point< TickClock > time_point
basic time_point type of clock
Definition: TickClock.hpp:42
CHECK_FUNCTION_CONTEXT() macro.
TickClock class header.
static time_point now()
Definition: TickClock.cpp:20
Top-level namespace of distortos project.
Mutex is the basic synchronization primitive.
Definition: Mutex.hpp:30
sorted intrusive list of threads (thread control blocks)
Definition: ThreadList.hpp:55
#define CHECK_FUNCTION_CONTEXT()
Macro used to check whether the function is executed from thread context.
Definition: CHECK_FUNCTION_CONTEXT.hpp:29
std::chrono::duration< rep, period > duration
basic duration type of clock
Definition: TickClock.hpp:39
int waitUntil(Mutex &mutex, TickClock::time_point timePoint)
Waits for notification until given time point.
Definition: ConditionVariable.cpp:65
internal::ThreadList blockedList_
ThreadControlBlock objects blocked on this condition variable.
Definition: ConditionVariable.hpp:291
int waitFor(Mutex &mutex, TickClock::duration duration)
Waits for notification for given duration of time.
Definition: ConditionVariable.cpp:60