|
ADS
Algorithms and Data Structures
|
An inbox that many producer threads fill and one consumer thread empties, lock-free, beyond the book: the messages to a worker of a parallel search, e.g., the nodes that the other workers send to their owner. More...
#include "ADS/Contract.h"#include "ADS/concurrent/EventCount.h"#include <atomic>#include <cstddef>#include <cstdint>#include <new>#include <utility>#include <vector>#include "ADS/concurrent/impl/MPSCInbox.tpp"Go to the source code of this file.
Classes | |
| class | ADS::MPSCInbox< T > |
| an inbox filled by many threads and emptied by one, lock-free More... | |
Namespaces | |
| namespace | ADS |
An inbox that many producer threads fill and one consumer thread empties, lock-free, beyond the book: the messages to a worker of a parallel search, e.g., the nodes that the other workers send to their owner.
A producer pushes a cell on a stack with a compare-and-swap on its top (the stack of Treiber); the consumer takes the whole stack at once by exchanging its top with nil, and reverses it, so that the messages of each producer come out in the order in which it sent them, and the consumer never competes with the producers on single items; since the consumer only takes the whole stack, and never pops a single cell, the ABA problem of the stack of Treiber cannot occur (a cell used again is pushed as a new one, above the top that its pusher read).
The cells come from blocks of 64 that each producing thread owns, one set for each type of message, shared by all the inboxes it sends to: the producer takes the cells of its current block in order, with no atomic operation, and a consumer that has taken some messages of a block adds their number to a count of the block, once per run of them; the consumer that brings it to 64 gives the block back to its owner, on a stack that the owner empties at once (an exchange) when it needs a block, before it allocates one. Thus a producer in a steady state cycles over the same blocks, with no allocation, and the consumer frees nothing. Hence take() costs one exchange however many messages there are, one fetch-and-add per run of messages of one block, a loop of compare-and-swap per block given back, and \( O( k ) \) time for the \( k \) messages taken: it is lock-free, and wait-free but for the blocks given back. A block is given back only after its consumers have moved out the messages of all its cells, so that a cell is never written again while a consumer reads it, nor pushed while it is in a stack. A producer keeps for reuse all the blocks it ever held at once, at least \( \lceil m / 64 \rceil \) for \( m \) of its messages in the inboxes. A thread that ends frees the blocks it holds and closes its stack, and the consumer that empties one of its blocks still in some inbox frees it.
A push wakes the consumer only when it finds the stack empty: a push onto a stack that is not empty is taken by the same take() as the push that found it empty, which woke the consumer, so that a burst of messages costs one fence of the event count instead of one per message.
The contracts [see Contract.h]: only one thread calls take(). The operations are defined in MPSCInbox.tpp.