distortos  v0.4.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 
30 class Mutex
31 {
32 public:
33 
36 
38  using RecursiveLocksCount = uint16_t;
39 
41  enum class Type : uint8_t
42  {
44  normal,
48  recursive
49  };
50 
60  {
61  return std::numeric_limits<RecursiveLocksCount>::max();
62  }
63 
77  constexpr explicit Mutex(const Type type = Type::normal, const Protocol protocol = Protocol::none,
78  const uint8_t priorityCeiling = {}) :
79  controlBlock_{protocol, priorityCeiling},
81  type_{type}
82  {
83 
84  }
85 
107  int lock();
108 
129  int tryLock();
130 
158  int tryLockFor(TickClock::duration duration);
159 
181  template<typename Rep, typename Period>
182  int tryLockFor(const std::chrono::duration<Rep, Period> duration)
183  {
184  return tryLockFor(std::chrono::duration_cast<TickClock::duration>(duration));
185  }
186 
215  int tryLockUntil(TickClock::time_point timePoint);
216 
237  template<typename Duration>
238  int tryLockUntil(const std::chrono::time_point<TickClock, Duration> timePoint)
239  {
240  return tryLockUntil(std::chrono::time_point_cast<TickClock::duration>(timePoint));
241  }
242 
261  int unlock();
262 
263 private:
264 
280  int tryLockInternal();
281 
284 
287 
290 };
291 
292 } // namespace distortos
293 
294 #endif // INCLUDE_DISTORTOS_MUTEX_HPP_
int tryLockFor(TickClock::duration duration)
Tries to lock the mutex for given duration of time.
Definition: Mutex.cpp:49
normal mutex, similar to PTHREAD_MUTEX_NORMAL
int lock()
Locks the mutex.
Definition: Mutex.cpp:30
RecursiveLocksCount recursiveLocksCount_
number of recursive locks, used when mutex type is Recursive
Definition: Mutex.hpp:286
mutex with additional error checking, similar to PTHREAD_MUTEX_ERRORCHECK
MutexControlBlock class is a control block for Mutex.
Definition: MutexControlBlock.hpp:28
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
Type
type of mutex
Definition: Mutex.hpp:41
std::chrono::time_point< TickClock > time_point
basic time_point type of clock
Definition: TickClock.hpp:42
MutexControlBlock class header.
Top-level namespace of distortos project.
Mutex is the basic synchronization primitive.
Definition: Mutex.hpp:30
Protocol
mutex protocols
Definition: MutexControlBlock.hpp:33
int unlock()
Unlocks the mutex.
Definition: Mutex.cpp:67
int tryLockFor(const std::chrono::duration< Rep, Period > duration)
Definition: Mutex.hpp:182
static constexpr RecursiveLocksCount getMaxRecursiveLocks()
Gets the maximum number of recursive locks possible before returning EAGAIN.
Definition: Mutex.hpp:59
Type type_
type of mutex
Definition: Mutex.hpp:289
constexpr Mutex(const Type type=Type::normal, const Protocol protocol=Protocol::none, const uint8_t priorityCeiling={})
Mutex constructor.
Definition: Mutex.hpp:77
no protocol, similar to PTHREAD_PRIO_NONE
std::chrono::duration< rep, period > duration
basic duration type of clock
Definition: TickClock.hpp:39
internal::MutexControlBlock controlBlock_
instance of control block
Definition: Mutex.hpp:283
int tryLockUntil(const std::chrono::time_point< TickClock, Duration > timePoint)
Tries to lock the mutex until given time point.
Definition: Mutex.hpp:238
uint16_t RecursiveLocksCount
type used for counting recursive locks
Definition: Mutex.hpp:38
int tryLockInternal()
Internal version of tryLock().
Definition: Mutex.cpp:94
recursive mutex, similar to PTHREAD_MUTEX_RECURSIVE