distortos  v0.5.0
object-oriented C++ RTOS for microcontrollers
Semaphore.hpp
Go to the documentation of this file.
1 
12 #ifndef INCLUDE_DISTORTOS_SEMAPHORE_HPP_
13 #define INCLUDE_DISTORTOS_SEMAPHORE_HPP_
14 
16 
17 #include "distortos/TickClock.hpp"
18 
19 namespace distortos
20 {
21 
30 class Semaphore
31 {
32 public:
33 
35  using Value = unsigned int;
36 
48  constexpr explicit Semaphore(const Value value, const Value maxValue = std::numeric_limits<Value>::max()) :
49  blockedList_{},
50  value_{value <= maxValue ? value : maxValue},
51  maxValue_{maxValue}
52  {
53 
54  }
55 
66  {
67 
68  }
69 
78  Value getValue() const
79  {
80  return value_;
81  }
82 
99  int post();
100 
114  int tryWait();
115 
137  int tryWaitFor(TickClock::duration duration);
138 
156  template<typename Rep, typename Period>
157  int tryWaitFor(const std::chrono::duration<Rep, Period> duration)
158  {
159  return tryWaitFor(std::chrono::duration_cast<TickClock::duration>(duration));
160  }
161 
183  int tryWaitUntil(TickClock::time_point timePoint);
184 
201  template<typename Duration>
202  int tryWaitUntil(const std::chrono::time_point<TickClock, Duration> timePoint)
203  {
204  return tryWaitUntil(std::chrono::time_point_cast<TickClock::duration>(timePoint));
205  }
206 
223  int wait();
224 
225  Semaphore(const Semaphore&) = delete;
226  Semaphore(Semaphore&&) = default;
227  const Semaphore& operator=(const Semaphore&) = delete;
228  Semaphore& operator=(Semaphore&&) = delete;
229 
230 private:
231 
241  int tryWaitInternal();
242 
245 
248 
251 };
252 
253 } // namespace distortos
254 
255 #endif // INCLUDE_DISTORTOS_SEMAPHORE_HPP_
int tryWait()
Tries to lock the semaphore.
Definition: Semaphore.cpp:48
Semaphore is the basic synchronization primitive.
Definition: Semaphore.hpp:30
ThreadList class header.
~Semaphore()
Semaphore destructor.
Definition: Semaphore.hpp:65
int tryWaitFor(const std::chrono::duration< Rep, Period > duration)
Tries to lock the semaphore for given duration of time.
Definition: Semaphore.hpp:157
int tryWaitFor(TickClock::duration duration)
Tries to lock the semaphore for given duration of time.
Definition: Semaphore.cpp:54
int post()
Unlocks the semaphore.
Definition: Semaphore.cpp:30
std::chrono::time_point< TickClock > time_point
basic time_point type of clock
Definition: TickClock.hpp:42
TickClock class header.
int wait()
Locks the semaphore.
Definition: Semaphore.cpp:72
internal::ThreadList blockedList_
ThreadControlBlock objects blocked on this semaphore.
Definition: Semaphore.hpp:244
Value getValue() const
Gets current value of semaphore.
Definition: Semaphore.hpp:78
int tryWaitInternal()
Internal version of tryWait().
Definition: Semaphore.cpp:89
constexpr Semaphore(const Value value, const Value maxValue=std::numeric_limits< Value >::max())
Semaphore constructor.
Definition: Semaphore.hpp:48
Value maxValue_
max value of the semaphore
Definition: Semaphore.hpp:250
int tryWaitUntil(TickClock::time_point timePoint)
Tries to lock the semaphore until given time point.
Definition: Semaphore.cpp:59
Top-level namespace of distortos project.
sorted intrusive list of threads (thread control blocks)
Definition: ThreadList.hpp:55
Value value_
internal value of the semaphore
Definition: Semaphore.hpp:247
std::chrono::duration< rep, period > duration
basic duration type of clock
Definition: TickClock.hpp:39
int tryWaitUntil(const std::chrono::time_point< TickClock, Duration > timePoint)
Tries to lock the semaphore until given time point.
Definition: Semaphore.hpp:202
unsigned int Value
type used for semaphore&#39;s "value"
Definition: Semaphore.hpp:35