distortos  v0.6.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 
66  ~ConditionVariable() = default;
67 
80  void notifyAll();
81 
94  void notifyOne();
95 
114  int wait(Mutex& mutex);
115 
135  template<typename Predicate>
136  int wait(Mutex& mutex, Predicate predicate);
137 
160  int waitFor(Mutex& mutex, TickClock::duration duration);
161 
185  template<typename Rep, typename Period>
186  int waitFor(Mutex& mutex, const std::chrono::duration<Rep, Period> duration)
187  {
188  return waitFor(mutex, std::chrono::duration_cast<TickClock::duration>(duration));
189  }
190 
217  template<typename Rep, typename Period, typename Predicate>
218  int waitFor(Mutex& mutex, const std::chrono::duration<Rep, Period> duration, Predicate predicate)
219  {
220  return waitUntil(mutex, TickClock::now() + duration + TickClock::duration{1}, std::move(predicate));
221  }
222 
245  int waitUntil(Mutex& mutex, TickClock::time_point timePoint);
246 
269  template<typename Duration>
270  int waitUntil(Mutex& mutex, const std::chrono::time_point<TickClock, Duration> timePoint)
271  {
272  return waitUntil(mutex, std::chrono::time_point_cast<TickClock::duration>(timePoint));
273  }
274 
300  template<typename Duration, typename Predicate>
301  int waitUntil(Mutex& mutex, std::chrono::time_point<TickClock, Duration> timePoint, Predicate predicate);
302 
303 private:
304 
307 };
308 
309 template<typename Predicate>
310 int ConditionVariable::wait(Mutex& mutex, Predicate predicate)
311 {
313 
314  while (predicate() == false)
315  {
316  const auto ret = wait(mutex);
317  if (ret != 0)
318  return ret;
319  }
320 
321  return 0;
322 }
323 
324 template<typename Duration, typename Predicate>
325 int ConditionVariable::waitUntil(Mutex& mutex, const std::chrono::time_point<TickClock, Duration> timePoint,
326  Predicate predicate)
327 {
329 
330  while (predicate() == false)
331  {
332  const auto ret = waitUntil(mutex, timePoint);
333  if (ret != 0)
334  return ret;
335  }
336 
337  return 0;
338 }
339 
340 } // namespace distortos
341 
342 #endif // INCLUDE_DISTORTOS_CONDITIONVARIABLE_HPP_
constexpr ConditionVariable()
ConditionVariable&#39;s 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:186
int waitUntil(Mutex &mutex, const std::chrono::time_point< TickClock, Duration > timePoint)
Waits for notification until given time point.
Definition: ConditionVariable.hpp:270
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:218
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
~ConditionVariable()=default
ConditionVariable&#39;s destructor.
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:306
int waitFor(Mutex &mutex, TickClock::duration duration)
Waits for notification for given duration of time.
Definition: ConditionVariable.cpp:60