distortos  v0.6.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 
65  ~Semaphore() = default;
66 
75  Value getValue() const
76  {
77  return value_;
78  }
79 
96  int post();
97 
111  int tryWait();
112 
134  int tryWaitFor(TickClock::duration duration);
135 
153  template<typename Rep, typename Period>
154  int tryWaitFor(const std::chrono::duration<Rep, Period> duration)
155  {
156  return tryWaitFor(std::chrono::duration_cast<TickClock::duration>(duration));
157  }
158 
180  int tryWaitUntil(TickClock::time_point timePoint);
181 
198  template<typename Duration>
199  int tryWaitUntil(const std::chrono::time_point<TickClock, Duration> timePoint)
200  {
201  return tryWaitUntil(std::chrono::time_point_cast<TickClock::duration>(timePoint));
202  }
203 
220  int wait();
221 
222  Semaphore(const Semaphore&) = delete;
223  Semaphore(Semaphore&&) = default;
224  const Semaphore& operator=(const Semaphore&) = delete;
225  Semaphore& operator=(Semaphore&&) = delete;
226 
227 private:
228 
238  int tryWaitInternal();
239 
242 
245 
248 };
249 
250 } // namespace distortos
251 
252 #endif // INCLUDE_DISTORTOS_SEMAPHORE_HPP_
int tryWait()
Tries to lock the semaphore.
Definition: Semaphore.cpp:48
~Semaphore()=default
Semaphore&#39;s destructor.
Semaphore is the basic synchronization primitive.
Definition: Semaphore.hpp:30
ThreadList class header.
int tryWaitFor(const std::chrono::duration< Rep, Period > duration)
Tries to lock the semaphore for given duration of time.
Definition: Semaphore.hpp:154
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:241
Value getValue() const
Gets current value of semaphore.
Definition: Semaphore.hpp:75
int tryWaitInternal()
Internal version of tryWait().
Definition: Semaphore.cpp:89
constexpr Semaphore(const Value value, const Value maxValue=std::numeric_limits< Value >::max())
Semaphore&#39;s constructor.
Definition: Semaphore.hpp:48
Value maxValue_
max value of the semaphore
Definition: Semaphore.hpp:247
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:244
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:199
unsigned int Value
type used for semaphore&#39;s "value"
Definition: Semaphore.hpp:35