distortos  v0.6.0
object-oriented C++ RTOS for microcontrollers
MutexControlBlock.hpp
Go to the documentation of this file.
1 
12 #ifndef INCLUDE_DISTORTOS_INTERNAL_SYNCHRONIZATION_MUTEXCONTROLBLOCK_HPP_
13 #define INCLUDE_DISTORTOS_INTERNAL_SYNCHRONIZATION_MUTEXCONTROLBLOCK_HPP_
14 
16 
18 
20 #include "distortos/MutexType.hpp"
21 #include "distortos/TickClock.hpp"
22 
23 #include <climits>
24 
25 namespace distortos
26 {
27 
28 namespace internal
29 {
30 
33 {
34 public:
35 
38 
40  using RecursiveLocksCount = uint16_t;
41 
43  using Type = MutexType;
44 
57  uint8_t getBoostedPriority() const;
58 
64  {
65  return owner_;
66  }
67 
69  constexpr static uint8_t typeShift {0};
70 
72  constexpr static uint8_t typeWidth {CHAR_BIT / 2};
73 
75  constexpr static uint8_t protocolShift {typeShift + typeWidth};
76 
78  constexpr static uint8_t protocolWidth {CHAR_BIT / 2};
79 
80 protected:
81 
90  constexpr MutexControlBlock(const Type type, const Protocol protocol, const uint8_t priorityCeiling) :
91  MutexListNode{},
92  blockedList_{},
93  owner_{},
95  priorityCeiling_{priorityCeiling},
96  typeProtocol_{static_cast<uint8_t>(static_cast<uint8_t>(type) << typeShift |
97  static_cast<uint8_t>(protocol) << protocolShift)}
98  {
99 
100  }
101 
109  int doBlock();
110 
120  int doBlockUntil(TickClock::time_point timePoint);
121 
128  void doLock();
129 
138  void doUnlockOrTransferLock();
139 
144  uint8_t getPriorityCeiling() const
145  {
146  return priorityCeiling_;
147  }
148 
154  {
155  return static_cast<Protocol>((typeProtocol_ >> protocolShift) & ((1 << protocolWidth) - 1));
156  }
157 
163  {
164  return recursiveLocksCount_;
165  }
166 
171  Type getType() const
172  {
173  return static_cast<Type>((typeProtocol_ >> typeShift) & ((1 << typeWidth) - 1));
174  }
175 
176 private:
177 
187  void beforeBlock() const;
188 
195  void doTransferLock();
196 
203  void doUnlock();
204 
207 
210 
213 
216 
218  uint8_t typeProtocol_;
219 };
220 
221 } // namespace internal
222 
223 } // namespace distortos
224 
225 #endif // INCLUDE_DISTORTOS_INTERNAL_SYNCHRONIZATION_MUTEXCONTROLBLOCK_HPP_
uint8_t getBoostedPriority() const
Gets "boosted priority" of the mutex.
Definition: MutexControlBlock.cpp:81
ThreadList blockedList_
ThreadControlBlock objects blocked on mutex.
Definition: MutexControlBlock.hpp:206
uint8_t priorityCeiling_
priority ceiling of mutex, valid only when protocol_ == Protocol::priorityProtect ...
Definition: MutexControlBlock.hpp:215
uint8_t typeProtocol_
type of mutex and its protocol
Definition: MutexControlBlock.hpp:218
ThreadList class header.
Protocol getProtocol() const
Definition: MutexControlBlock.hpp:153
ThreadControlBlock class is a simple description of a Thread.
Definition: ThreadControlBlock.hpp:39
static constexpr uint8_t protocolWidth
width of "protocol" subfield, bits
Definition: MutexControlBlock.hpp:78
uint16_t RecursiveLocksCount
type used for counting recursive locks
Definition: MutexControlBlock.hpp:40
MutexListNode class is a base for MutexControlBlock that serves as a node in intrusive list of mutexe...
Definition: MutexListNode.hpp:31
ThreadControlBlock * getOwner() const
Definition: MutexControlBlock.hpp:63
void doUnlockOrTransferLock()
Performs unlocking or transfer of lock from current owner to next thread on the list.
Definition: MutexControlBlock.cpp:132
void beforeBlock() const
Performs any actions required before actually blocking on the mutex.
Definition: MutexControlBlock.cpp:156
Type getType() const
Definition: MutexControlBlock.hpp:171
MutexControlBlock class is a control block for Mutex.
Definition: MutexControlBlock.hpp:32
std::chrono::time_point< TickClock > time_point
basic time_point type of clock
Definition: TickClock.hpp:42
TickClock class header.
RecursiveLocksCount recursiveLocksCount_
number of recursive locks, used when mutex type is recursive
Definition: MutexControlBlock.hpp:212
static constexpr uint8_t typeWidth
width of "type" subfield, bits
Definition: MutexControlBlock.hpp:72
MutexListNode class header.
ThreadControlBlock * owner_
owner of the mutex
Definition: MutexControlBlock.hpp:209
void doUnlock()
Performs actual unlocking of previously locked mutex.
Definition: MutexControlBlock.cpp:183
void doLock()
Performs actual locking of previously unlocked mutex.
Definition: MutexControlBlock.cpp:118
Top-level namespace of distortos project.
MutexType enum class header.
sorted intrusive list of threads (thread control blocks)
Definition: ThreadList.hpp:55
void doTransferLock()
Performs transfer of lock from current owner to next thread on the list.
Definition: MutexControlBlock.cpp:169
MutexProtocol
mutex protocols
Definition: MutexProtocol.hpp:21
uint8_t getPriorityCeiling() const
Definition: MutexControlBlock.hpp:144
RecursiveLocksCount & getRecursiveLocksCount()
Definition: MutexControlBlock.hpp:162
int doBlock()
Blocks current thread, transferring it to blockedList_.
Definition: MutexControlBlock.cpp:100
constexpr MutexControlBlock(const Type type, const Protocol protocol, const uint8_t priorityCeiling)
MutexControlBlock&#39;s constructor.
Definition: MutexControlBlock.hpp:90
static constexpr uint8_t protocolShift
shift of "protocol" subfield, bits
Definition: MutexControlBlock.hpp:75
int doBlockUntil(TickClock::time_point timePoint)
Blocks current thread with timeout, transferring it to blockedList_.
Definition: MutexControlBlock.cpp:109
MutexType
type of mutex
Definition: MutexType.hpp:21
static constexpr uint8_t typeShift
shift of "type" subfield, bits
Definition: MutexControlBlock.hpp:69
MutexProtocol enum class header.