-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspsc_queue.h
176 lines (156 loc) · 4.94 KB
/
spsc_queue.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
/*
MIT License
Copyright (c) 2024 Marcus Spangenberg
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#pragma once
#include <atomic>
#ifdef _WIN32
#include <malloc.h>
#endif
#include <algorithm>
#include <cassert>
#include <csignal>
#include <cstddef>
#include <cstdlib>
#include <cstring>
#include <new>
#include <type_traits>
#include <utility>
namespace waitfree
{
/**
* Single header, wait-free, single producer, single consumer queue with possibility
* to query queue size.
*
* T is the type of the elements in the queue.
* S is the maximum number of elements in the queue. S must be a power of 2.
*/
template<typename T, size_t S>
class spsc_queue
{
public:
spsc_queue()
: size_(0),
head_(0),
tail_(0)
{
static_assert(is_power_of_two(S));
constexpr auto alignment = std::max(alignof(T), sizeof(void*));
constexpr auto adjusted_size = round_up_to_multiple_of(sizeof(T) * S, alignment);
#ifdef _WIN32
auto alloc_result = _aligned_malloc(adjusted_size, alignment);
#else
auto alloc_result = aligned_alloc(alignment, adjusted_size);
#endif
if (!alloc_result)
{
throw std::bad_alloc();
}
memset(alloc_result, 0, adjusted_size);
elements_ = reinterpret_cast<T*>(alloc_result);
}
~spsc_queue()
{
if constexpr (!std::is_trivially_destructible_v<T>)
{
const auto size = size_.load();
for (size_t i = 0; i < size; ++i)
{
const auto head = (head_ + i) & mod_value_;
(&elements_[head])->~T();
}
}
#ifdef _WIN32
_aligned_free(elements_);
#else
free(elements_);
#endif
}
/**
* @brief Pushes an item to the queue.
*
* Not thread safe with regards to other push operations, thread safe with regards to pop operations.
*/
template<typename... U>
void push(U&&... item) noexcept
{
const auto tail = tail_;
tail_ = (tail_ + 1) & mod_value_;
new (&elements_[tail]) T(std::forward<U>(item)...);
[[maybe_unused]] const auto oldValue = size_.fetch_add(1, std::memory_order_acq_rel);
assert(oldValue < S);
}
/**
* @brief Pops an item from the queue.
*
* @details
* Returns false if the queue is empty, otherwise true. item is only valid if the function returns true.
*
* Not thread safe with regards to other pop operations, thread safe with regards to push operations.
*/
bool pop(T& item) noexcept
{
if (size_.load(std::memory_order_acquire) == 0)
{
return false;
}
const auto head = head_;
head_ = (head_ + 1) & mod_value_;
if constexpr (std::is_move_assignable_v<T>)
{
item = std::move(elements_[head]);
}
else
{
item = elements_[head];
if constexpr (!std::is_trivially_destructible_v<T>)
{
(&elements_[head])->~T();
}
}
size_.fetch_sub(1, std::memory_order_acq_rel);
return true;
}
/**
* @brief Get the current size of the queue.
*
* Thread safe with regards to push and pop operations.
*/
[[nodiscard]] size_t size() const noexcept
{
return size_.load(std::memory_order_acquire);
}
private:
static constexpr size_t cacheLine_size_ = 64;
static constexpr uint32_t mod_value_ = S - 1;
T* elements_;
std::atomic<size_t> size_;
alignas(cacheLine_size_) size_t head_;
alignas(cacheLine_size_) size_t tail_;
static constexpr bool is_power_of_two(const size_t size)
{
return (size & (size - 1)) == 0;
}
static constexpr size_t round_up_to_multiple_of(const size_t size, const size_t multiple)
{
const auto remaining = size % multiple;
const auto adjusted_size = remaining == 0 ? size : size + (multiple - remaining);
return adjusted_size;
}
};
}// namespace waitfree