10#include "polymorphic_optional.hpp"
17namespace hi::inline v1 {
29template<
typename T, std::
size_t SlotSize>
32 static_assert(std::has_single_bit(SlotSize),
"Only power-of-two number of messages size allowed.");
33 static_assert(SlotSize < 65536);
40 static constexpr std::size_t num_slots = fifo_size / slot_size;
52 [[nodiscard]]
bool empty() const noexcept
54 return _head.load(std::memory_order::relaxed) == _tail;
64 template<
typename Func>
67 auto result = get_slot(_tail).invoke_and_reset(std::forward<Func>(func));
80 template<
typename Operation>
81 void take_all(Operation
const& operation)
noexcept
83 while (take_one(operation)) {}
93 template<
typename Message,
typename Func,
typename... Args>
103 hilet offset = _head.fetch_add(slot_size, std::memory_order::relaxed);
104 return get_slot(offset).wait_emplace_and_invoke<Message>(std::forward<Func>(func), std::forward<Args>(args)...);
107 template<
typename Func,
typename Object>
108 hi_force_inline
auto insert_and_invoke(Func&& func, Object&&
object)
noexcept
110 return emplace_and_invoke<std::decay_t<Object>>(std::forward<Func>(func), std::forward<Object>(
object));
113 template<
typename Message,
typename... Args>
114 hi_force_inline
void emplace(Args&&...args)
noexcept
116 return emplace_and_invoke<Message>([](Message&) ->
void {}, std::forward<Args>(args)...);
119 template<
typename Object>
120 hi_force_inline
void insert(Object &&
object)
noexcept
122 return emplace<std::decay_t<Object>>(std::forward<Object>(
object));
133 hi_force_inline slot_type& get_slot(uint16_t offset)
noexcept
135 hi_axiom(offset % slot_size == 0);
137 return *std::launder(
138 std::assume_aligned<slot_size>(
reinterpret_cast<slot_type *
>(
reinterpret_cast<char *
>(
this) + offset)));
#define hilet
Invariant should be the default for variables.
Definition required.hpp:23
Functions and macros for handling architectural difference between compilers, CPUs and operating syst...
Polymorphic optional.
Definition polymorphic_optional.hpp:29
A wait-free multiple-producer/single-consumer fifo designed for absolute performance.
Definition wfree_fifo.hpp:30
auto take_one(Func &&func) noexcept
Take one message from the fifo slot.
Definition wfree_fifo.hpp:65
void take_all(Operation const &operation) noexcept
Take all message from the queue.
Definition wfree_fifo.hpp:81
bool empty() const noexcept
Check if fifo is empty.
Definition wfree_fifo.hpp:52
hi_force_inline auto emplace_and_invoke(Func &&func, Args &&...args) noexcept
Create an message in-place on the fifo.
Definition wfree_fifo.hpp:94