|
| 1 | +// Copyright 2022, Roman Gershman. All rights reserved. |
| 2 | +// See LICENSE for licensing terms. |
| 3 | +// |
| 4 | + |
| 5 | +#pragma once |
| 6 | + |
| 7 | +#include <atomic> |
| 8 | +#include <cstddef> |
| 9 | + |
| 10 | +// TODO: to move to helio |
| 11 | + |
| 12 | +namespace dfly { |
| 13 | +namespace detail { |
| 14 | + |
| 15 | +// a MPSC queue where multiple threads push and a single thread pops. |
| 16 | +// |
| 17 | +// Requires global functions for T: |
| 18 | +// |
| 19 | +// T* MPSC_intrusive_load_next(const T& src) |
| 20 | +// void MPSC_intrusive_store_next(T* next, T* dest); |
| 21 | +// based on the design from here: |
| 22 | +// https://www.1024cores.net/home/lock-free-algorithms/queues/intrusive-mpsc-node-based-queue |
| 23 | +template <typename T> class MPSCIntrusiveQueue { |
| 24 | + private: |
| 25 | + static constexpr size_t cache_alignment = 64; |
| 26 | + static constexpr size_t cacheline_length = 64; |
| 27 | + |
| 28 | + alignas(cache_alignment) typename std::aligned_storage<sizeof(T), alignof(T)>::type storage_{}; |
| 29 | + T* dummy_; |
| 30 | + alignas(cache_alignment) std::atomic<T*> head_; |
| 31 | + alignas(cache_alignment) T* tail_; |
| 32 | + char pad_[cacheline_length]; |
| 33 | + |
| 34 | + public: |
| 35 | + MPSCIntrusiveQueue() |
| 36 | + : dummy_{reinterpret_cast<T*>(std::addressof(storage_))}, head_{dummy_}, tail_{dummy_} { |
| 37 | + MPSC_intrusive_store_next(dummy_, nullptr); |
| 38 | + } |
| 39 | + |
| 40 | + MPSCIntrusiveQueue(MPSCIntrusiveQueue const&) = delete; |
| 41 | + MPSCIntrusiveQueue& operator=(MPSCIntrusiveQueue const&) = delete; |
| 42 | + |
| 43 | + void Push(T* ctx) noexcept { |
| 44 | + // ctx becomes a new head. |
| 45 | + MPSC_intrusive_store_next(ctx, nullptr); |
| 46 | + T* prev = head_.exchange(ctx, std::memory_order_acq_rel); |
| 47 | + MPSC_intrusive_store_next(prev, ctx); |
| 48 | + } |
| 49 | + |
| 50 | + T* Pop() noexcept; |
| 51 | +}; |
| 52 | + |
| 53 | +template <typename T> T* MPSCIntrusiveQueue<T>::Pop() noexcept { |
| 54 | + T* tail = tail_; |
| 55 | + |
| 56 | + // tail->next_.load(std::memory_order_acquire); |
| 57 | + T* next = MPSC_intrusive_load_next(*tail); |
| 58 | + if (dummy_ == tail) { |
| 59 | + if (nullptr == next) { |
| 60 | + // empty |
| 61 | + return nullptr; |
| 62 | + } |
| 63 | + tail_ = next; |
| 64 | + tail = next; |
| 65 | + next = MPSC_intrusive_load_next(*next); |
| 66 | + } |
| 67 | + |
| 68 | + if (nullptr != next) { |
| 69 | + // non-empty |
| 70 | + tail_ = next; |
| 71 | + return tail; |
| 72 | + } |
| 73 | + |
| 74 | + T* head = head_.load(std::memory_order_acquire); |
| 75 | + if (tail != head) { |
| 76 | + // non-empty, retry is in order: we are in the middle of push. |
| 77 | + return nullptr; |
| 78 | + } |
| 79 | + |
| 80 | + Push(dummy_); |
| 81 | + |
| 82 | + next = MPSC_intrusive_load_next(*tail); |
| 83 | + if (nullptr != next) { |
| 84 | + tail_ = next; |
| 85 | + return tail; |
| 86 | + } |
| 87 | + |
| 88 | + // non-empty, retry is in order: we are still adding. |
| 89 | + return nullptr; |
| 90 | +} |
| 91 | + |
| 92 | +} // namespace detail |
| 93 | +} // namespace dfly |
0 commit comments