19inline constexpr size_t MailboxCacheLine = 64;
35 static constexpr uint64_t ClosedBit = uint64_t(1) << 63;
46 , Cells_(new TCell[capacity])
48 assert(capacity >= 2 && (capacity & Mask_) == 0);
49 for (
size_t i = 0; i < capacity; ++i) {
50 Cells_[i].Seq.store(i, std::memory_order_relaxed);
56 const uint64_t tail = Tail_.load(std::memory_order_relaxed) & ~ClosedBit;
57 for (uint64_t pos = Head_; pos != tail; ++pos) {
58 Value(Cells_[pos & Mask_])->~T();
68 uint64_t pos = Tail_.load(std::memory_order_relaxed);
69 TCell* cell =
nullptr;
71 if (pos & ClosedBit) {
74 cell = &Cells_[pos & Mask_];
76 const int64_t diff =
static_cast<int64_t
>(cell->Seq.load(std::memory_order_acquire) - pos);
79 if (Tail_.compare_exchange_weak(pos, pos + 1, std::memory_order_relaxed, std::memory_order_relaxed)) {
82 }
else if (diff < 0) {
85 pos = Tail_.load(std::memory_order_relaxed);
88 new (cell->Storage) T(std::move(value));
89 cell->Seq.store(pos + 1, std::memory_order_release);
95 TCell& cell = Cells_[Head_ & Mask_];
96 if (cell.Seq.load(std::memory_order_acquire) != Head_ + 1) {
99 T* value = Value(cell);
100 std::optional<T> result(std::move(*value));
103 cell.Seq.store(Head_ + Mask_ + 1, std::memory_order_release);
110 Tail_.fetch_or(ClosedBit, std::memory_order_acq_rel);
115 const uint64_t tail = Tail_.load(std::memory_order_acquire);
116 return (tail & ClosedBit) && Head_ == (tail & ~ClosedBit);
119 size_t Capacity()
const {
125 struct alignas(MailboxCacheLine) TCell {
126 std::atomic<uint64_t> Seq;
127 alignas(T) std::byte Storage[
sizeof(T)];
130 static T* Value(TCell& cell) {
131 return std::launder(
reinterpret_cast<T*
>(cell.Storage));
134 const uint64_t Mask_;
136 alignas(MailboxCacheLine) std::atomic<uint64_t> Tail_ = 0;
137 alignas(MailboxCacheLine) uint64_t Head_ = 0;
188template<
typename T,
size_t Levels = 32>
190 static_assert(std::is_nothrow_move_constructible_v<T>,
191 "a producer must not leave a claimed cell unwritten");
198 : Initial_(std::bit_ceil(std::max<size_t>(initialCapacity, 2)))
199 , Consuming_(new TRing(Initial_))
201 Rings_[0].store(Consuming_, std::memory_order_relaxed);
205 for (
auto& ring : Rings_) {
206 delete ring.load(std::memory_order_relaxed);
210 TGrowMailbox(
const TGrowMailbox&) =
delete;
211 TGrowMailbox& operator=(
const TGrowMailbox&) =
delete;
222 const uint32_t open = Open_.load(std::memory_order_acquire);
223 TRing* ring = Rings_[open].load(std::memory_order_acquire);
224 const auto result = ring->
TryPush(value);
225 if (result == TRing::EPush::Ok) {
228 if (result == TRing::EPush::Full) {
238 if (
auto value = Consuming_->TryPop()) {
242 if (Open_.load(std::memory_order_acquire) <= Current_ || !Consuming_->Finished()) {
246 Consuming_ = Rings_[Current_].load(std::memory_order_acquire);
252 return Initial_ << Open_.load(std::memory_order_relaxed);
257 void Grow(uint32_t open) {
258 if (open + 1 == Levels) {
259 throw std::length_error(
"TGrowMailbox: all levels are used");
261 if (!Rings_[open + 1].load(std::memory_order_acquire)) {
262 auto* fresh =
new TRing(Initial_ << (open + 1));
263 TRing* expected =
nullptr;
264 if (!Rings_[open + 1].compare_exchange_strong(expected, fresh,
265 std::memory_order_acq_rel, std::memory_order_acquire))
270 uint32_t expected = open;
271 Open_.compare_exchange_strong(expected, open + 1, std::memory_order_acq_rel, std::memory_order_relaxed);
274 const size_t Initial_;
276 alignas(NDetail::MailboxCacheLine) std::atomic<uint32_t> Open_ = 0;
277 std::atomic<TRing*> Rings_[Levels];
279 alignas(NDetail::MailboxCacheLine) uint32_t Current_ = 0;
Bounded ring with many producers and one consumer; a level of TGrowMailbox.
Definition grow_mailbox.hpp:33
TMpscRing(size_t capacity)
capacity is a power of two, at least 2: with one cell "written" and "free" look the same.
Definition grow_mailbox.hpp:44
EPush TryPush(T &value)
Any thread. Moves from value only on Ok.
Definition grow_mailbox.hpp:67
std::optional< T > TryPop()
Consumer only. Empty also while the cell at the head is claimed and not written yet.
Definition grow_mailbox.hpp:94
void Close()
Any thread.
Definition grow_mailbox.hpp:109
~TMpscRing()
Destroys the values that were not popped. The producers have stopped.
Definition grow_mailbox.hpp:55
bool Finished() const
Consumer only: closed, and every value pushed before has been popped.
Definition grow_mailbox.hpp:114
Unbounded lock-free queue with many producers and one consumer, without links.
Definition grow_mailbox.hpp:189
std::optional< T > Pop()
Removes the oldest value, or returns nothing if there is none. Consumer only.
Definition grow_mailbox.hpp:236
void Push(T &&value)
Appends a value. Any thread.
Definition grow_mailbox.hpp:219
TGrowMailbox(size_t initialCapacity=64)
initialCapacity is the size of the first ring, rounded up to a power of two, at least 2.
Definition grow_mailbox.hpp:197
size_t Capacity() const
Capacity of the ring that producers push to now. Any thread; for diagnostics and tests.
Definition grow_mailbox.hpp:251