-
Notifications
You must be signed in to change notification settings - Fork 0
/
ringbuffer.hpp
168 lines (141 loc) · 4.47 KB
/
ringbuffer.hpp
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
#ifndef DBSTD_RINGBUFFER
#define DBSTD_RINGBUFFER
#include <memory>
#include <optional>
#include <bit>
#include <thread>
#include <new>
// idk gcc on my machine setup didn't define the necessary macros
namespace std {
inline constexpr size_t hardware_destructive_interference_size = 256;
inline constexpr size_t hardware_constructive_interference_size = 256;
}
namespace dbstd {
// Lock-free thread safe single producer single consumer queue
template<typename T, typename Allocator=std::allocator<T>>
class RingBuffer {
private:
Allocator mAlloc;
size_t mCapacity;
alignas(std::hardware_destructive_interference_size) std::atomic<size_t> mSize;
alignas(std::hardware_destructive_interference_size) std::atomic<size_t> mHeadIdx;
T* mBacking;
void freeBacking() {
size_t end = mHeadIdx + mSize;
for (size_t idx = mHeadIdx; idx < end; ++idx)
(mBacking + (idx % mCapacity))->~T();
mAlloc.deallocate(mBacking, mCapacity);
}
public:
// capacity will be closest larger power of 2
RingBuffer(size_t minimumCapacity, const Allocator& alloc=std::allocator<T>())
: mAlloc(alloc)
, mCapacity(std::bit_ceil(minimumCapacity))
, mSize(0)
, mHeadIdx(0)
, mBacking(mAlloc.allocate(mCapacity))
{}
RingBuffer(const RingBuffer& other) = delete;
RingBuffer& operator=(const RingBuffer& other) = delete;
RingBuffer(RingBuffer&& other) noexcept
: mAlloc(other.mAlloc)
, mCapacity(other.mCapacity)
, mSize(other.mSize)
, mHeadIdx(other.mHeadIdx)
, mBacking(other.mBacking) {
other.mBacking = nullptr;
other.head = nullptr;
}
RingBuffer& operator=(RingBuffer&& other) noexcept {
if (this != &other) {
freeBacking();
mAlloc = (other.mAlloc);
mCapacity = other.mCapacity;
mSize = other.mSize;
mBacking = other.mBacking;
mHeadIdx = other.mHeadIdx;
other.mBacking = nullptr;
other.head = nullptr;
}
return *this;
}
~RingBuffer() { freeBacking(); }
template <typename ...Args>
bool enqueue(Args&&... args) {
if (mSize == mCapacity) {
return false;
}
size_t idx = mHeadIdx + mSize;
new
(mBacking + (idx & (mCapacity - 1)))
T(std::forward<Args>(args)...);
++mSize;
return true;
}
template <typename ...Args>
void blocking_enqueue(Args&&... args) {
while (mSize == mCapacity) {}
size_t idx = mHeadIdx + mSize;
new
(mBacking + (idx & (mCapacity - 1)))
T(std::forward<Args>(args)...);
++mSize;
}
template <typename ...Args>
void blocking_sleeping_enqueue(int sleepNano, Args&&... args) {
while (mSize == mCapacity) {
std::this_thread::sleep_for(std::chrono::milliseconds(sleepNano));
}
size_t idx = mHeadIdx + mSize;
new
(mBacking + (idx & (mCapacity - 1)))
T(std::forward<Args>(args)...);
++mSize;
}
template <typename ...Args>
void unchecked_enqueue(Args&&... args) {
size_t idx = mHeadIdx + mSize;
new
(mBacking + (idx & (mCapacity - 1)))
T(std::forward<Args>(args)...);
++mSize;
}
bool dequeue() {
if (mSize > 0) {
(mBacking + mHeadIdx)->~T();
mHeadIdx = (mHeadIdx + 1) & (mCapacity - 1);
--mSize;
return true;
}
return false;
}
void unchecked_dequeue() {
(mBacking + mHeadIdx)->~T();
mHeadIdx = (mHeadIdx + 1) & (mCapacity - 1);
--mSize;
}
std::optional<T> dequeue_and_get() {
std::optional<T> result = std::nullopt;
if (mSize > 0) {
result.emplace(std::move(*(mBacking+mHeadIdx)));
(mBacking + mHeadIdx)->~T();
mHeadIdx = (mHeadIdx + 1) & (mCapacity - 1);
--mSize;
}
return result;
}
T unchecked_dequeue_and_get() {
T result = std::move(*(mBacking+mHeadIdx));
(mBacking + mHeadIdx)->~T();
mHeadIdx = (mHeadIdx + 1) & (mCapacity - 1);
--mSize;
return result;
}
T& front() { return *(mBacking + mHeadIdx); }
const T& front() const { return *(mBacking + mHeadIdx); }
bool empty() const { return mSize == 0; }
size_t size() const { return mSize; }
size_t capacity() const { return mCapacity; }
};
}
#endif