@@ -123,19 +123,35 @@ class circular_queue
123
123
}
124
124
125
125
/* !
126
- @brief Peek at the next element pop returns without removing it from the queue.
127
- @return An rvalue copy of the next element that can be popped, or a default
128
- value of type T if the queue is empty .
126
+ @brief Peek at the next element pop will return without removing it from the queue.
127
+ @return An rvalue copy of the next element that can be popped. If the queue is empty,
128
+ return an rvalue copy of the element that is pending the next push .
129
129
*/
130
130
T peek () const
131
131
{
132
- const auto outPos = m_outPos.load (std::memory_order_acquire);
132
+ const auto outPos = m_outPos.load (std::memory_order_relaxed);
133
+ std::atomic_thread_fence (std::memory_order_acquire);
134
+ return m_buffer[outPos];
135
+ }
136
+
137
+ /* !
138
+ @brief Peek at the next pending input value.
139
+ @return A reference to the next element that can be pushed.
140
+ */
141
+ T& IRAM_ATTR pushpeek ()
142
+ {
133
143
const auto inPos = m_inPos.load (std::memory_order_relaxed);
134
144
std::atomic_thread_fence (std::memory_order_acquire);
135
- if (inPos == outPos) return defaultValue;
136
- else return m_buffer[outPos];
145
+ return m_buffer[inPos];
137
146
}
138
147
148
+ /* !
149
+ @brief Release the next pending input value, accessible by pushpeek(), into the queue.
150
+ @return true if the queue accepted the value, false if the queue
151
+ was full.
152
+ */
153
+ bool IRAM_ATTR push ();
154
+
139
155
/* !
140
156
@brief Move the rvalue parameter into the queue.
141
157
@return true if the queue accepted the value, false if the queue
@@ -212,6 +228,21 @@ bool circular_queue<T>::capacity(const size_t cap)
212
228
return true ;
213
229
}
214
230
231
+ template < typename T >
232
+ bool IRAM_ATTR circular_queue<T>::push()
233
+ {
234
+ const auto inPos = m_inPos.load (std::memory_order_acquire);
235
+ const unsigned next = (inPos + 1 ) % m_bufSize;
236
+ if (next == m_outPos.load (std::memory_order_relaxed)) {
237
+ return false ;
238
+ }
239
+
240
+ std::atomic_thread_fence (std::memory_order_acquire);
241
+
242
+ m_inPos.store (next, std::memory_order_release);
243
+ return true ;
244
+ }
245
+
215
246
template < typename T >
216
247
bool IRAM_ATTR circular_queue<T>::push(T&& val)
217
248
{
0 commit comments