ADS
Algorithms and Data Structures
Loading...
Searching...
No Matches
MPMCQueue.h
Go to the documentation of this file.
1/*--------------------------------------------------------------------------*/
2/*---------------------------- File MPMCQueue.h ----------------------------*/
3/*--------------------------------------------------------------------------*/
43/*--------------------------------------------------------------------------*/
44/*----------------------------- DEFINITIONS --------------------------------*/
45/*--------------------------------------------------------------------------*/
46
47#ifndef __ADS_MPMCQueue
48 #define __ADS_MPMCQueue
49/* self-identification: #endif constant */
50
51/*--------------------------------------------------------------------------*/
52/*------------------------------ INCLUDES ----------------------------------*/
53/*--------------------------------------------------------------------------*/
54
55#include "ADS/Contract.h"
56
57#include <algorithm>
58#include <atomic>
59#include <bit>
60#include <cstddef>
61#include <memory>
62#include <new>
63#include <utility>
64
65/*--------------------------------------------------------------------------*/
66/*--------------------------- NAMESPACE ------------------------------------*/
67/*--------------------------------------------------------------------------*/
68
69namespace ADS
70{
71
72/*--------------------------------------------------------------------------*/
73/*---------------------------- CLASS MPMCQueue -----------------------------*/
74/*--------------------------------------------------------------------------*/
75
77template< class T >
79{
80
82 struct Cell
83 {
85 std::atomic< std::size_t > seq;
87 alignas( T ) unsigned char raw[ sizeof( T ) ];
89 [[nodiscard]] T * item( void ) {
90 return( std::launder( reinterpret_cast< T * >( raw ) ) );
91 }
92 };
93
94/*--------------------------------------------------------------------------*/
95 public:
96 using Item = T;
97
100 explicit MPMCQueue( std::size_t capacity );
102 MPMCQueue( const MPMCQueue & ) = delete;
104 MPMCQueue & operator=( const MPMCQueue & ) = delete;
105
108
136 template< class... Args >
137 bool emplace( Args &&... args );
138
141 bool push( const T & v ) { return( emplace( v ) ); }
142
145 bool push( T && v ) { return( emplace( std::move( v ) ) ); }
146
171 bool tryPop( T & x );
172
174 [[nodiscard]] std::size_t size( void ) const;
175
177 [[nodiscard]] bool empty( void ) const { return( size() == 0 ); }
178
180 [[nodiscard]] std::size_t capacity( void ) const { return( f_mask + 1 ); }
181
182/*--------------------------------------------------------------------------*/
183 private:
184 static constexpr std::size_t line = 64;
185
186 alignas( 2 * line ) Cell * f_cell;
187 std::size_t f_mask;
188 alignas( 2 * line ) std::atomic< std::size_t > f_tail{ 0 };
189 alignas( 2 * line ) std::atomic< std::size_t > f_head{ 0 };
190 alignas( 2 * line ) char f_end = 0;
191 }; // end( class MPMCQueue )
192
193/*--------------------------------------------------------------------------*/
194
195} // end( namespace ADS )
196
197/*--------------------------------------------------------------------------*/
198/*-------------------------- IMPLEMENTATION --------------------------------*/
199/*--------------------------------------------------------------------------*/
200
201#include "ADS/concurrent/impl/MPMCQueue.tpp"
202
203/*--------------------------------------------------------------------------*/
204
205#endif /* MPMCQueue.h included */
206
207/*--------------------------------------------------------------------------*/
208/*-------------------------- End File MPMCQueue.h --------------------------*/
209/*--------------------------------------------------------------------------*/
The contracts of ADS: the preconditions, the postconditions and the invariants of the operations,...
MPMCQueue & operator=(const MPMCQueue &)=delete
no copy, since the threads share it by its address
bool push(T &&v)
enqueues , moved, false if the queue is full; any thread may call it
Definition MPMCQueue.h:145
std::size_t size(void) const
the number of items at some moment of the call, at most
bool tryPop(T &x)
dequeues an item into , false if the queue is empty; any thread may call it
bool empty(void) const
true if the queue held no item at some moment of the call
Definition MPMCQueue.h:177
~MPMCQueue()
destroys the items left
MPMCQueue(const MPMCQueue &)=delete
no copy, since the threads share it by its address
bool push(const T &v)
enqueues a copy of , false if the queue is full; any thread may call it
Definition MPMCQueue.h:141
T Item
the type of the items
Definition MPMCQueue.h:96
MPMCQueue(std::size_t capacity)
the empty queue of capacity
bool emplace(Args &&... args)
enqueues the item built from args, false if the queue is full (and then nothing is built); any thread...
std::size_t capacity(void) const
the capacity
Definition MPMCQueue.h:180
Definition ADT.h:96