Embedded Template Library 1.0
Loading...
Searching...
No Matches
queue_spsc_atomic.h
Go to the documentation of this file.
1
2
3/******************************************************************************
4The MIT License(MIT)
5
6Embedded Template Library.
7https://github.com/ETLCPP/etl
8https://www.etlcpp.com
9
10Copyright(c) 2018 John Wellbelove
11
12Permission is hereby granted, free of charge, to any person obtaining a copy
13of this software and associated documentation files(the "Software"), to deal
14in the Software without restriction, including without limitation the rights
15to use, copy, modify, merge, publish, distribute, sublicense, and / or sell
16copies of the Software, and to permit persons to whom the Software is
17furnished to do so, subject to the following conditions :
18
19The above copyright notice and this permission notice shall be included in all
20copies or substantial portions of the Software.
21
22THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE
25AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
27OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
28SOFTWARE.
29******************************************************************************/
30
31#ifndef ETL_SPSC_QUEUE_ATOMIC_INCLUDED
32#define ETL_SPSC_QUEUE_ATOMIC_INCLUDED
33
34#include "platform.h"
35#include "alignment.h"
36#include "atomic.h"
37#include "integral_limits.h"
38#include "memory_model.h"
39#include "parameter_type.h"
40#include "placement_new.h"
41#include "utility.h"
42
43#include <stddef.h>
44
45#if ETL_HAS_ATOMIC
46
47namespace etl
48{
49 template <size_t Memory_Model = etl::memory_model::MEMORY_MODEL_LARGE>
50 class queue_spsc_atomic_base
51 {
52 public:
53
55 typedef typename etl::size_type_lookup<Memory_Model>::type size_type;
56
57 //*************************************************************************
61 //*************************************************************************
62 bool empty() const
63 {
64 return read.load(etl::memory_order_acquire) == write.load(etl::memory_order_acquire);
65 }
66
67 //*************************************************************************
71 //*************************************************************************
72 bool full() const
73 {
74 size_type next_index = get_next_index(write.load(etl::memory_order_acquire), Reserved);
75
76 return (next_index == read.load(etl::memory_order_acquire));
77 }
78
79 //*************************************************************************
82 //*************************************************************************
83 size_type size() const
84 {
85 size_type write_index = write.load(etl::memory_order_acquire);
86 size_type read_index = read.load(etl::memory_order_acquire);
87
88 size_type n;
89
90 if (write_index >= read_index)
91 {
92 n = write_index - read_index;
93 }
94 else
95 {
96 n = Reserved - read_index + write_index;
97 }
98
99 return n;
100 }
101
102 //*************************************************************************
105 //*************************************************************************
106 size_type available() const
107 {
108 return Reserved - size() - 1;
109 }
110
111 //*************************************************************************
113 //*************************************************************************
114 size_type capacity() const
115 {
116 return Reserved - 1;
117 }
118
119 //*************************************************************************
121 //*************************************************************************
122 size_type max_size() const
123 {
124 return Reserved - 1;
125 }
126
127 protected:
128
129 queue_spsc_atomic_base(size_type reserved_)
130 : write(0)
131 , read(0)
132 , Reserved(reserved_)
133 {
134 }
135
136 //*************************************************************************
138 //*************************************************************************
139 static size_type get_next_index(size_type index, size_type maximum)
140 {
141 ++index;
142
143 if (index == maximum) ETL_UNLIKELY
144 {
145 index = 0;
146 }
147
148 return index;
149 }
150
151 etl::atomic<size_type> write;
152 etl::atomic<size_type> read;
153 const size_type Reserved;
154
155 private:
156
157 //*************************************************************************
159 //*************************************************************************
160 #if defined(ETL_POLYMORPHIC_SPSC_QUEUE_ATOMIC) || defined(ETL_POLYMORPHIC_CONTAINERS)
161
162 public:
163
164 virtual ~queue_spsc_atomic_base() {}
165 #else
166
167 protected:
168
169 ~queue_spsc_atomic_base() {}
170 #endif
171 };
172
173 //***************************************************************************
183 //***************************************************************************
184 template <typename T, const size_t Memory_Model = etl::memory_model::MEMORY_MODEL_LARGE>
185 class iqueue_spsc_atomic : public queue_spsc_atomic_base<Memory_Model>
186 {
187 private:
188
189 typedef typename etl::queue_spsc_atomic_base<Memory_Model> base_t;
190
191 public:
192
193 typedef T value_type;
194 typedef T& reference;
195 typedef const T& const_reference;
196 #if ETL_USING_CPP11
197 typedef T&& rvalue_reference;
198 #endif
199 typedef typename base_t::size_type size_type;
200
201 using base_t::get_next_index;
202 using base_t::read;
203 using base_t::Reserved;
204 using base_t::write;
205
206 //*************************************************************************
208 //*************************************************************************
209 bool push(const_reference value)
210 {
211 size_type write_index = write.load(etl::memory_order_relaxed);
212 size_type next_index = get_next_index(write_index, Reserved);
213
214 if (next_index != read.load(etl::memory_order_acquire))
215 {
216 ::new (&p_buffer[write_index]) T(value);
217
218 write.store(next_index, etl::memory_order_release);
219
220 return true;
221 }
222
223 // Queue is full.
224 return false;
225 }
226
227 #if ETL_USING_CPP11 && ETL_NOT_USING_STLPORT && !defined(ETL_QUEUE_ATOMIC_FORCE_CPP03_IMPLEMENTATION)
228 //*************************************************************************
230 //*************************************************************************
231 bool push(rvalue_reference value)
232 {
233 size_type write_index = write.load(etl::memory_order_relaxed);
234 size_type next_index = get_next_index(write_index, Reserved);
235
236 if (next_index != read.load(etl::memory_order_acquire))
237 {
238 ::new (&p_buffer[write_index]) T(etl::move(value));
239
240 write.store(next_index, etl::memory_order_release);
241
242 return true;
243 }
244
245 // Queue is full.
246 return false;
247 }
248 #endif
249
250 #if ETL_USING_CPP11 && ETL_NOT_USING_STLPORT && !defined(ETL_QUEUE_ATOMIC_FORCE_CPP03_IMPLEMENTATION)
251 //*************************************************************************
255 //*************************************************************************
256 template <typename... Args>
257 bool emplace(Args&&... args)
258 {
259 size_type write_index = write.load(etl::memory_order_relaxed);
260 size_type next_index = get_next_index(write_index, Reserved);
261
262 if (next_index != read.load(etl::memory_order_acquire))
263 {
264 ::new (&p_buffer[write_index]) T(etl::forward<Args>(args)...);
265
266 write.store(next_index, etl::memory_order_release);
267
268 return true;
269 }
270
271 // Queue is full.
272 return false;
273 }
274 #else
275 //*************************************************************************
279 //*************************************************************************
280 bool emplace()
281 {
282 size_type write_index = write.load(etl::memory_order_relaxed);
283 size_type next_index = get_next_index(write_index, Reserved);
284
285 if (next_index != read.load(etl::memory_order_acquire))
286 {
287 ::new (&p_buffer[write_index]) T();
288
289 write.store(next_index, etl::memory_order_release);
290
291 return true;
292 }
293
294 // Queue is full.
295 return false;
296 }
297
298 //*************************************************************************
302 //*************************************************************************
303 template <typename T1>
304 bool emplace(const T1& value1)
305 {
306 size_type write_index = write.load(etl::memory_order_relaxed);
307 size_type next_index = get_next_index(write_index, Reserved);
308
309 if (next_index != read.load(etl::memory_order_acquire))
310 {
311 ::new (&p_buffer[write_index]) T(value1);
312
313 write.store(next_index, etl::memory_order_release);
314
315 return true;
316 }
317
318 // Queue is full.
319 return false;
320 }
321
322 //*************************************************************************
326 //*************************************************************************
327 template <typename T1, typename T2>
328 bool emplace(const T1& value1, const T2& value2)
329 {
330 size_type write_index = write.load(etl::memory_order_relaxed);
331 size_type next_index = get_next_index(write_index, Reserved);
332
333 if (next_index != read.load(etl::memory_order_acquire))
334 {
335 ::new (&p_buffer[write_index]) T(value1, value2);
336
337 write.store(next_index, etl::memory_order_release);
338
339 return true;
340 }
341
342 // Queue is full.
343 return false;
344 }
345
346 //*************************************************************************
350 //*************************************************************************
351 template <typename T1, typename T2, typename T3>
352 bool emplace(const T1& value1, const T2& value2, const T3& value3)
353 {
354 size_type write_index = write.load(etl::memory_order_relaxed);
355 size_type next_index = get_next_index(write_index, Reserved);
356
357 if (next_index != read.load(etl::memory_order_acquire))
358 {
359 ::new (&p_buffer[write_index]) T(value1, value2, value3);
360
361 write.store(next_index, etl::memory_order_release);
362
363 return true;
364 }
365
366 // Queue is full.
367 return false;
368 }
369
370 //*************************************************************************
374 //*************************************************************************
375 template <typename T1, typename T2, typename T3, typename T4>
376 bool emplace(const T1& value1, const T2& value2, const T3& value3, const T4& value4)
377 {
378 size_type write_index = write.load(etl::memory_order_relaxed);
379 size_type next_index = get_next_index(write_index, Reserved);
380
381 if (next_index != read.load(etl::memory_order_acquire))
382 {
383 ::new (&p_buffer[write_index]) T(value1, value2, value3, value4);
384
385 write.store(next_index, etl::memory_order_release);
386
387 return true;
388 }
389
390 // Queue is full.
391 return false;
392 }
393 #endif
394
395 //*************************************************************************
397 //*************************************************************************
398 bool front(reference value)
399 {
400 size_type read_index = read.load(etl::memory_order_relaxed);
401
402 if (read_index == write.load(etl::memory_order_acquire))
403 {
404 // Queue is empty
405 return false;
406 }
407
408 value = p_buffer[read_index];
409
410 return true;
411 }
412
413 //*************************************************************************
415 //*************************************************************************
416 bool pop(reference value)
417 {
418 size_type read_index = read.load(etl::memory_order_relaxed);
419
420 if (read_index == write.load(etl::memory_order_acquire))
421 {
422 // Queue is empty
423 return false;
424 }
425
426 size_type next_index = get_next_index(read_index, Reserved);
427
428 #if ETL_USING_CPP11 && ETL_NOT_USING_STLPORT && !defined(ETL_QUEUE_LOCKABLE_FORCE_CPP03_IMPLEMENTATION)
429 value = etl::move(p_buffer[read_index]);
430 #else
431 value = p_buffer[read_index];
432 #endif
433
434 p_buffer[read_index].~T();
435
436 read.store(next_index, etl::memory_order_release);
437
438 return true;
439 }
440
441 //*************************************************************************
443 //*************************************************************************
444 bool pop()
445 {
446 size_type read_index = read.load(etl::memory_order_relaxed);
447
448 if (read_index == write.load(etl::memory_order_acquire))
449 {
450 // Queue is empty
451 return false;
452 }
453
454 size_type next_index = get_next_index(read_index, Reserved);
455
456 p_buffer[read_index].~T();
457
458 read.store(next_index, etl::memory_order_release);
459
460 return true;
461 }
462
463 //*************************************************************************
465 //*************************************************************************
466 reference front()
467 {
468 size_type read_index = read.load(etl::memory_order_relaxed);
469
470 return p_buffer[read_index];
471 }
472
473 //*************************************************************************
475 //*************************************************************************
476 const_reference front() const
477 {
478 size_type read_index = read.load(etl::memory_order_relaxed);
479
480 return p_buffer[read_index];
481 }
482
483 //*************************************************************************
487 //*************************************************************************
488 void clear()
489 {
490 if ETL_IF_CONSTEXPR (etl::is_trivially_destructible<T>::value)
491 {
492 write = 0;
493 read = 0;
494 }
495 else
496 {
497 while (pop())
498 {
499 // Do nothing.
500 }
501 }
502 }
503
504 protected:
505
506 //*************************************************************************
508 //*************************************************************************
509 iqueue_spsc_atomic(T* p_buffer_, size_type reserved_)
510 : base_t(reserved_)
511 , p_buffer(p_buffer_)
512 {
513 }
514
515 private:
516
517 // Disable copy construction and assignment.
518 iqueue_spsc_atomic(const iqueue_spsc_atomic&) ETL_DELETE;
519 iqueue_spsc_atomic& operator=(const iqueue_spsc_atomic&) ETL_DELETE;
520
521 #if ETL_USING_CPP11
522 iqueue_spsc_atomic(iqueue_spsc_atomic&&) = delete;
523 iqueue_spsc_atomic& operator=(iqueue_spsc_atomic&&) = delete;
524 #endif
525
526 T* p_buffer;
527 };
528
529 //***************************************************************************
537 //***************************************************************************
538 template <typename T, size_t Size, const size_t Memory_Model = etl::memory_model::MEMORY_MODEL_LARGE>
539 class queue_spsc_atomic : public iqueue_spsc_atomic<T, Memory_Model>
540 {
541 private:
542
543 typedef typename etl::iqueue_spsc_atomic<T, Memory_Model> base_t;
544
545 public:
546
547 typedef typename base_t::size_type size_type;
548
549 private:
550
551 static ETL_CONSTANT size_type Reserved_Size = size_type(Size + 1);
552
553 public:
554
555 ETL_STATIC_ASSERT((Size <= (etl::integral_limits<size_type>::max - 1)), "Size too large for memory model");
556
557 static ETL_CONSTANT size_type MAX_SIZE = size_type(Size);
558
559 //*************************************************************************
561 //*************************************************************************
562 queue_spsc_atomic()
563 : base_t(reinterpret_cast<T*>(&buffer[0]), Reserved_Size)
564 {
565 }
566
567 //*************************************************************************
569 //*************************************************************************
570 ~queue_spsc_atomic()
571 {
572 base_t::clear();
573 }
574
575 private:
576
578 typename etl::aligned_storage<sizeof(T), etl::alignment_of<T>::value>::type buffer[Reserved_Size];
579 };
580
581 template <typename T, size_t Size, const size_t Memory_Model>
582 ETL_CONSTANT typename queue_spsc_atomic<T, Size, Memory_Model>::size_type queue_spsc_atomic<T, Size, Memory_Model>::MAX_SIZE;
583} // namespace etl
584
585#endif
586
587#endif
Definition absolute.h:40
etl::optional< T > read(etl::bit_stream_reader &stream)
Read a checked type from a stream.
Definition bit_stream.h:1430
ETL_CONSTEXPR TContainer::size_type size(const TContainer &container)
Definition iterator.h:1434
bool write(etl::bit_stream_writer &stream, bool value)
Definition bit_stream.h:1035