COROIO: NNet::TGrowMailbox< T, Levels > Class Template Reference
COROIO
Loading...
Searching...
No Matches
NNet::TGrowMailbox< T, Levels > Class Template Reference

Unbounded lock-free queue with many producers and one consumer, without links. More...

#include <grow_mailbox.hpp>

Public Member Functions

 TGrowMailbox (size_t initialCapacity=64)
 initialCapacity is the size of the first ring, rounded up to a power of two, at least 2.
 TGrowMailbox (const TGrowMailbox &)=delete
TGrowMailbox & operator= (const TGrowMailbox &)=delete
void Push (T &&value)
 Appends a value. Any thread.
std::optional< T > Pop ()
 Removes the oldest value, or returns nothing if there is none. Consumer only.
size_t Capacity () const
 Capacity of the ring that producers push to now. Any thread; for diagnostics and tests.

Detailed Description

template<typename T, size_t Levels = 32>
class NNet::TGrowMailbox< T, Levels >

Unbounded lock-free queue with many producers and one consumer, without links.

Values are moved into the cells of a ring (NDetail::TMpscRing), so a push neither allocates nor links a node. When the open ring is full, the producer that sees it closes it and opens the next ring, twice as large, from a fixed directory of Levels rings. Rings are never freed while the mailbox lives: a slow producer may still hold a pointer to a closed ring, and a level is allocated once, so no reclamation scheme is needed. A mailbox that keeps up stays in one ring.

All rings together are smaller than twice the largest one. While a level opens, every producer that finds it without a ring allocates one, so that none waits for another, and all but one free theirs again: with P producers in Push at once, the memory briefly stays below P + 1 times the new ring.

Push may be called from any thread, Pop from one thread at a time. Values of one producer are popped in the order it pushed them, and a push that happens before another is popped first: the producers go to later rings only, and the consumer leaves a ring only when it is closed and empty.

The mailbox does not wake the consumer. A producer notifies after every push, and an INotifier coalesces the notifications:

// Any producer thread
mailbox.Push(std::move(value));
notifier.Notify();
// Consumer coroutine on the notifier's poller
while (true) {
co_await notifier.Wait();
while (auto value = mailbox.Pop()) {
Process(*value);
}
}

Pop may miss a value whose producer has claimed a cell and not written it yet; that producer notifies after it writes.

Template Parameters
TValue type; its move constructor must not throw.
LevelsSize of the directory: the mailbox holds up to initialCapacity * (2^Levels - 1) values.
Note
Stop the producers and the consumer before destroying the mailbox. The destructor destroys the values that were not popped.

Member Function Documentation

◆ Push()

template<typename T, size_t Levels = 32>
void NNet::TGrowMailbox< T, Levels >::Push ( T && value)
inline

Appends a value. Any thread.

Exceptions
std::bad_allocif the next ring cannot be allocated, std::length_error if the directory is exhausted. value is left as it was.

The documentation for this class was generated from the following file: