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