ADS
Algorithms and Data Structures
Toggle main menu visibility
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
69
namespace
ADS
70
{
71
72
/*--------------------------------------------------------------------------*/
73
/*---------------------------- CLASS MPMCQueue -----------------------------*/
74
/*--------------------------------------------------------------------------*/
75
77
template
<
class
T >
78
class
MPMCQueue
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
107
~MPMCQueue
();
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
/*--------------------------------------------------------------------------*/
Contract.h
The contracts of ADS: the preconditions, the postconditions and the invariants of the operations,...
ADS::MPMCQueue::operator=
MPMCQueue & operator=(const MPMCQueue &)=delete
no copy, since the threads share it by its address
ADS::MPMCQueue::push
bool push(T &&v)
enqueues , moved, false if the queue is full; any thread may call it
Definition
MPMCQueue.h:145
ADS::MPMCQueue::size
std::size_t size(void) const
the number of items at some moment of the call, at most
ADS::MPMCQueue::tryPop
bool tryPop(T &x)
dequeues an item into , false if the queue is empty; any thread may call it
ADS::MPMCQueue::empty
bool empty(void) const
true if the queue held no item at some moment of the call
Definition
MPMCQueue.h:177
ADS::MPMCQueue::~MPMCQueue
~MPMCQueue()
destroys the items left
ADS::MPMCQueue::MPMCQueue
MPMCQueue(const MPMCQueue &)=delete
no copy, since the threads share it by its address
ADS::MPMCQueue::push
bool push(const T &v)
enqueues a copy of , false if the queue is full; any thread may call it
Definition
MPMCQueue.h:141
ADS::MPMCQueue::Item
T Item
the type of the items
Definition
MPMCQueue.h:96
ADS::MPMCQueue::MPMCQueue
MPMCQueue(std::size_t capacity)
the empty queue of capacity
ADS::MPMCQueue::emplace
bool emplace(Args &&... args)
enqueues the item built from args, false if the queue is full (and then nothing is built); any thread...
ADS::MPMCQueue::capacity
std::size_t capacity(void) const
the capacity
Definition
MPMCQueue.h:180
ADS
Definition
ADT.h:96
ADS
concurrent
MPMCQueue.h
Generated by
1.17.0