ADS
Algorithms and Data Structures
Loading...
Searching...
No Matches
MPSCInbox.h
Go to the documentation of this file.
1/*--------------------------------------------------------------------------*/
2/*---------------------------- File MPSCInbox.h ----------------------------*/
3/*--------------------------------------------------------------------------*/
56/*--------------------------------------------------------------------------*/
57/*----------------------------- DEFINITIONS --------------------------------*/
58/*--------------------------------------------------------------------------*/
59
60#ifndef __ADS_MPSCInbox
61 #define __ADS_MPSCInbox
62/* self-identification: #endif constant */
63
64/*--------------------------------------------------------------------------*/
65/*------------------------------ INCLUDES ----------------------------------*/
66/*--------------------------------------------------------------------------*/
67
68#include "ADS/Contract.h"
70
71#include <atomic>
72#include <cstddef>
73#include <cstdint>
74#include <new>
75#include <utility>
76#include <vector>
77
78/*--------------------------------------------------------------------------*/
79/*--------------------------- NAMESPACE ------------------------------------*/
80/*--------------------------------------------------------------------------*/
81
82namespace ADS
83{
84
85/*--------------------------------------------------------------------------*/
86/*---------------------------- CLASS MPSCInbox -----------------------------*/
87/*--------------------------------------------------------------------------*/
88
90template< class T >
92{
93
94/*--------------------------------------------------------------------------*/
95 public:
96 using Item = T;
97
99 MPSCInbox( void ) = default;
101 MPSCInbox( const MPSCInbox & ) = delete;
103 MPSCInbox & operator=( const MPSCInbox & ) = delete;
104
107
126 void push( T v );
127
145 [[nodiscard]] std::vector< T > take( void );
146
149 std::size_t takeInto( std::vector< T > & out );
150
153 [[nodiscard]] std::vector< T > waitTake( void ) {
154 std::vector< T > v;
155 f_wait.await( [ & ] {
156 v = take();
157 return( ! v.empty() );
158 } );
159 return( v );
160 }
161
163 [[nodiscard]] bool empty( void ) const;
164
165/*--------------------------------------------------------------------------*/
166 private:
167 static constexpr std::size_t kCells = 64;
168
169 struct Block;
170 struct Owner;
171
173 struct Cell
174 {
176 alignas( T ) unsigned char raw[ sizeof( T ) ];
178 Cell * next;
180 Block * home;
182 [[nodiscard]] T * value( void ) {
183 return( std::launder( reinterpret_cast< T * >( raw ) ) );
184 }
185 };
186
188 struct Block
189 {
191 alignas( 64 ) std::atomic< std::size_t > taken{ 0 };
192 Owner * owner;
193 Block * spare = nullptr;
194 Cell cells[ kCells ];
195
197 explicit Block( Owner * o ) : owner( o ) {
198 for( Cell & c : cells )
199 c.home = this;
200 }
201 };
202
204 struct Owner
205 {
206 Block * cur = nullptr;
207 std::size_t used = 0;
208 Block * spare = nullptr;
210 alignas( 64 ) std::atomic< Block * > given{ nullptr };
212 std::atomic< std::size_t > refs{ 1 };
213 };
214
216 class Home;
217
219 static Owner & owner( void );
220
222 static Block * fresh( Owner & o );
223
225 static void release( Block * b , std::size_t k );
226
228 static void destroy( Block * b );
229
231 static Block * closed( void ) {
232 return( reinterpret_cast< Block * >( std::uintptr_t( 1 ) ) );
233 }
234
237 template< class G , class F >
238 static void drain( Cell * c , G && count , F && f );
239
240 std::atomic< Cell * > f_top{ nullptr };
241 EventCount f_wait;
242
243 }; // end( class MPSCInbox )
244
245/*--------------------------------------------------------------------------*/
246
247} // end( namespace ADS )
248
249/*--------------------------------------------------------------------------*/
250/*-------------------------- IMPLEMENTATION --------------------------------*/
251/*--------------------------------------------------------------------------*/
252
253#include "ADS/concurrent/impl/MPSCInbox.tpp"
254
255/*--------------------------------------------------------------------------*/
256
257#endif /* MPSCInbox.h included */
258
259/*--------------------------------------------------------------------------*/
260/*-------------------------- End File MPSCInbox.h --------------------------*/
261/*--------------------------------------------------------------------------*/
The contracts of ADS: the preconditions, the postconditions and the invariants of the operations,...
An event count, beyond the book: the way for a thread to sleep until a condition on a lock-free struc...
MPSCInbox & operator=(const MPSCInbox &)=delete
no copy, since the threads share it by its address
std::size_t takeInto(std::vector< T > &out)
the messages of take() appended to out, which keeps its room from call to call, and their number; onl...
std::vector< T > waitTake(void)
the messages of take(), sleeping until at least one comes [see EventCount.h]; only the consumer calls...
Definition MPSCInbox.h:153
MPSCInbox(void)=default
the empty inbox
std::vector< T > take(void)
all the messages sent so far and not yet taken, those of each sender in the order of sending; only th...
~MPSCInbox()
frees the messages not taken
MPSCInbox(const MPSCInbox &)=delete
no copy, since the threads share it by its address
bool empty(void) const
true if no message was waiting at some moment of the call
T Item
the type of the messages
Definition MPSCInbox.h:96
void push(T v)
sends , and wakes the consumer if it sleeps in waitTake() and the stack was empty (a fence and the re...
Definition ADT.h:96