native-vector-store 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +201 -0
- package/README.md +223 -0
- package/binding.gyp +45 -0
- package/index.js +3 -0
- package/lib/index.d.ts +60 -0
- package/native-vector-store-0.1.0.tgz +0 -0
- package/package.json +52 -0
- package/prebuilds/darwin-arm64/native-vector-store.node +0 -0
- package/scripts/build-prebuilds.sh +23 -0
- package/src/atomic_queue.h +646 -0
- package/src/binding.cc +151 -0
- package/src/defs.h +107 -0
- package/src/mmap_file.h +159 -0
- package/src/vector_store.h +401 -0
- package/src/vector_store_loader.cpp +176 -0
- package/src/vector_store_loader.h +19 -0
- package/src/vector_store_loader_adaptive.cpp +220 -0
- package/src/vector_store_loader_mmap.cpp +154 -0
@@ -0,0 +1,646 @@
|
|
1
|
+
/* -*- mode: c++; c-basic-offset: 4; indent-tabs-mode: nil; tab-width: 4 -*- */
|
2
|
+
#ifndef ATOMIC_QUEUE_ATOMIC_QUEUE_H_INCLUDED
|
3
|
+
#define ATOMIC_QUEUE_ATOMIC_QUEUE_H_INCLUDED
|
4
|
+
|
5
|
+
// Copyright (c) 2019 Maxim Egorushkin. MIT License. See the full licence in file LICENSE.
|
6
|
+
|
7
|
+
#include "defs.h"
|
8
|
+
|
9
|
+
#include <algorithm>
|
10
|
+
#include <cassert>
|
11
|
+
#include <cstddef>
|
12
|
+
#include <cstdint>
|
13
|
+
#include <memory>
|
14
|
+
#include <utility>
|
15
|
+
|
16
|
+
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
17
|
+
|
18
|
+
namespace atomic_queue {
|
19
|
+
|
20
|
+
using std::uint32_t;
|
21
|
+
using std::uint64_t;
|
22
|
+
using std::uint8_t;
|
23
|
+
|
24
|
+
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
25
|
+
|
26
|
+
namespace details {
|
27
|
+
|
28
|
+
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
29
|
+
|
30
|
+
template<size_t elements_per_cache_line> struct GetCacheLineIndexBits { static int constexpr value = 0; };
|
31
|
+
template<> struct GetCacheLineIndexBits<256> { static int constexpr value = 8; };
|
32
|
+
template<> struct GetCacheLineIndexBits<128> { static int constexpr value = 7; };
|
33
|
+
template<> struct GetCacheLineIndexBits< 64> { static int constexpr value = 6; };
|
34
|
+
template<> struct GetCacheLineIndexBits< 32> { static int constexpr value = 5; };
|
35
|
+
template<> struct GetCacheLineIndexBits< 16> { static int constexpr value = 4; };
|
36
|
+
template<> struct GetCacheLineIndexBits< 8> { static int constexpr value = 3; };
|
37
|
+
template<> struct GetCacheLineIndexBits< 4> { static int constexpr value = 2; };
|
38
|
+
template<> struct GetCacheLineIndexBits< 2> { static int constexpr value = 1; };
|
39
|
+
|
40
|
+
template<bool minimize_contention, unsigned array_size, size_t elements_per_cache_line>
|
41
|
+
struct GetIndexShuffleBits {
|
42
|
+
static int constexpr bits = GetCacheLineIndexBits<elements_per_cache_line>::value;
|
43
|
+
static unsigned constexpr min_size = 1u << (bits * 2);
|
44
|
+
static int constexpr value = array_size < min_size ? 0 : bits;
|
45
|
+
};
|
46
|
+
|
47
|
+
template<unsigned array_size, size_t elements_per_cache_line>
|
48
|
+
struct GetIndexShuffleBits<false, array_size, elements_per_cache_line> {
|
49
|
+
static int constexpr value = 0;
|
50
|
+
};
|
51
|
+
|
52
|
+
// Multiple writers/readers contend on the same cache line when storing/loading elements at
|
53
|
+
// subsequent indexes, aka false sharing. For power of 2 ring buffer size it is possible to re-map
|
54
|
+
// the index in such a way that each subsequent element resides on another cache line, which
|
55
|
+
// minimizes contention. This is done by swapping the lowest order N bits (which are the index of
|
56
|
+
// the element within the cache line) with the next N bits (which are the index of the cache line)
|
57
|
+
// of the element index.
|
58
|
+
template<int BITS>
|
59
|
+
constexpr unsigned remap_index(unsigned index) noexcept {
|
60
|
+
unsigned constexpr mix_mask{(1u << BITS) - 1};
|
61
|
+
unsigned const mix{(index ^ (index >> BITS)) & mix_mask};
|
62
|
+
return index ^ mix ^ (mix << BITS);
|
63
|
+
}
|
64
|
+
|
65
|
+
template<>
|
66
|
+
constexpr unsigned remap_index<0>(unsigned index) noexcept {
|
67
|
+
return index;
|
68
|
+
}
|
69
|
+
|
70
|
+
template<int BITS, class T>
|
71
|
+
constexpr T& map(T* elements, unsigned index) noexcept {
|
72
|
+
return elements[remap_index<BITS>(index)];
|
73
|
+
}
|
74
|
+
|
75
|
+
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
76
|
+
|
77
|
+
// Implement a "bit-twiddling hack" for finding the next power of 2 in either 32 bits or 64 bits
|
78
|
+
// in C++11 compatible constexpr functions. The library no longer maintains C++11 compatibility.
|
79
|
+
|
80
|
+
// "Runtime" version for 32 bits
|
81
|
+
// --a;
|
82
|
+
// a |= a >> 1;
|
83
|
+
// a |= a >> 2;
|
84
|
+
// a |= a >> 4;
|
85
|
+
// a |= a >> 8;
|
86
|
+
// a |= a >> 16;
|
87
|
+
// ++a;
|
88
|
+
|
89
|
+
template<class T>
|
90
|
+
constexpr T decrement(T x) noexcept {
|
91
|
+
return x - 1;
|
92
|
+
}
|
93
|
+
|
94
|
+
template<class T>
|
95
|
+
constexpr T increment(T x) noexcept {
|
96
|
+
return x + 1;
|
97
|
+
}
|
98
|
+
|
99
|
+
template<class T>
|
100
|
+
constexpr T or_equal(T x, unsigned u) noexcept {
|
101
|
+
return x | x >> u;
|
102
|
+
}
|
103
|
+
|
104
|
+
template<class T, class... Args>
|
105
|
+
constexpr T or_equal(T x, unsigned u, Args... rest) noexcept {
|
106
|
+
return or_equal(or_equal(x, u), rest...);
|
107
|
+
}
|
108
|
+
|
109
|
+
constexpr uint32_t round_up_to_power_of_2(uint32_t a) noexcept {
|
110
|
+
return increment(or_equal(decrement(a), 1, 2, 4, 8, 16));
|
111
|
+
}
|
112
|
+
|
113
|
+
constexpr uint64_t round_up_to_power_of_2(uint64_t a) noexcept {
|
114
|
+
return increment(or_equal(decrement(a), 1, 2, 4, 8, 16, 32));
|
115
|
+
}
|
116
|
+
|
117
|
+
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
118
|
+
|
119
|
+
template<class T>
|
120
|
+
constexpr T nil() noexcept {
|
121
|
+
#if __cpp_lib_atomic_is_always_lock_free // Better compile-time error message requires C++17.
|
122
|
+
static_assert(std::atomic<T>::is_always_lock_free, "Queue element type T is not atomic. Use AtomicQueue2/AtomicQueueB2 for such element types.");
|
123
|
+
#endif
|
124
|
+
return {};
|
125
|
+
}
|
126
|
+
|
127
|
+
template<class T>
|
128
|
+
inline void destroy_n(T* p, unsigned n) noexcept {
|
129
|
+
for(auto q = p + n; p != q;)
|
130
|
+
(p++)->~T();
|
131
|
+
}
|
132
|
+
|
133
|
+
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
134
|
+
|
135
|
+
} // namespace details
|
136
|
+
|
137
|
+
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
138
|
+
|
139
|
+
template<class Derived>
|
140
|
+
class AtomicQueueCommon {
|
141
|
+
protected:
|
142
|
+
// Put these on different cache lines to avoid false sharing between readers and writers.
|
143
|
+
alignas(CACHE_LINE_SIZE) std::atomic<unsigned> head_ = {};
|
144
|
+
alignas(CACHE_LINE_SIZE) std::atomic<unsigned> tail_ = {};
|
145
|
+
|
146
|
+
// The special member functions are not thread-safe.
|
147
|
+
|
148
|
+
AtomicQueueCommon() noexcept = default;
|
149
|
+
|
150
|
+
AtomicQueueCommon(AtomicQueueCommon const& b) noexcept
|
151
|
+
: head_(b.head_.load(X))
|
152
|
+
, tail_(b.tail_.load(X)) {}
|
153
|
+
|
154
|
+
AtomicQueueCommon& operator=(AtomicQueueCommon const& b) noexcept {
|
155
|
+
head_.store(b.head_.load(X), X);
|
156
|
+
tail_.store(b.tail_.load(X), X);
|
157
|
+
return *this;
|
158
|
+
}
|
159
|
+
|
160
|
+
void swap(AtomicQueueCommon& b) noexcept {
|
161
|
+
unsigned h = head_.load(X);
|
162
|
+
unsigned t = tail_.load(X);
|
163
|
+
head_.store(b.head_.load(X), X);
|
164
|
+
tail_.store(b.tail_.load(X), X);
|
165
|
+
b.head_.store(h, X);
|
166
|
+
b.tail_.store(t, X);
|
167
|
+
}
|
168
|
+
|
169
|
+
template<class T, T NIL>
|
170
|
+
static T do_pop_atomic(std::atomic<T>& q_element) noexcept {
|
171
|
+
if(Derived::spsc_) {
|
172
|
+
for(;;) {
|
173
|
+
T element = q_element.load(A);
|
174
|
+
if(ATOMIC_QUEUE_LIKELY(element != NIL)) {
|
175
|
+
q_element.store(NIL, X);
|
176
|
+
return element;
|
177
|
+
}
|
178
|
+
if(Derived::maximize_throughput_)
|
179
|
+
spin_loop_pause();
|
180
|
+
}
|
181
|
+
}
|
182
|
+
else {
|
183
|
+
for(;;) {
|
184
|
+
T element = q_element.exchange(NIL, A); // (2) The store to wait for.
|
185
|
+
if(ATOMIC_QUEUE_LIKELY(element != NIL))
|
186
|
+
return element;
|
187
|
+
// Do speculative loads while busy-waiting to avoid broadcasting RFO messages.
|
188
|
+
do
|
189
|
+
spin_loop_pause();
|
190
|
+
while(Derived::maximize_throughput_ && q_element.load(X) == NIL);
|
191
|
+
}
|
192
|
+
}
|
193
|
+
}
|
194
|
+
|
195
|
+
template<class T, T NIL>
|
196
|
+
static void do_push_atomic(T element, std::atomic<T>& q_element) noexcept {
|
197
|
+
assert(element != NIL);
|
198
|
+
if(Derived::spsc_) {
|
199
|
+
while(ATOMIC_QUEUE_UNLIKELY(q_element.load(X) != NIL))
|
200
|
+
if(Derived::maximize_throughput_)
|
201
|
+
spin_loop_pause();
|
202
|
+
q_element.store(element, R);
|
203
|
+
}
|
204
|
+
else {
|
205
|
+
for(T expected = NIL; ATOMIC_QUEUE_UNLIKELY(!q_element.compare_exchange_weak(expected, element, R, X)); expected = NIL) {
|
206
|
+
do
|
207
|
+
spin_loop_pause(); // (1) Wait for store (2) to complete.
|
208
|
+
while(Derived::maximize_throughput_ && q_element.load(X) != NIL);
|
209
|
+
}
|
210
|
+
}
|
211
|
+
}
|
212
|
+
|
213
|
+
enum State : unsigned char { EMPTY, STORING, STORED, LOADING };
|
214
|
+
|
215
|
+
template<class T>
|
216
|
+
static T do_pop_any(std::atomic<unsigned char>& state, T& q_element) noexcept {
|
217
|
+
if(Derived::spsc_) {
|
218
|
+
while(ATOMIC_QUEUE_UNLIKELY(state.load(A) != STORED))
|
219
|
+
if(Derived::maximize_throughput_)
|
220
|
+
spin_loop_pause();
|
221
|
+
T element{std::move(q_element)};
|
222
|
+
state.store(EMPTY, R);
|
223
|
+
return element;
|
224
|
+
}
|
225
|
+
else {
|
226
|
+
for(;;) {
|
227
|
+
unsigned char expected = STORED;
|
228
|
+
if(ATOMIC_QUEUE_LIKELY(state.compare_exchange_weak(expected, LOADING, A, X))) {
|
229
|
+
T element{std::move(q_element)};
|
230
|
+
state.store(EMPTY, R);
|
231
|
+
return element;
|
232
|
+
}
|
233
|
+
// Do speculative loads while busy-waiting to avoid broadcasting RFO messages.
|
234
|
+
do
|
235
|
+
spin_loop_pause();
|
236
|
+
while(Derived::maximize_throughput_ && state.load(X) != STORED);
|
237
|
+
}
|
238
|
+
}
|
239
|
+
}
|
240
|
+
|
241
|
+
template<class U, class T>
|
242
|
+
static void do_push_any(U&& element, std::atomic<unsigned char>& state, T& q_element) noexcept {
|
243
|
+
if(Derived::spsc_) {
|
244
|
+
while(ATOMIC_QUEUE_UNLIKELY(state.load(A) != EMPTY))
|
245
|
+
if(Derived::maximize_throughput_)
|
246
|
+
spin_loop_pause();
|
247
|
+
q_element = std::forward<U>(element);
|
248
|
+
state.store(STORED, R);
|
249
|
+
}
|
250
|
+
else {
|
251
|
+
for(;;) {
|
252
|
+
unsigned char expected = EMPTY;
|
253
|
+
if(ATOMIC_QUEUE_LIKELY(state.compare_exchange_weak(expected, STORING, A, X))) {
|
254
|
+
q_element = std::forward<U>(element);
|
255
|
+
state.store(STORED, R);
|
256
|
+
return;
|
257
|
+
}
|
258
|
+
// Do speculative loads while busy-waiting to avoid broadcasting RFO messages.
|
259
|
+
do
|
260
|
+
spin_loop_pause();
|
261
|
+
while(Derived::maximize_throughput_ && state.load(X) != EMPTY);
|
262
|
+
}
|
263
|
+
}
|
264
|
+
}
|
265
|
+
|
266
|
+
public:
|
267
|
+
template<class T>
|
268
|
+
bool try_push(T&& element) noexcept {
|
269
|
+
auto head = head_.load(X);
|
270
|
+
if(Derived::spsc_) {
|
271
|
+
if(static_cast<int>(head - tail_.load(X)) >= static_cast<int>(static_cast<Derived&>(*this).size_))
|
272
|
+
return false;
|
273
|
+
head_.store(head + 1, X);
|
274
|
+
}
|
275
|
+
else {
|
276
|
+
do {
|
277
|
+
if(static_cast<int>(head - tail_.load(X)) >= static_cast<int>(static_cast<Derived&>(*this).size_))
|
278
|
+
return false;
|
279
|
+
} while(ATOMIC_QUEUE_UNLIKELY(!head_.compare_exchange_weak(head, head + 1, X, X))); // This loop is not FIFO.
|
280
|
+
}
|
281
|
+
|
282
|
+
static_cast<Derived&>(*this).do_push(std::forward<T>(element), head);
|
283
|
+
return true;
|
284
|
+
}
|
285
|
+
|
286
|
+
template<class T>
|
287
|
+
bool try_pop(T& element) noexcept {
|
288
|
+
auto tail = tail_.load(X);
|
289
|
+
if(Derived::spsc_) {
|
290
|
+
if(static_cast<int>(head_.load(X) - tail) <= 0)
|
291
|
+
return false;
|
292
|
+
tail_.store(tail + 1, X);
|
293
|
+
}
|
294
|
+
else {
|
295
|
+
do {
|
296
|
+
if(static_cast<int>(head_.load(X) - tail) <= 0)
|
297
|
+
return false;
|
298
|
+
} while(ATOMIC_QUEUE_UNLIKELY(!tail_.compare_exchange_weak(tail, tail + 1, X, X))); // This loop is not FIFO.
|
299
|
+
}
|
300
|
+
|
301
|
+
element = static_cast<Derived&>(*this).do_pop(tail);
|
302
|
+
return true;
|
303
|
+
}
|
304
|
+
|
305
|
+
template<class T>
|
306
|
+
void push(T&& element) noexcept {
|
307
|
+
unsigned head;
|
308
|
+
if(Derived::spsc_) {
|
309
|
+
head = head_.load(X);
|
310
|
+
head_.store(head + 1, X);
|
311
|
+
}
|
312
|
+
else {
|
313
|
+
constexpr auto memory_order = Derived::total_order_ ? std::memory_order_seq_cst : std::memory_order_relaxed;
|
314
|
+
head = head_.fetch_add(1, memory_order); // FIFO and total order on Intel regardless, as of 2019.
|
315
|
+
}
|
316
|
+
static_cast<Derived&>(*this).do_push(std::forward<T>(element), head);
|
317
|
+
}
|
318
|
+
|
319
|
+
auto pop() noexcept {
|
320
|
+
unsigned tail;
|
321
|
+
if(Derived::spsc_) {
|
322
|
+
tail = tail_.load(X);
|
323
|
+
tail_.store(tail + 1, X);
|
324
|
+
}
|
325
|
+
else {
|
326
|
+
constexpr auto memory_order = Derived::total_order_ ? std::memory_order_seq_cst : std::memory_order_relaxed;
|
327
|
+
tail = tail_.fetch_add(1, memory_order); // FIFO and total order on Intel regardless, as of 2019.
|
328
|
+
}
|
329
|
+
return static_cast<Derived&>(*this).do_pop(tail);
|
330
|
+
}
|
331
|
+
|
332
|
+
bool was_empty() const noexcept {
|
333
|
+
return !was_size();
|
334
|
+
}
|
335
|
+
|
336
|
+
bool was_full() const noexcept {
|
337
|
+
return was_size() >= capacity();
|
338
|
+
}
|
339
|
+
|
340
|
+
unsigned was_size() const noexcept {
|
341
|
+
// tail_ can be greater than head_ because of consumers doing pop, rather that try_pop, when the queue is empty.
|
342
|
+
return std::max(static_cast<int>(head_.load(X) - tail_.load(X)), 0);
|
343
|
+
}
|
344
|
+
|
345
|
+
unsigned capacity() const noexcept {
|
346
|
+
return static_cast<Derived const&>(*this).size_;
|
347
|
+
}
|
348
|
+
};
|
349
|
+
|
350
|
+
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
351
|
+
|
352
|
+
template<class T, unsigned SIZE, T NIL = details::nil<T>(), bool MINIMIZE_CONTENTION = true, bool MAXIMIZE_THROUGHPUT = true, bool TOTAL_ORDER = false, bool SPSC = false>
|
353
|
+
class AtomicQueue : public AtomicQueueCommon<AtomicQueue<T, SIZE, NIL, MINIMIZE_CONTENTION, MAXIMIZE_THROUGHPUT, TOTAL_ORDER, SPSC>> {
|
354
|
+
using Base = AtomicQueueCommon<AtomicQueue<T, SIZE, NIL, MINIMIZE_CONTENTION, MAXIMIZE_THROUGHPUT, TOTAL_ORDER, SPSC>>;
|
355
|
+
friend Base;
|
356
|
+
|
357
|
+
static constexpr unsigned size_ = MINIMIZE_CONTENTION ? details::round_up_to_power_of_2(SIZE) : SIZE;
|
358
|
+
static constexpr int SHUFFLE_BITS = details::GetIndexShuffleBits<MINIMIZE_CONTENTION, size_, CACHE_LINE_SIZE / sizeof(std::atomic<T>)>::value;
|
359
|
+
static constexpr bool total_order_ = TOTAL_ORDER;
|
360
|
+
static constexpr bool spsc_ = SPSC;
|
361
|
+
static constexpr bool maximize_throughput_ = MAXIMIZE_THROUGHPUT;
|
362
|
+
|
363
|
+
alignas(CACHE_LINE_SIZE) std::atomic<T> elements_[size_];
|
364
|
+
|
365
|
+
T do_pop(unsigned tail) noexcept {
|
366
|
+
std::atomic<T>& q_element = details::map<SHUFFLE_BITS>(elements_, tail % size_);
|
367
|
+
return Base::template do_pop_atomic<T, NIL>(q_element);
|
368
|
+
}
|
369
|
+
|
370
|
+
void do_push(T element, unsigned head) noexcept {
|
371
|
+
std::atomic<T>& q_element = details::map<SHUFFLE_BITS>(elements_, head % size_);
|
372
|
+
Base::template do_push_atomic<T, NIL>(element, q_element);
|
373
|
+
}
|
374
|
+
|
375
|
+
public:
|
376
|
+
using value_type = T;
|
377
|
+
|
378
|
+
AtomicQueue() noexcept {
|
379
|
+
assert(std::atomic<T>{NIL}.is_lock_free()); // Queue element type T is not atomic. Use AtomicQueue2/AtomicQueueB2 for such element types.
|
380
|
+
for(auto p = elements_, q = elements_ + size_; p != q; ++p)
|
381
|
+
p->store(NIL, X);
|
382
|
+
}
|
383
|
+
|
384
|
+
AtomicQueue(AtomicQueue const&) = delete;
|
385
|
+
AtomicQueue& operator=(AtomicQueue const&) = delete;
|
386
|
+
};
|
387
|
+
|
388
|
+
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
389
|
+
|
390
|
+
template<class T, unsigned SIZE, bool MINIMIZE_CONTENTION = true, bool MAXIMIZE_THROUGHPUT = true, bool TOTAL_ORDER = false, bool SPSC = false>
|
391
|
+
class AtomicQueue2 : public AtomicQueueCommon<AtomicQueue2<T, SIZE, MINIMIZE_CONTENTION, MAXIMIZE_THROUGHPUT, TOTAL_ORDER, SPSC>> {
|
392
|
+
using Base = AtomicQueueCommon<AtomicQueue2<T, SIZE, MINIMIZE_CONTENTION, MAXIMIZE_THROUGHPUT, TOTAL_ORDER, SPSC>>;
|
393
|
+
using State = typename Base::State;
|
394
|
+
friend Base;
|
395
|
+
|
396
|
+
static constexpr unsigned size_ = MINIMIZE_CONTENTION ? details::round_up_to_power_of_2(SIZE) : SIZE;
|
397
|
+
static constexpr int SHUFFLE_BITS = details::GetIndexShuffleBits<MINIMIZE_CONTENTION, size_, CACHE_LINE_SIZE / sizeof(State)>::value;
|
398
|
+
static constexpr bool total_order_ = TOTAL_ORDER;
|
399
|
+
static constexpr bool spsc_ = SPSC;
|
400
|
+
static constexpr bool maximize_throughput_ = MAXIMIZE_THROUGHPUT;
|
401
|
+
|
402
|
+
alignas(CACHE_LINE_SIZE) std::atomic<unsigned char> states_[size_] = {};
|
403
|
+
alignas(CACHE_LINE_SIZE) T elements_[size_] = {};
|
404
|
+
|
405
|
+
T do_pop(unsigned tail) noexcept {
|
406
|
+
unsigned index = details::remap_index<SHUFFLE_BITS>(tail % size_);
|
407
|
+
return Base::do_pop_any(states_[index], elements_[index]);
|
408
|
+
}
|
409
|
+
|
410
|
+
template<class U>
|
411
|
+
void do_push(U&& element, unsigned head) noexcept {
|
412
|
+
unsigned index = details::remap_index<SHUFFLE_BITS>(head % size_);
|
413
|
+
Base::do_push_any(std::forward<U>(element), states_[index], elements_[index]);
|
414
|
+
}
|
415
|
+
|
416
|
+
public:
|
417
|
+
using value_type = T;
|
418
|
+
|
419
|
+
AtomicQueue2() noexcept = default;
|
420
|
+
AtomicQueue2(AtomicQueue2 const&) = delete;
|
421
|
+
AtomicQueue2& operator=(AtomicQueue2 const&) = delete;
|
422
|
+
};
|
423
|
+
|
424
|
+
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
425
|
+
|
426
|
+
template<class T, class A = std::allocator<T>, T NIL = details::nil<T>(), bool MAXIMIZE_THROUGHPUT = true, bool TOTAL_ORDER = false, bool SPSC = false>
|
427
|
+
class AtomicQueueB : private std::allocator_traits<A>::template rebind_alloc<std::atomic<T>>,
|
428
|
+
public AtomicQueueCommon<AtomicQueueB<T, A, NIL, MAXIMIZE_THROUGHPUT, TOTAL_ORDER, SPSC>> {
|
429
|
+
using AllocatorElements = typename std::allocator_traits<A>::template rebind_alloc<std::atomic<T>>;
|
430
|
+
using Base = AtomicQueueCommon<AtomicQueueB<T, A, NIL, MAXIMIZE_THROUGHPUT, TOTAL_ORDER, SPSC>>;
|
431
|
+
friend Base;
|
432
|
+
|
433
|
+
static constexpr bool total_order_ = TOTAL_ORDER;
|
434
|
+
static constexpr bool spsc_ = SPSC;
|
435
|
+
static constexpr bool maximize_throughput_ = MAXIMIZE_THROUGHPUT;
|
436
|
+
|
437
|
+
static constexpr auto ELEMENTS_PER_CACHE_LINE = CACHE_LINE_SIZE / sizeof(std::atomic<T>);
|
438
|
+
static_assert(ELEMENTS_PER_CACHE_LINE, "Unexpected ELEMENTS_PER_CACHE_LINE.");
|
439
|
+
|
440
|
+
static constexpr auto SHUFFLE_BITS = details::GetCacheLineIndexBits<ELEMENTS_PER_CACHE_LINE>::value;
|
441
|
+
static_assert(SHUFFLE_BITS, "Unexpected SHUFFLE_BITS.");
|
442
|
+
|
443
|
+
// AtomicQueueCommon members are stored into by readers and writers.
|
444
|
+
// Allocate these immutable members on another cache line which never gets invalidated by stores.
|
445
|
+
alignas(CACHE_LINE_SIZE) unsigned size_;
|
446
|
+
std::atomic<T>* elements_;
|
447
|
+
|
448
|
+
T do_pop(unsigned tail) noexcept {
|
449
|
+
std::atomic<T>& q_element = details::map<SHUFFLE_BITS>(elements_, tail & (size_ - 1));
|
450
|
+
return Base::template do_pop_atomic<T, NIL>(q_element);
|
451
|
+
}
|
452
|
+
|
453
|
+
void do_push(T element, unsigned head) noexcept {
|
454
|
+
std::atomic<T>& q_element = details::map<SHUFFLE_BITS>(elements_, head & (size_ - 1));
|
455
|
+
Base::template do_push_atomic<T, NIL>(element, q_element);
|
456
|
+
}
|
457
|
+
|
458
|
+
public:
|
459
|
+
using value_type = T;
|
460
|
+
using allocator_type = A;
|
461
|
+
|
462
|
+
// The special member functions are not thread-safe.
|
463
|
+
|
464
|
+
AtomicQueueB(unsigned size, A const& allocator = A{})
|
465
|
+
: AllocatorElements(allocator)
|
466
|
+
, size_(std::max(details::round_up_to_power_of_2(size), 1u << (SHUFFLE_BITS * 2)))
|
467
|
+
, elements_(AllocatorElements::allocate(size_)) {
|
468
|
+
assert(std::atomic<T>{NIL}.is_lock_free()); // Queue element type T is not atomic. Use AtomicQueue2/AtomicQueueB2 for such element types.
|
469
|
+
std::uninitialized_fill_n(elements_, size_, NIL);
|
470
|
+
assert(get_allocator() == allocator); // The standard requires the original and rebound allocators to manage the same state.
|
471
|
+
}
|
472
|
+
|
473
|
+
AtomicQueueB(AtomicQueueB&& b) noexcept
|
474
|
+
: AllocatorElements(static_cast<AllocatorElements&&>(b)) // TODO: This must be noexcept, static_assert that.
|
475
|
+
, Base(static_cast<Base&&>(b))
|
476
|
+
, size_(std::exchange(b.size_, 0))
|
477
|
+
, elements_(std::exchange(b.elements_, nullptr))
|
478
|
+
{}
|
479
|
+
|
480
|
+
AtomicQueueB& operator=(AtomicQueueB&& b) noexcept {
|
481
|
+
b.swap(*this);
|
482
|
+
return *this;
|
483
|
+
}
|
484
|
+
|
485
|
+
~AtomicQueueB() noexcept {
|
486
|
+
if(elements_) {
|
487
|
+
details::destroy_n(elements_, size_);
|
488
|
+
AllocatorElements::deallocate(elements_, size_); // TODO: This must be noexcept, static_assert that.
|
489
|
+
}
|
490
|
+
}
|
491
|
+
|
492
|
+
A get_allocator() const noexcept {
|
493
|
+
return *this; // The standard requires implicit conversion between rebound allocators.
|
494
|
+
}
|
495
|
+
|
496
|
+
void swap(AtomicQueueB& b) noexcept {
|
497
|
+
using std::swap;
|
498
|
+
swap(static_cast<AllocatorElements&>(*this), static_cast<AllocatorElements&>(b));
|
499
|
+
Base::swap(b);
|
500
|
+
swap(size_, b.size_);
|
501
|
+
swap(elements_, b.elements_);
|
502
|
+
}
|
503
|
+
|
504
|
+
friend void swap(AtomicQueueB& a, AtomicQueueB& b) noexcept {
|
505
|
+
a.swap(b);
|
506
|
+
}
|
507
|
+
};
|
508
|
+
|
509
|
+
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
510
|
+
|
511
|
+
template<class T, class A = std::allocator<T>, bool MAXIMIZE_THROUGHPUT = true, bool TOTAL_ORDER = false, bool SPSC = false>
|
512
|
+
class AtomicQueueB2 : private std::allocator_traits<A>::template rebind_alloc<unsigned char>,
|
513
|
+
public AtomicQueueCommon<AtomicQueueB2<T, A, MAXIMIZE_THROUGHPUT, TOTAL_ORDER, SPSC>> {
|
514
|
+
using StorageAllocator = typename std::allocator_traits<A>::template rebind_alloc<unsigned char>;
|
515
|
+
using Base = AtomicQueueCommon<AtomicQueueB2<T, A, MAXIMIZE_THROUGHPUT, TOTAL_ORDER, SPSC>>;
|
516
|
+
using State = typename Base::State;
|
517
|
+
using AtomicState = std::atomic<unsigned char>;
|
518
|
+
friend Base;
|
519
|
+
|
520
|
+
static constexpr bool total_order_ = TOTAL_ORDER;
|
521
|
+
static constexpr bool spsc_ = SPSC;
|
522
|
+
static constexpr bool maximize_throughput_ = MAXIMIZE_THROUGHPUT;
|
523
|
+
|
524
|
+
// AtomicQueueCommon members are stored into by readers and writers.
|
525
|
+
// Allocate these immutable members on another cache line which never gets invalidated by stores.
|
526
|
+
alignas(CACHE_LINE_SIZE) unsigned size_;
|
527
|
+
AtomicState* states_;
|
528
|
+
T* elements_;
|
529
|
+
|
530
|
+
static constexpr auto STATES_PER_CACHE_LINE = CACHE_LINE_SIZE / sizeof(AtomicState);
|
531
|
+
static_assert(STATES_PER_CACHE_LINE, "Unexpected STATES_PER_CACHE_LINE.");
|
532
|
+
|
533
|
+
static constexpr auto SHUFFLE_BITS = details::GetCacheLineIndexBits<STATES_PER_CACHE_LINE>::value;
|
534
|
+
static_assert(SHUFFLE_BITS, "Unexpected SHUFFLE_BITS.");
|
535
|
+
|
536
|
+
T do_pop(unsigned tail) noexcept {
|
537
|
+
unsigned index = details::remap_index<SHUFFLE_BITS>(tail & (size_ - 1));
|
538
|
+
return Base::do_pop_any(states_[index], elements_[index]);
|
539
|
+
}
|
540
|
+
|
541
|
+
template<class U>
|
542
|
+
void do_push(U&& element, unsigned head) noexcept {
|
543
|
+
unsigned index = details::remap_index<SHUFFLE_BITS>(head & (size_ - 1));
|
544
|
+
Base::do_push_any(std::forward<U>(element), states_[index], elements_[index]);
|
545
|
+
}
|
546
|
+
|
547
|
+
template<class U>
|
548
|
+
U* allocate_() {
|
549
|
+
U* p = reinterpret_cast<U*>(StorageAllocator::allocate(size_ * sizeof(U)));
|
550
|
+
assert(reinterpret_cast<uintptr_t>(p) % alignof(U) == 0); // Allocated storage must be suitably aligned for U.
|
551
|
+
return p;
|
552
|
+
}
|
553
|
+
|
554
|
+
template<class U>
|
555
|
+
void deallocate_(U* p) noexcept {
|
556
|
+
StorageAllocator::deallocate(reinterpret_cast<unsigned char*>(p), size_ * sizeof(U)); // TODO: This must be noexcept, static_assert that.
|
557
|
+
}
|
558
|
+
|
559
|
+
public:
|
560
|
+
using value_type = T;
|
561
|
+
using allocator_type = A;
|
562
|
+
|
563
|
+
// The special member functions are not thread-safe.
|
564
|
+
|
565
|
+
AtomicQueueB2(unsigned size, A const& allocator = A{})
|
566
|
+
: StorageAllocator(allocator)
|
567
|
+
, size_(std::max(details::round_up_to_power_of_2(size), 1u << (SHUFFLE_BITS * 2)))
|
568
|
+
, states_(allocate_<AtomicState>())
|
569
|
+
, elements_(allocate_<T>()) {
|
570
|
+
std::uninitialized_fill_n(states_, size_, Base::EMPTY);
|
571
|
+
A a = get_allocator();
|
572
|
+
assert(a == allocator); // The standard requires the original and rebound allocators to manage the same state.
|
573
|
+
for(auto p = elements_, q = elements_ + size_; p < q; ++p)
|
574
|
+
std::allocator_traits<A>::construct(a, p);
|
575
|
+
}
|
576
|
+
|
577
|
+
AtomicQueueB2(AtomicQueueB2&& b) noexcept
|
578
|
+
: StorageAllocator(static_cast<StorageAllocator&&>(b)) // TODO: This must be noexcept, static_assert that.
|
579
|
+
, Base(static_cast<Base&&>(b))
|
580
|
+
, size_(std::exchange(b.size_, 0))
|
581
|
+
, states_(std::exchange(b.states_, nullptr))
|
582
|
+
, elements_(std::exchange(b.elements_, nullptr))
|
583
|
+
{}
|
584
|
+
|
585
|
+
AtomicQueueB2& operator=(AtomicQueueB2&& b) noexcept {
|
586
|
+
b.swap(*this);
|
587
|
+
return *this;
|
588
|
+
}
|
589
|
+
|
590
|
+
~AtomicQueueB2() noexcept {
|
591
|
+
if(elements_) {
|
592
|
+
A a = get_allocator();
|
593
|
+
for(auto p = elements_, q = elements_ + size_; p < q; ++p)
|
594
|
+
std::allocator_traits<A>::destroy(a, p);
|
595
|
+
deallocate_(elements_);
|
596
|
+
details::destroy_n(states_, size_);
|
597
|
+
deallocate_(states_);
|
598
|
+
}
|
599
|
+
}
|
600
|
+
|
601
|
+
A get_allocator() const noexcept {
|
602
|
+
return *this; // The standard requires implicit conversion between rebound allocators.
|
603
|
+
}
|
604
|
+
|
605
|
+
void swap(AtomicQueueB2& b) noexcept {
|
606
|
+
using std::swap;
|
607
|
+
swap(static_cast<StorageAllocator&>(*this), static_cast<StorageAllocator&>(b));
|
608
|
+
Base::swap(b);
|
609
|
+
swap(size_, b.size_);
|
610
|
+
swap(states_, b.states_);
|
611
|
+
swap(elements_, b.elements_);
|
612
|
+
}
|
613
|
+
|
614
|
+
friend void swap(AtomicQueueB2& a, AtomicQueueB2& b) noexcept {
|
615
|
+
a.swap(b);
|
616
|
+
}
|
617
|
+
};
|
618
|
+
|
619
|
+
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
620
|
+
|
621
|
+
template<class Queue>
|
622
|
+
struct RetryDecorator : Queue {
|
623
|
+
using T = typename Queue::value_type;
|
624
|
+
|
625
|
+
using Queue::Queue;
|
626
|
+
|
627
|
+
void push(T element) noexcept {
|
628
|
+
while(!this->try_push(element))
|
629
|
+
spin_loop_pause();
|
630
|
+
}
|
631
|
+
|
632
|
+
T pop() noexcept {
|
633
|
+
T element;
|
634
|
+
while(!this->try_pop(element))
|
635
|
+
spin_loop_pause();
|
636
|
+
return element;
|
637
|
+
}
|
638
|
+
};
|
639
|
+
|
640
|
+
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
641
|
+
|
642
|
+
} // namespace atomic_queue
|
643
|
+
|
644
|
+
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
645
|
+
|
646
|
+
#endif // ATOMIC_QUEUE_ATOMIC_QUEUE_H_INCLUDED
|