|
| 1 | +#pragma once |
| 2 | + |
| 3 | +#include <atomic> |
| 4 | +#include <cassert> |
| 5 | +#include <memory> |
| 6 | +#include <new> |
| 7 | + |
| 8 | + |
| 9 | +/** |
| 10 | + * Threadsafe, efficient circular FIFO with constrained cursors |
| 11 | + * |
| 12 | + * This Fifo is useful when you need to constrain the cursor ranges. For |
| 13 | + * example say if the sizeof(CursorType) is 8 or 15. The cursors may |
| 14 | + * take on any value up to the fifo's capacity + 1. Furthermore, there |
| 15 | + * are no calculations where an intermediate cursor value is larger then |
| 16 | + * this number. And, finally, the cursors are never negative. |
| 17 | + * |
| 18 | + * The problem that must be resolved is how to distinguish an empty fifo |
| 19 | + * from a full one and still meet the above constraints. First, define |
| 20 | + * an empty fifo as when the pushCursor and popCursor are equal. We |
| 21 | + * cannot define a full fifo as pushCursor == popCursor + capacity. |
| 22 | + * Firstly, the intermediate value popCursor + capacity can overflow if |
| 23 | + * a signed cursor is used and if the cursors are constrained to |
| 24 | + * [0..capacity) there is no distiction between the full definition and |
| 25 | + * the empty definition. |
| 26 | + * |
| 27 | + * To resolve this we introduce the idea of a sentinal element by |
| 28 | + * allocating one more element than the capacity of the fifo and define |
| 29 | + * a full fifo as when the cursors are "one apart". That is, |
| 30 | + * |
| 31 | + * @code |
| 32 | + * | pushCursor < popCursor: pushCursor == popCursor - 1 |
| 33 | + * | popCursor < pushCursor: popCursor == pushCursor - capacity |
| 34 | + * | else: false |
| 35 | + * @endcode |
| 36 | + */ |
| 37 | +template<typename T, typename Alloc = std::allocator<T>> |
| 38 | +class Fifo3a : private Alloc |
| 39 | +{ |
| 40 | +public: |
| 41 | + using value_type = T; |
| 42 | + using allocator_traits = std::allocator_traits<Alloc>; |
| 43 | + using size_type = typename allocator_traits::size_type; |
| 44 | + |
| 45 | + explicit Fifo3a(size_type capacity, Alloc const& alloc = Alloc{}) |
| 46 | + : Alloc{alloc} |
| 47 | + , capacity_{capacity} |
| 48 | + , ring_{allocator_traits::allocate(*this, capacity + 1)} |
| 49 | + {} |
| 50 | + |
| 51 | + ~Fifo3a() { |
| 52 | + // TODO fix shouldn't matter for benchmark since it waits until |
| 53 | + // the fifo is empty and only need if destructors have side |
| 54 | + // effects. |
| 55 | + // while(not empty()) { |
| 56 | + // ring_[popCursor_ & mask_].~T(); |
| 57 | + // ++popCursor_; |
| 58 | + // } |
| 59 | + allocator_traits::deallocate(*this, ring_, capacity_ + 1); |
| 60 | + } |
| 61 | + |
| 62 | + /// Returns the number of elements in the fifo |
| 63 | + auto size() const noexcept { |
| 64 | + auto pushCursor = pushCursor_.load(std::memory_order_relaxed); |
| 65 | + auto popCursor = popCursor_.load(std::memory_order_relaxed); |
| 66 | + if (popCursor <= pushCursor) { |
| 67 | + return pushCursor - popCursor; |
| 68 | + } else { |
| 69 | + return capacity_ - (popCursor - (pushCursor + 1)); |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + /// Returns whether the container has no elements |
| 74 | + auto empty() const noexcept { |
| 75 | + auto pushCursor = pushCursor_.load(std::memory_order_relaxed); |
| 76 | + auto popCursor = popCursor_.load(std::memory_order_relaxed); |
| 77 | + return empty(pushCursor, popCursor); |
| 78 | + } |
| 79 | + |
| 80 | + /// Returns whether the container has capacity() elements |
| 81 | + auto full() const noexcept { |
| 82 | + auto pushCursor = pushCursor_.load(std::memory_order_relaxed); |
| 83 | + auto popCursor = popCursor_.load(std::memory_order_relaxed); |
| 84 | + return full(pushCursor, popCursor); |
| 85 | + } |
| 86 | + |
| 87 | + /// Returns the number of elements that can be held in the fifo |
| 88 | + auto capacity() const noexcept { return capacity_; } |
| 89 | + |
| 90 | + |
| 91 | + /// Push one object onto the fifo. |
| 92 | + /// @return `true` if the operation is successful; `false` if fifo is full. |
| 93 | + auto push(T const& value) { |
| 94 | + auto pushCursor = pushCursor_.load(std::memory_order_relaxed); |
| 95 | + auto popCursor = popCursor_.load(std::memory_order_acquire); |
| 96 | + if (full(pushCursor, popCursor)) { |
| 97 | + return false; |
| 98 | + } |
| 99 | + new (&ring_[pushCursor]) T(value); |
| 100 | + if (pushCursor == capacity_) { |
| 101 | + pushCursor_.store(0, std::memory_order_release); |
| 102 | + } else { |
| 103 | + pushCursor_.store(pushCursor + 1, std::memory_order_release); |
| 104 | + } |
| 105 | + return true; |
| 106 | + } |
| 107 | + |
| 108 | + /// Pop one object from the fifo. |
| 109 | + /// @return `true` if the pop operation is successful; `false` if fifo is empty. |
| 110 | + auto pop(T& value) { |
| 111 | + auto pushCursor = pushCursor_.load(std::memory_order_acquire); |
| 112 | + auto popCursor = popCursor_.load(std::memory_order_relaxed); |
| 113 | + if (empty(pushCursor, popCursor)) { |
| 114 | + return false; |
| 115 | + } |
| 116 | + value = ring_[popCursor]; |
| 117 | + ring_[popCursor].~T(); |
| 118 | + if (popCursor == capacity_) { |
| 119 | + popCursor_.store(0, std::memory_order_release); |
| 120 | + } else { |
| 121 | + popCursor_.store(popCursor + 1, std::memory_order_release); |
| 122 | + } |
| 123 | + return true; |
| 124 | + } |
| 125 | + |
| 126 | +private: |
| 127 | + auto full(size_type pushCursor, size_type popCursor) const noexcept { |
| 128 | + if (pushCursor < popCursor) { |
| 129 | + return pushCursor == popCursor - 1; |
| 130 | + } else if (popCursor < pushCursor) { |
| 131 | + return popCursor == pushCursor - capacity_; |
| 132 | + } else { |
| 133 | + return false; |
| 134 | + } |
| 135 | + } |
| 136 | + static auto empty(size_type pushCursor, size_type popCursor) noexcept { |
| 137 | + return pushCursor == popCursor; |
| 138 | + } |
| 139 | + |
| 140 | +private: |
| 141 | + size_type capacity_; |
| 142 | + T* ring_; |
| 143 | + |
| 144 | + using CursorType = std::atomic<size_type>; |
| 145 | + static_assert(CursorType::is_always_lock_free); |
| 146 | + |
| 147 | + // See Fifo3 for reason std::hardware_destructive_interference_size is not used directly |
| 148 | + static constexpr auto hardware_destructive_interference_size = size_type{64}; |
| 149 | + |
| 150 | + /// Loaded and stored by the push thread; loaded by the pop thread |
| 151 | + alignas(hardware_destructive_interference_size) CursorType pushCursor_; |
| 152 | + |
| 153 | + /// Loaded and stored by the pop thread; loaded by the push thread |
| 154 | + alignas(hardware_destructive_interference_size) CursorType popCursor_; |
| 155 | + |
| 156 | + // Padding to avoid false sharing with adjacent objects |
| 157 | + char padding_[hardware_destructive_interference_size - sizeof(CursorType)]; |
| 158 | +}; |
0 commit comments