distortos  v0.7.0
object-oriented C++ RTOS for microcontrollers
MessageQueueBase.hpp
Go to the documentation of this file.
1 
12 #ifndef INCLUDE_DISTORTOS_INTERNAL_SYNCHRONIZATION_MESSAGEQUEUEBASE_HPP_
13 #define INCLUDE_DISTORTOS_INTERNAL_SYNCHRONIZATION_MESSAGEQUEUEBASE_HPP_
14 
15 #include "distortos/Semaphore.hpp"
16 
19 
21 
22 #include <memory>
23 
24 namespace distortos
25 {
26 
27 namespace internal
28 {
29 
32 {
33 public:
34 
36  struct Entry
37  {
45  constexpr Entry(const uint8_t priorityy, void* const storagee) :
46  node{},
47  priority{priorityy},
48  storage{storagee}
49  {
50 
51  }
52 
55 
57  uint8_t priority;
58 
60  void* storage;
61  };
62 
64  using EntryStorage = typename std::aligned_storage<sizeof(Entry), alignof(Entry)>::type;
65 
67  using EntryStorageUniquePointer = std::unique_ptr<EntryStorage[], void(&)(EntryStorage*)>;
68 
75  template<typename T>
76  using ValueStorage = typename std::aligned_storage<sizeof(T), alignof(T)>::type;
77 
79  using ValueStorageUniquePointer = std::unique_ptr<void, void(&)(void*)>;
80 
83  {
88  constexpr DescendingPriority()
89  {
90 
91  }
92 
102  bool operator()(const Entry& left, const Entry& right) const
103  {
104  return left.priority < right.priority;
105  }
106  };
107 
110 
113 
122  class InternalFunctor : public estd::TypeErasedFunctor<void(EntryList&, FreeEntryList&)>
123  {
124 
125  };
126 
138  MessageQueueBase(EntryStorageUniquePointer&& entryStorageUniquePointer,
139  ValueStorageUniquePointer&& valueStorageUniquePointer, size_t elementSize, size_t maxElements);
140 
146 
151  size_t getCapacity() const
152  {
153  return popSemaphore_.getMaxValue();
154  }
155 
169  int pop(const SemaphoreFunctor& waitSemaphoreFunctor, uint8_t& priority, const QueueFunctor& functor);
170 
184  int push(const SemaphoreFunctor& waitSemaphoreFunctor, uint8_t priority, const QueueFunctor& functor);
185 
186 private:
187 
204  int popPush(const SemaphoreFunctor& waitSemaphoreFunctor, const InternalFunctor& internalFunctor,
205  Semaphore& waitSemaphore, Semaphore& postSemaphore);
206 
209 
212 
215 
218 
221 
224 };
225 
226 } // namespace internal
227 
228 } // namespace distortos
229 
230 #endif // INCLUDE_DISTORTOS_INTERNAL_SYNCHRONIZATION_MESSAGEQUEUEBASE_HPP_
int pop(const SemaphoreFunctor &waitSemaphoreFunctor, uint8_t &priority, const QueueFunctor &functor)
Implementation of pop() using type-erased functor.
Definition: MessageQueueBase.cpp:157
constexpr DescendingPriority()
DescendingPriority's constructor.
Definition: MessageQueueBase.hpp:88
Semaphore is the basic synchronization primitive.
Definition: Semaphore.hpp:30
SortedIntrusiveForwardList template class header.
const EntryStorageUniquePointer entryStorageUniquePointer_
storage for queue entries
Definition: MessageQueueBase.hpp:214
Definition: TypeErasedFunctor.hpp:19
IntrusiveForwardList class is an intrusive linear singly linked list.
Definition: IntrusiveForwardList.hpp:855
FreeEntryList freeEntryList_
list of "free" entries
Definition: MessageQueueBase.hpp:223
void * storage
storage for the entry
Definition: MessageQueueBase.hpp:60
int push(const SemaphoreFunctor &waitSemaphoreFunctor, uint8_t priority, const QueueFunctor &functor)
Implementation of push() using type-erased functor.
Definition: MessageQueueBase.cpp:163
MessageQueueBase class implements basic functionality of MessageQueue template class.
Definition: MessageQueueBase.hpp:31
size_t getCapacity() const
Definition: MessageQueueBase.hpp:151
IntrusiveForwardListNode class is the node that is needed for the object to be linked in IntrusiveFor...
Definition: IntrusiveForwardList.hpp:38
Semaphore class header.
estd::IntrusiveForwardListNode node
node for intrusive forward list
Definition: MessageQueueBase.hpp:54
IntrusiveForwardList< Entry, NodePointer, Entry > UnsortedIntrusiveForwardList
unsorted intrusive forward list used internally
Definition: SortedIntrusiveForwardList.hpp:45
constexpr Entry(const uint8_t priorityy, void *const storagee)
Entry's constructor.
Definition: MessageQueueBase.hpp:45
const ValueStorageUniquePointer valueStorageUniquePointer_
storage for queue elements
Definition: MessageQueueBase.hpp:217
EntryList entryList_
list of available entries, sorted in descending order of priority
Definition: MessageQueueBase.hpp:220
Value getMaxValue() const
Definition: Semaphore.hpp:71
int popPush(const SemaphoreFunctor &waitSemaphoreFunctor, const InternalFunctor &internalFunctor, Semaphore &waitSemaphore, Semaphore &postSemaphore)
Implementation of pop() and push() using type-erased internal functor.
Definition: MessageQueueBase.cpp:174
MessageQueueBase(EntryStorageUniquePointer &&entryStorageUniquePointer, ValueStorageUniquePointer &&valueStorageUniquePointer, size_t elementSize, size_t maxElements)
MessageQueueBase's constructor.
Definition: MessageQueueBase.cpp:135
Top-level namespace of distortos project.
Definition: buttons.hpp:33
~MessageQueueBase()
MessageQueueBase's destructor.
Definition: MessageQueueBase.cpp:152
QueueFunctor class header.
typename std::aligned_storage< sizeof(T), alignof(T)>::type ValueStorage
Definition: MessageQueueBase.hpp:76
Semaphore popSemaphore_
semaphore guarding access to "pop" functions - its value is equal to the number of available elements
Definition: MessageQueueBase.hpp:208
SemaphoreFunctor is a type-erased interface for functors which execute some action on semaphore (wait...
Definition: SemaphoreFunctor.hpp:34
std::unique_ptr< EntryStorage[], void(&)(EntryStorage *)> EntryStorageUniquePointer
unique_ptr (with deleter) to EntryStorage[]
Definition: MessageQueueBase.hpp:67
functor which gives descending priority order of elements on the list
Definition: MessageQueueBase.hpp:82
typename std::aligned_storage< sizeof(Entry), alignof(Entry)>::type EntryStorage
type of uninitialized storage for Entry
Definition: MessageQueueBase.hpp:64
Semaphore pushSemaphore_
semaphore guarding access to "push" functions - its value is equal to the number of free slots
Definition: MessageQueueBase.hpp:211
uint8_t priority
priority of the entry
Definition: MessageQueueBase.hpp:57
QueueFunctor is a type-erased interface for functors which execute some action on queue's storage (li...
Definition: QueueFunctor.hpp:31
SemaphoreFunctor class header.
std::unique_ptr< void, void(&)(void *)> ValueStorageUniquePointer
unique_ptr (with deleter) to storage
Definition: MessageQueueBase.hpp:79
InternalFunctor is a type-erased interface for functors which execute common code of pop() and push()...
Definition: MessageQueueBase.hpp:122
bool operator()(const Entry &left, const Entry &right) const
DescendingPriority's function call operator.
Definition: MessageQueueBase.hpp:102
entry in the MessageQueueBase
Definition: MessageQueueBase.hpp:36