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