distortos  v0.7.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 
72  {
73  return maxValue_;
74  }
75 
84  Value getValue() const
85  {
86  return value_;
87  }
88 
105  int post();
106 
120  int tryWait();
121 
143  int tryWaitFor(TickClock::duration duration);
144 
162  template<typename Rep, typename Period>
163  int tryWaitFor(const std::chrono::duration<Rep, Period> duration)
164  {
165  return tryWaitFor(std::chrono::duration_cast<TickClock::duration>(duration));
166  }
167 
189  int tryWaitUntil(TickClock::time_point timePoint);
190 
207  template<typename Duration>
208  int tryWaitUntil(const std::chrono::time_point<TickClock, Duration> timePoint)
209  {
210  return tryWaitUntil(std::chrono::time_point_cast<TickClock::duration>(timePoint));
211  }
212 
229  int wait();
230 
231  Semaphore(const Semaphore&) = delete;
232  Semaphore(Semaphore&&) = default;
233  const Semaphore& operator=(const Semaphore&) = delete;
234  Semaphore& operator=(Semaphore&&) = delete;
235 
236 private:
237 
247  int tryWaitInternal();
248 
251 
254 
257 };
258 
259 } // namespace distortos
260 
261 #endif // INCLUDE_DISTORTOS_SEMAPHORE_HPP_
int tryWait()
Tries to lock the semaphore.
Definition: Semaphore.cpp:48
~Semaphore()=default
Semaphore'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:163
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:250
Value getValue() const
Gets current value of semaphore.
Definition: Semaphore.hpp:84
int tryWaitInternal()
Internal version of tryWait().
Definition: Semaphore.cpp:89
constexpr Semaphore(const Value value, const Value maxValue=std::numeric_limits< Value >::max())
Semaphore's constructor.
Definition: Semaphore.hpp:48
Value getMaxValue() const
Definition: Semaphore.hpp:71
Value maxValue_
max value of the semaphore
Definition: Semaphore.hpp:256
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.
Definition: buttons.hpp:33
sorted intrusive list of threads (thread control blocks)
Definition: ThreadList.hpp:55
Value value_
internal value of the semaphore
Definition: Semaphore.hpp:253
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:208
unsigned int Value
type used for semaphore's "value"
Definition: Semaphore.hpp:35