HikoGUI
A low latency retained GUI
Loading...
Searching...
No Matches
wfree_fifo.hpp
1// Copyright Take Vos 2022.
2// Distributed under the Boost Software License, Version 1.0.
3// (See accompanying file LICENSE_1_0.txt or copy at https://www.boost.org/LICENSE_1_0.txt)
4
5#pragma once
6
7#include "utility/module.hpp"
8#include "counters.hpp"
9#include "polymorphic_optional.hpp"
10#include <type_traits>
11#include <concepts>
12#include <atomic>
13#include <memory>
14#include <array>
15
16hi_warning_push();
17// C26490: Don't use reinterpret_cast (type.1).
18// Implementing a container.
19hi_warning_ignore_msvc(26490);
20
21namespace hi::inline v1 {
22
33template<typename T, std::size_t SlotSize>
34class alignas(SlotSize) wfree_fifo {
35public:
36 static_assert(std::has_single_bit(SlotSize), "Only power-of-two number of messages size allowed.");
37 static_assert(SlotSize < 65536);
38
39 using value_type = T;
41
42 static constexpr std::size_t fifo_size = 65536;
43 static constexpr std::size_t slot_size = SlotSize;
44 static constexpr std::size_t num_slots = fifo_size / slot_size;
45
46 constexpr wfree_fifo() noexcept = default;
47 wfree_fifo(wfree_fifo const&) = delete;
48 wfree_fifo(wfree_fifo&&) = delete;
49 wfree_fifo& operator=(wfree_fifo const&) = delete;
50 wfree_fifo& operator=(wfree_fifo&&) = delete;
51
56 [[nodiscard]] bool empty() const noexcept
57 {
58 return _head.load(std::memory_order::relaxed) == _tail;
59 }
60
68 template<typename Func>
69 auto take_one(Func&& func) noexcept
70 {
71 auto result = get_slot(_tail).invoke_and_reset(std::forward<Func>(func));
72 if (result) {
73 _tail += slot_size;
74 }
75 return result;
76 }
77
84 template<typename Operation>
85 void take_all(Operation const& operation) noexcept
86 {
87 while (take_one(operation)) {}
88 }
89
97 template<typename Message, typename Func, typename... Args>
98 hi_force_inline auto emplace_and_invoke(Func&& func, Args&&...args) noexcept
99 {
100 // We need a new offset.
101 // - The offset is a byte index into 64kByte of memory.
102 // - Increment offset by the slot_size and the _head will overflow naturally
103 // at the end of the fifo.
104 // - We don't care about memory ordering with other writer threads. as
105 // each slot has an atomic for handling read/writer contention.
106 // - We don't have to check full/empty, this is done on the slot itself.
107 hilet offset = _head.fetch_add(slot_size, std::memory_order::relaxed);
108 return get_slot(offset).template wait_emplace_and_invoke<Message>(std::forward<Func>(func), std::forward<Args>(args)...);
109 }
110
111 template<typename Func, typename Object>
112 hi_force_inline auto insert_and_invoke(Func&& func, Object&& object) noexcept
113 {
114 return emplace_and_invoke<std::decay_t<Object>>(std::forward<Func>(func), std::forward<Object>(object));
115 }
116
117 template<typename Message, typename... Args>
118 hi_force_inline void emplace(Args&&...args) noexcept
119 {
120 return emplace_and_invoke<Message>([](Message&) -> void {}, std::forward<Args>(args)...);
121 }
122
123 template<typename Object>
124 hi_force_inline void insert(Object &&object) noexcept
125 {
126 return emplace<std::decay_t<Object>>(std::forward<Object>(object));
127 }
128
129private:
130 std::array<slot_type, num_slots> _slots = {}; // must be at offset 0
131 std::atomic<uint16_t> _head = 0;
133 uint16_t _tail = 0;
134
137 hi_force_inline slot_type& get_slot(uint16_t offset) noexcept
138 {
139 hi_axiom(offset % slot_size == 0);
140 // The head and tail are 16 bit offsets within the _slots, which are
141 return *std::launder(
142 std::assume_aligned<slot_size>(reinterpret_cast<slot_type *>(reinterpret_cast<char *>(this) + offset)));
143 }
144};
145
146} // namespace hi::inline v1
147
148hi_warning_pop();
#define hi_axiom(expression,...)
Specify an axiom; an expression that is true.
Definition assert.hpp:238
#define hilet
Invariant should be the default for variables.
Definition utility.hpp:23
DOXYGEN BUG.
Definition algorithm.hpp:13
Polymorphic optional.
Definition polymorphic_optional.hpp:39
A wait-free multiple-producer/single-consumer fifo designed for absolute performance.
Definition wfree_fifo.hpp:34
auto take_one(Func &&func) noexcept
Take one message from the fifo slot.
Definition wfree_fifo.hpp:69
void take_all(Operation const &operation) noexcept
Take all message from the queue.
Definition wfree_fifo.hpp:85
bool empty() const noexcept
Check if fifo is empty.
Definition wfree_fifo.hpp:56
hi_force_inline auto emplace_and_invoke(Func &&func, Args &&...args) noexcept
Create an message in-place on the fifo.
Definition wfree_fifo.hpp:98