Open Model Railroad Network (OpenMRN)
Loading...
Searching...
No Matches
FixedQueue.hxx
Go to the documentation of this file.
1
34#ifndef _FREERTOS_DRIVERS_COMMON_FIXEDQUEUE_HXX_
35#define _FREERTOS_DRIVERS_COMMON_FIXEDQUEUE_HXX_
36
37#include <algorithm>
38#include <cstdint>
39
40#include "Devtab.hxx"
42
55template<class T, uint8_t SIZE> class FixedQueue {
56public:
58 : rdIndex_(0)
59 , wrIndex_(0)
60 , count_(0)
61 {
62 }
63
65 bool empty() { return size() == 0; }
67 bool full() { return size() >= SIZE; }
69 size_t size() { return __atomic_load_n(&count_, __ATOMIC_SEQ_CST); }
70
72 T& front() {
73 HASSERT(!empty());
74 return storage_[rdIndex_];
75 }
76
79 HASSERT(!empty());
80 if (++rdIndex_ >= SIZE) rdIndex_ = 0;
81 __atomic_fetch_add(&count_, -1, __ATOMIC_SEQ_CST);
82 }
83
85 T& back() {
86 HASSERT(!full());
87 return storage_[wrIndex_];
88 }
89
92 HASSERT(!full());
93 if (++wrIndex_ >= SIZE) wrIndex_ = 0;
94 __atomic_fetch_add(&count_, 1, __ATOMIC_SEQ_CST);
95 }
96
107 if (++wrIndex_ >= SIZE) wrIndex_ = 0;
108 }
109
112 if (full()) return false;
113 auto new_index = wrIndex_;
114 if (++new_index >= SIZE) new_index = 0;
115 return new_index != rdIndex_;
116 }
117
119 void commit_back() {
120 HASSERT(count_ <= SIZE);
121 __atomic_fetch_add(&count_, 1, __ATOMIC_SEQ_CST);
122 }
123
124private:
126 T storage_[SIZE];
129 uint8_t rdIndex_;
131 uint8_t wrIndex_;
133 volatile uint8_t count_;
134};
135
136#endif // _FREERTOS_DRIVERS_COMMON_FIXEDQUEUE_HXX_
This structure is safe to use from an interrupt context and a regular context at the same time,...
bool empty()
void increment_front()
Removes the head of the FIFO from the queue.
T storage_[SIZE]
Payload of elements stored.
uint8_t rdIndex_
The index of the element to return next upon a read.
bool has_noncommit_space()
void increment_back()
Commits the element at back() into the queue.
void commit_back()
Commits the oldest entry reserved by noncommit_back.
size_t size()
T & front()
Returns the head of the FIFO (next element to read).
T & back()
Returns the space to write the next element to.
uint8_t wrIndex_
The index of the element where to write the next input to.
void noncommit_back()
Increments the back pointer without committing the entry into the queue.
bool full()
volatile uint8_t count_
How many elements are there in the queue.
#define HASSERT(x)
Checks that the value of expression x is true, else terminates the current process.
Definition macros.h:138