|
| 1 | +#pragma once |
| 2 | + |
| 3 | +#include <atomic> |
| 4 | +#include <cstdlib> |
| 5 | +#include <memory> |
| 6 | +#include <new> |
| 7 | + |
| 8 | + |
| 9 | +/// Threadsafe, efficient circular FIFO |
| 10 | +template<typename T> |
| 11 | +class Fifo4 |
| 12 | +{ |
| 13 | +public: |
| 14 | + using ValueType = T; |
| 15 | + |
| 16 | + explicit Fifo4(std::size_t size) |
| 17 | + : size_{size} |
| 18 | + , ring_{ |
| 19 | + static_cast<ValueType*>(std::aligned_alloc(alignof(T), size * sizeof(T))), |
| 20 | + &std::free} |
| 21 | + {} |
| 22 | + |
| 23 | + |
| 24 | + std::size_t size() const { return size_; } |
| 25 | + bool empty() const { return popCursor_ == pushCursor_; } |
| 26 | + bool full() const { return (pushCursor_ - popCursor_) == size_; } |
| 27 | + |
| 28 | + bool push(T const& value) { |
| 29 | + auto pushCursor = pushCursor_.load(std::memory_order_relaxed); |
| 30 | + if (full(pushCursor, popCursorCached_)) { |
| 31 | + popCursorCached_ = popCursor_.load(std::memory_order_acquire); |
| 32 | + } |
| 33 | + if (full(pushCursor, popCursorCached_)) { |
| 34 | + return false; |
| 35 | + } |
| 36 | + |
| 37 | + new (&ring_[pushCursor % size_]) T(value); |
| 38 | + pushCursor_.store(pushCursor + 1, std::memory_order_release); |
| 39 | + return true; |
| 40 | + } |
| 41 | + |
| 42 | + /// Pop one object from the fifo. |
| 43 | + /// @return `true` if the pop operation is successful; `false` if fifo was empty. |
| 44 | + bool pop(T& value) { |
| 45 | + auto popCursor = popCursor_.load(std::memory_order_relaxed); |
| 46 | + if (empty(pushCursorCached_, popCursor)) { |
| 47 | + pushCursorCached_ = pushCursor_.load(std::memory_order_acquire); |
| 48 | + } |
| 49 | + if (empty(pushCursorCached_, popCursor)) { |
| 50 | + return false; |
| 51 | + } |
| 52 | + |
| 53 | + value = ring_[popCursor % size_]; |
| 54 | + ring_[popCursor % size_].~T(); |
| 55 | + popCursor_.store(popCursor + 1, std::memory_order_release); |
| 56 | + return true; |
| 57 | + } |
| 58 | + |
| 59 | +private: |
| 60 | + bool full(std::size_t pushCursor, std::size_t popCursor) const { |
| 61 | + return (pushCursor - popCursor) == size_; |
| 62 | + } |
| 63 | + static bool empty(std::size_t pushCursor, std::size_t popCursor) { |
| 64 | + return pushCursor == popCursor; |
| 65 | + } |
| 66 | + |
| 67 | +private: |
| 68 | + std::size_t size_; |
| 69 | + |
| 70 | + using RingType = std::unique_ptr<ValueType[], decltype(&std::free)>; |
| 71 | + RingType ring_; |
| 72 | + |
| 73 | + // https://stackoverflow.com/questions/39680206/understanding-stdhardware-destructive-interference-size-and-stdhardware-cons |
| 74 | + static constexpr auto hardware_destructive_interference_size = std::size_t{128}; |
| 75 | + |
| 76 | + /// Loaded and stored by the push thread; loaded by the pop thread |
| 77 | + // TODO fix initialization on the others |
| 78 | + alignas(hardware_destructive_interference_size) std::atomic<std::size_t> pushCursor_; |
| 79 | + |
| 80 | + /// Exclusive to the pop thread |
| 81 | + alignas(hardware_destructive_interference_size) std::size_t popCursorCached_{}; |
| 82 | + |
| 83 | + /// Loaded and stored by the pop thread; loaded by the push thread |
| 84 | + alignas(hardware_destructive_interference_size) std::atomic<std::size_t> popCursor_; |
| 85 | + |
| 86 | + /// Exclusive to the push thread |
| 87 | + alignas(hardware_destructive_interference_size) std::size_t pushCursorCached_{}; |
| 88 | +}; |
0 commit comments