distortos  v0.7.0
object-oriented C++ RTOS for microcontrollers
Mutex.hpp
Go to the documentation of this file.
1 
12 #ifndef INCLUDE_DISTORTOS_MUTEX_HPP_
13 #define INCLUDE_DISTORTOS_MUTEX_HPP_
14 
16 
17 namespace distortos
18 {
19 
31 {
32 public:
33 
36 
38  using RecursiveLocksCount = MutexControlBlock::RecursiveLocksCount;
39 
41  using Type = MutexType;
42 
52  {
53  return std::numeric_limits<RecursiveLocksCount>::max();
54  }
55 
69  constexpr explicit Mutex(const Type type = Type::normal, const Protocol protocol = Protocol::none,
70  const uint8_t priorityCeiling = {}) :
71  MutexControlBlock{type, protocol, priorityCeiling}
72  {
73 
74  }
75 
88  constexpr explicit Mutex(const Protocol protocol, const uint8_t priorityCeiling = {}) :
89  Mutex{Type::normal, protocol, priorityCeiling}
90  {
91 
92  }
93 
106  ~Mutex() = default;
107 
129  int lock();
130 
151  int tryLock();
152 
180  int tryLockFor(TickClock::duration duration);
181 
203  template<typename Rep, typename Period>
204  int tryLockFor(const std::chrono::duration<Rep, Period> duration)
205  {
206  return tryLockFor(std::chrono::duration_cast<TickClock::duration>(duration));
207  }
208 
237  int tryLockUntil(TickClock::time_point timePoint);
238 
259  template<typename Duration>
260  int tryLockUntil(const std::chrono::time_point<TickClock, Duration> timePoint)
261  {
262  return tryLockUntil(std::chrono::time_point_cast<TickClock::duration>(timePoint));
263  }
264 
276  bool try_lock()
277  {
278  return tryLock() == 0;
279  }
280 
297  template<typename Rep, typename Period>
298  bool try_lock_for(const std::chrono::duration<Rep, Period> duration)
299  {
300  return tryLockFor(duration) == 0;
301  }
302 
318  template<typename Duration>
319  bool try_lock_until(const std::chrono::time_point<TickClock, Duration> timePoint)
320  {
321  return tryLockUntil(timePoint) == 0;
322  }
323 
342  int unlock();
343 
344  Mutex(const Mutex&) = delete;
345  Mutex(Mutex&&) = default;
346  const Mutex& operator=(const Mutex&) = delete;
347  Mutex& operator=(Mutex&&) = delete;
348 
349 private:
350 
366  int tryLockInternal();
367 };
368 
369 } // namespace distortos
370 
371 #endif // INCLUDE_DISTORTOS_MUTEX_HPP_
int tryLockFor(TickClock::duration duration)
Tries to lock the mutex for given duration of time.
Definition: Mutex.cpp:49
int lock()
Locks the mutex.
Definition: Mutex.cpp:30
uint16_t RecursiveLocksCount
type used for counting recursive locks
Definition: MutexControlBlock.hpp:40
MutexControlBlock class is a control block for Mutex.
Definition: MutexControlBlock.hpp:32
int tryLockUntil(TickClock::time_point timePoint)
Tries to lock the mutex until given time point.
Definition: Mutex.cpp:54
int tryLock()
Tries to lock the mutex.
Definition: Mutex.cpp:42
bool try_lock_until(const std::chrono::time_point< TickClock, Duration > timePoint)
Tries to lock the mutex until given time point.
Definition: Mutex.hpp:319
std::chrono::time_point< TickClock > time_point
basic time_point type of clock
Definition: TickClock.hpp:42
bool try_lock()
Tries to lock the mutex.
Definition: Mutex.hpp:276
normal mutex, similar to PTHREAD_MUTEX_NORMAL
MutexControlBlock class header.
~Mutex()=default
Mutex's destructor.
Top-level namespace of distortos project.
Definition: buttons.hpp:33
Mutex is the basic synchronization primitive.
Definition: Mutex.hpp:30
int unlock()
Unlocks the mutex.
Definition: Mutex.cpp:67
no protocol, similar to PTHREAD_PRIO_NONE
int tryLockFor(const std::chrono::duration< Rep, Period > duration)
Tries to lock the mutex for given duration of time.
Definition: Mutex.hpp:204
static constexpr RecursiveLocksCount getMaxRecursiveLocks()
Gets the maximum number of recursive locks possible before returning EAGAIN.
Definition: Mutex.hpp:51
MutexProtocol
mutex protocols
Definition: MutexProtocol.hpp:21
constexpr Mutex(const Type type=Type::normal, const Protocol protocol=Protocol::none, const uint8_t priorityCeiling={})
Mutex's constructor.
Definition: Mutex.hpp:69
std::chrono::duration< rep, period > duration
basic duration type of clock
Definition: TickClock.hpp:39
constexpr Mutex(const Protocol protocol, const uint8_t priorityCeiling={})
Mutex's constructor, overload for "normal" type.
Definition: Mutex.hpp:88
int tryLockUntil(const std::chrono::time_point< TickClock, Duration > timePoint)
Tries to lock the mutex until given time point.
Definition: Mutex.hpp:260
constexpr MutexControlBlock(const Type type, const Protocol protocol, const uint8_t priorityCeiling)
MutexControlBlock's constructor.
Definition: MutexControlBlock.hpp:90
bool try_lock_for(const std::chrono::duration< Rep, Period > duration)
Tries to lock the mutex for given duration of time.
Definition: Mutex.hpp:298
MutexType
type of mutex
Definition: MutexType.hpp:21
int tryLockInternal()
Internal version of tryLock().
Definition: Mutex.cpp:94