distortos  v0.6.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 
283  int unlock();
284 
285  Mutex(const Mutex&) = delete;
286  Mutex(Mutex&&) = default;
287  const Mutex& operator=(const Mutex&) = delete;
288  Mutex& operator=(Mutex&&) = delete;
289 
290 private:
291 
307  int tryLockInternal();
308 };
309 
310 } // namespace distortos
311 
312 #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
std::chrono::time_point< TickClock > time_point
basic time_point type of clock
Definition: TickClock.hpp:42
normal mutex, similar to PTHREAD_MUTEX_NORMAL
MutexControlBlock class header.
~Mutex()=default
Mutex&#39;s destructor.
Top-level namespace of distortos project.
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)
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&#39;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&#39;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&#39;s constructor.
Definition: MutexControlBlock.hpp:90
MutexType
type of mutex
Definition: MutexType.hpp:21
int tryLockInternal()
Internal version of tryLock().
Definition: Mutex.cpp:94