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