-
Notifications
You must be signed in to change notification settings - Fork 2
/
FastForward5.hpp
38 lines (34 loc) · 907 Bytes
/
FastForward5.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
#pragma once
#include <atomic>
#include <array>
#include <cstddef>
#include <new>
#include <type_traits>
template<typename T, std::size_t SIZE>
struct alignas(64) FastForward5 {
using value_type = T;
alignas(64) size_t tail{ 0 };
alignas(64) size_t head{ 0 };
alignas(64) std::array<std::atomic<T*>, SIZE> buffer;
int Enqueue(T* val) {
size_t t = tail;
auto& e = buffer[t];
if (e.load(std::memory_order_acquire)) return 0;
e.store(val, std::memory_order_release);
t += 1;
if (t == SIZE) t = 0;
tail = t;
return 1;
}
int Dequeue(T*& out) {
size_t h = head;
auto& e = buffer[h];
out = e.load(std::memory_order_acquire);
if (!out) return 0;
e.store(NULL, std::memory_order_release);
h += 1;
if (h == SIZE) h = 0;
head = h;
return 1;
}
};