distortos  v0.7.0
object-oriented C++ RTOS for microcontrollers
FifoQueueBase.hpp
Go to the documentation of this file.
1 
12 #ifndef INCLUDE_DISTORTOS_INTERNAL_SYNCHRONIZATION_FIFOQUEUEBASE_HPP_
13 #define INCLUDE_DISTORTOS_INTERNAL_SYNCHRONIZATION_FIFOQUEUEBASE_HPP_
14 
15 #include "distortos/Semaphore.hpp"
16 
19 
20 #include <memory>
21 
22 namespace distortos
23 {
24 
25 namespace internal
26 {
27 
30 {
31 public:
32 
34  using StorageUniquePointer = std::unique_ptr<void, void(&)(void*)>;
35 
45  FifoQueueBase(StorageUniquePointer&& storageUniquePointer, size_t elementSize, size_t maxElements);
46 
52 
57  size_t getCapacity() const
58  {
59  return popSemaphore_.getMaxValue();
60  }
61 
66  size_t getElementSize() const
67  {
68  return elementSize_;
69  }
70 
83  int pop(const SemaphoreFunctor& waitSemaphoreFunctor, const QueueFunctor& functor)
84  {
85  return popPush(waitSemaphoreFunctor, functor, popSemaphore_, pushSemaphore_, readPosition_);
86  }
87 
100  int push(const SemaphoreFunctor& waitSemaphoreFunctor, const QueueFunctor& functor)
101  {
102  return popPush(waitSemaphoreFunctor, functor, pushSemaphore_, popSemaphore_, writePosition_);
103  }
104 
105 private:
106 
125  int popPush(const SemaphoreFunctor& waitSemaphoreFunctor, const QueueFunctor& functor, Semaphore& waitSemaphore,
126  Semaphore& postSemaphore, void*& storage);
127 
130 
133 
136 
138  const void* const storageEnd_;
139 
142 
145 
147  const size_t elementSize_;
148 };
149 
150 } // namespace internal
151 
152 } // namespace distortos
153 
154 #endif // INCLUDE_DISTORTOS_INTERNAL_SYNCHRONIZATION_FIFOQUEUEBASE_HPP_
void * writePosition_
pointer to first free slot available for writing
Definition: FifoQueueBase.hpp:144
Semaphore is the basic synchronization primitive.
Definition: Semaphore.hpp:30
FifoQueueBase(StorageUniquePointer &&storageUniquePointer, size_t elementSize, size_t maxElements)
FifoQueueBase's constructor.
Definition: FifoQueueBase.cpp:26
const StorageUniquePointer storageUniquePointer_
storage for queue elements
Definition: FifoQueueBase.hpp:135
Semaphore popSemaphore_
semaphore guarding access to "pop" functions - its value is equal to the number of available elements
Definition: FifoQueueBase.hpp:129
Semaphore class header.
int pop(const SemaphoreFunctor &waitSemaphoreFunctor, const QueueFunctor &functor)
Implementation of pop() using type-erased functor.
Definition: FifoQueueBase.hpp:83
~FifoQueueBase()
FifoQueueBase's destructor.
Definition: FifoQueueBase.cpp:39
void * readPosition_
pointer to first element available for reading
Definition: FifoQueueBase.hpp:141
const void *const storageEnd_
pointer to past-the-last element of storage for queue elements
Definition: FifoQueueBase.hpp:138
Value getMaxValue() const
Definition: Semaphore.hpp:71
std::unique_ptr< void, void(&)(void *)> StorageUniquePointer
unique_ptr (with deleter) to storage
Definition: FifoQueueBase.hpp:34
FifoQueueBase class implements basic functionality of FifoQueue template class.
Definition: FifoQueueBase.hpp:29
Top-level namespace of distortos project.
Definition: buttons.hpp:33
QueueFunctor class header.
size_t getElementSize() const
Definition: FifoQueueBase.hpp:66
SemaphoreFunctor is a type-erased interface for functors which execute some action on semaphore (wait...
Definition: SemaphoreFunctor.hpp:34
size_t getCapacity() const
Definition: FifoQueueBase.hpp:57
int popPush(const SemaphoreFunctor &waitSemaphoreFunctor, const QueueFunctor &functor, Semaphore &waitSemaphore, Semaphore &postSemaphore, void *&storage)
Implementation of pop() and push() using type-erased functor.
Definition: FifoQueueBase.cpp:48
QueueFunctor is a type-erased interface for functors which execute some action on queue's storage (li...
Definition: QueueFunctor.hpp:31
SemaphoreFunctor class header.
int push(const SemaphoreFunctor &waitSemaphoreFunctor, const QueueFunctor &functor)
Implementation of push() using type-erased functor.
Definition: FifoQueueBase.hpp:100
Semaphore pushSemaphore_
semaphore guarding access to "push" functions - its value is equal to the number of free slots
Definition: FifoQueueBase.hpp:132
const size_t elementSize_
size of single queue element, bytes
Definition: FifoQueueBase.hpp:147