|
| 1 | +#pragma once |
| 2 | + |
| 3 | +#include <atomic> |
| 4 | +#include <cassert> |
| 5 | +#include <memory> |
| 6 | +#include <new> |
| 7 | + |
| 8 | + |
| 9 | +/// Threadsafe, efficient circular FIFO with cached cursors; constrained cursors |
| 10 | +template<typename T, typename Alloc = std::allocator<T>> |
| 11 | +class Fifo4b : private Alloc |
| 12 | +{ |
| 13 | +public: |
| 14 | + using value_type = T; |
| 15 | + using allocator_traits = std::allocator_traits<Alloc>; |
| 16 | + using size_type = typename allocator_traits::size_type; |
| 17 | + |
| 18 | + explicit Fifo4b(size_type capacity, Alloc const& alloc = Alloc{}) |
| 19 | + : Alloc{alloc} |
| 20 | + , capacity_{capacity + 1} |
| 21 | + , ring_{allocator_traits::allocate(*this, capacity)} |
| 22 | + {} |
| 23 | + |
| 24 | + ~Fifo4b() { |
| 25 | + // TODO fix shouldn't matter for benchmark since it waits until |
| 26 | + // the fifo is empty |
| 27 | + // while(not empty()) { |
| 28 | + // ring_[popCursor_ & mask_].~T(); |
| 29 | + // ++popCursor_; |
| 30 | + // } |
| 31 | + allocator_traits::deallocate(*this, ring_, capacity()); |
| 32 | + } |
| 33 | + |
| 34 | + /// Returns the number of elements in the fifo |
| 35 | + auto size() const noexcept { |
| 36 | + auto pushCursor = pushCursor_.load(std::memory_order_relaxed); |
| 37 | + auto popCursor = popCursor_.load(std::memory_order_relaxed); |
| 38 | + |
| 39 | + assert(popCursor <= pushCursor); |
| 40 | + return pushCursor - popCursor; |
| 41 | + } |
| 42 | + |
| 43 | + /// Returns whether the container has no elements |
| 44 | + auto empty() const noexcept { return size() == 0; } |
| 45 | + |
| 46 | + /// Returns whether the container has capacity() elements |
| 47 | + auto full() const noexcept { return size() == capacity(); } |
| 48 | + |
| 49 | + /// Returns the number of elements that can be held in the fifo |
| 50 | + auto capacity() const noexcept { return capacity_ - 1; } |
| 51 | + |
| 52 | + |
| 53 | + /// Push one object onto the fifo. |
| 54 | + /// @return `true` if the operation is successful; `false` if fifo is full. |
| 55 | + auto push(T const& value) { |
| 56 | + auto pushCursor = pushCursor_.load(std::memory_order_relaxed); |
| 57 | + auto nextPushCursor = pushCursor + 1; |
| 58 | + if (nextPushCursor == capacity_) { |
| 59 | + nextPushCursor = 0; |
| 60 | + } |
| 61 | + if (nextPushCursor == popCursorCached_) { |
| 62 | + popCursorCached_ = popCursor_.load(std::memory_order_acquire); |
| 63 | + if (nextPushCursor == popCursorCached_) { |
| 64 | + return false; |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + new (&ring_[pushCursor]) T(value); |
| 69 | + pushCursor_.store(nextPushCursor, std::memory_order_release); |
| 70 | + return true; |
| 71 | + } |
| 72 | + |
| 73 | + /// Pop one object from the fifo. |
| 74 | + /// @return `true` if the pop operation is successful; `false` if fifo is empty. |
| 75 | + auto pop(T& value) { |
| 76 | + auto popCursor = popCursor_.load(std::memory_order_relaxed); |
| 77 | + if (popCursor == pushCursorCached_) { |
| 78 | + pushCursorCached_ = pushCursor_.load(std::memory_order_acquire); |
| 79 | + if (pushCursorCached_ == popCursor) { |
| 80 | + return false; |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + value = ring_[popCursor]; |
| 85 | + ring_[popCursor].~T(); |
| 86 | + auto nextPopCursor = popCursor + 1; |
| 87 | + if (nextPopCursor == capacity_) { |
| 88 | + nextPopCursor = 0; |
| 89 | + } |
| 90 | + popCursor_.store(nextPopCursor, std::memory_order_release); |
| 91 | + return true; |
| 92 | + } |
| 93 | + |
| 94 | +private: |
| 95 | + auto full(size_type pushCursor, size_type popCursor) const noexcept { |
| 96 | + return (pushCursor - popCursor) == capacity(); |
| 97 | + } |
| 98 | + static auto empty(size_type pushCursor, size_type popCursor) noexcept { |
| 99 | + return pushCursor == popCursor; |
| 100 | + } |
| 101 | + |
| 102 | +private: |
| 103 | + size_type capacity_; |
| 104 | + T* ring_; |
| 105 | + |
| 106 | + using CursorType = std::atomic<size_type>; |
| 107 | + static_assert(CursorType::is_always_lock_free); |
| 108 | + |
| 109 | + // See Fifo3 for reason std::hardware_destructive_interference_size is not used directly |
| 110 | + static constexpr auto hardware_destructive_interference_size = size_type{64}; |
| 111 | + |
| 112 | + /// Loaded and stored by the push thread; loaded by the pop thread |
| 113 | + alignas(hardware_destructive_interference_size) CursorType pushCursor_; |
| 114 | + |
| 115 | + /// Exclusive to the push thread |
| 116 | + alignas(hardware_destructive_interference_size) size_type popCursorCached_{}; |
| 117 | + |
| 118 | + /// Loaded and stored by the pop thread; loaded by the push thread |
| 119 | + alignas(hardware_destructive_interference_size) CursorType popCursor_; |
| 120 | + |
| 121 | + /// Exclusive to the pop thread |
| 122 | + alignas(hardware_destructive_interference_size) size_type pushCursorCached_{}; |
| 123 | + |
| 124 | + // Padding to avoid false sharing with adjacent objects |
| 125 | + char padding_[hardware_destructive_interference_size - sizeof(size_type)]; |
| 126 | +}; |
0 commit comments