-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathring_buffers.cpp
More file actions
168 lines (121 loc) · 4.34 KB
/
Copy pathring_buffers.cpp
File metadata and controls
168 lines (121 loc) · 4.34 KB
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
#include <array>
#include <atomic>
#include <optional>
template <typename T, size_t Capacity> class SCSPring {
private:
static constexpr size_t increment(size_t i) { return (i + 1) % Capacity; }
alignas(64) std::array<T, Capacity> buffer;
alignas(64) std::atomic<size_t> head{0};
alignas(64) std::atomic<size_t> tail{0};
public:
bool push(const T& item) {
size_t curr_tail = tail.load(std::memory_order_relaxed);
size_t next = increment(curr_tail);
size_t curr_head = head.load(std::memory_order_acquire);
if (next == curr_head) {
return false;
}
buffer[next] == item;
tail.store(next, std::memory_order_release);
return true;
}
T pop() {
size_t curr_head = head.load(std::memory_order_relaxed);
size_t curr_tail = tail.load(std::memory_order_acquire);
if (curr_head == curr_tail) {
return std::nullopt;
}
head.store(increment(head), std::memory_order_release);
return buffer[curr_head];
}
};
// not strictly serialziable
template <typename T, size_t Capacity> class MPMCRing {
private:
static_assert(Capacity > 0);
struct Slot {
T item{};
std::atomic<size_t> published_seq{0};
std::atomic<size_t> done_seq{0};
};
static constexpr size_t index(std::size_t pos) { return pos % Capacity; }
alignas(64) std::array<Slot, Capacity> buffer{};
alignas(64) std::atomic<std::size_t> write_claim_pos{0};
alignas(64) std::atomic<std::size_t> publish_pos{0};
alignas(64) std::atomic<std::size_t> read_claim_pos{0};
alignas(64) std::atomic<std::size_t> reclaim_pos{0};
void try_advance_publish() {
size_t p = publish_pos.load(std::memory_order_relaxed);
while (true) {
Slot& s = buffer[index(p)];
if (s.published_seq.load(std::memory_order_acquire) != p + 1) {
return;
}
std::size_t expected = p;
if (publish_pos.compare_exchange_weak(expected, p + 1, std::memory_order_release,
std::memory_order_relaxed)) {
p++;
} else {
p = expected;
}
}
}
void try_advance_reclaim() {
std::size_t r = reclaim_pos.load(std::memory_order_relaxed);
while (true) {
Slot& s = buffer[index(r)];
if (s.done_seq.load(std::memory_order_acquire) != r + 1) {
return;
}
std::size_t expected = r;
if (reclaim_pos.compare_exchange_weak(expected, r + 1, std::memory_order_release,
std::memory_order_relaxed)) {
r++;
} else {
r = expected;
}
}
}
public:
bool try_push(const T& item) {
std::size_t p;
while (true) {
p = write_claim_pos.load(std::memory_order_relaxed);
std::size_t r = reclaim_pos.load(std::memory_order_acquire);
if (p - r >= Capacity) {
return false;
}
if (write_claim_pos.compare_exchange_weak(p, p + 1, std::memory_order_acq_rel,
std::memory_order_relaxed)) {
break;
}
}
Slot& s = buffer[index(p)];
s.item = item;
s.published_seq.store(p + 1, std::memory_order_release);
try_advance_publish();
return true;
}
std::optional<T> try_pop() {
std::size_t c;
while (true) {
c = read_claim_pos.load(std::memory_order_relaxed);
std::size_t pub = publish_pos.load(std::memory_order_acquire);
if (c >= pub) {
return std::nullopt;
}
if (read_claim_pos.compare_exchange_weak(c, c + 1, std::memory_order_acq_rel,
std::memory_order_relaxed)) {
break;
}
}
Slot& s = buffer[index(c)];
if (s.published_seq.load(std::memory_order_acquire) != c + 1) {
return std::nullopt;
}
T value = s.item;
s.done_seq.store(c + 1, std::memory_order_release);
try_advance_reclaim();
return value;
}
};