Repository navigation
Expand file tree
/
Copy pathLFSPSCQueueSimple.h
More file actions
63 lines (59 loc) · 1.68 KB
/
Copy pathLFSPSCQueueSimple.h
File metadata and controls
63 lines (59 loc) · 1.68 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
#ifndef DYNA_WFSPSCQ_H
#define DYNA_WFSPSCQ_H
#include <atomic>
template<typename T>
class LFSPSCQueue {
static const size_t CACHE_LINE_SIZE = 64;
struct RingBuffer
{
T * ring_;
char padding0[CACHE_LINE_SIZE - sizeof(T *)];
std::atomic<size_t> head;
char padding1[CACHE_LINE_SIZE - sizeof(std::atomic<size_t>)];
std::atomic<size_t> tail;
char padding2[CACHE_LINE_SIZE - sizeof(std::atomic<size_t>)];
};
public:
LFSPSCQueue(size_t buffer_size) : buffer_mask_(buffer_size - 1)
{
lane.ring_ = new T[buffer_size];
lane.tail = 0;
lane.head = 0;
}
~LFSPSCQueue()
{
delete[] lane.ring_;
}
bool push(const T & value)
{
size_t head = lane.head.load(std::memory_order_relaxed);
size_t next_head = next(head);
if (next_head != lane.tail.load(std::memory_order_acquire))
{
lane.ring_[head] = std::move(value);
lane.head.store(next_head, std::memory_order_release);
return true;
}
return false;
}
bool pop(T & value)
{
size_t tail = lane.tail.load(std::memory_order_relaxed);
if (tail != lane.head.load(std::memory_order_acquire))
{
value = std::move(lane.ring_[tail]);
lane.tail.store(next(tail), std::memory_order_release);
return true;
}
return false;
}
private:
inline size_t next(size_t current)
{
return (current + 1) & buffer_mask_;
}
RingBuffer lane; // lanes are cache line aligned
size_t const buffer_mask_;
char padding1[CACHE_LINE_SIZE - sizeof(size_t)];
};
#endif //DYNA_WFSPSCQ_H