forked from craflin/LockFreeQueue
-
Notifications
You must be signed in to change notification settings - Fork 0
/
LockFreeQueueSlow1.h
141 lines (129 loc) · 3.63 KB
/
LockFreeQueueSlow1.h
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
#pragma once
#include <nstd/Atomic.h>
#include <nstd/Memory.h>
template <typename T> class LockFreeQueueSlow1
{
public:
explicit LockFreeQueueSlow1(usize capacity)
{
_capacityMask = capacity - 1;
for(usize i = 1; i <= sizeof(void*) * 4; i <<= 1)
_capacityMask |= _capacityMask >> i;
_capacity = _capacityMask + 1;
_queue = (Node*)Memory::alloc(sizeof(Node) * _capacity);
for(Node* node = _queue, * end = _queue + _capacity; node < end; ++node)
node->state = Node::free;
_freeNodes = _capacity;
_occupiedNodes = 0;
_writeIndex = -1;
_safeWriteIndex = -1;
_readIndex = -1;
_safeReadIndex = -1;
}
~LockFreeQueueSlow1()
{
for(Node* node = _queue, * end = _queue + _capacity; node < end; ++node)
switch(node->state)
{
case Node::set:
case Node::occupied:
(&node->data)->~T();
break;
default:
break;
}
Memory::free(_queue);
}
usize capacity() const {return _capacity;}
usize size() const {return _capacity - _freeNodes;}
bool push(const T& data)
{
begin:
usize freeNodes = _freeNodes;
if(freeNodes == 0)
return false; // queue is full
if(Atomic::compareAndSwap(_freeNodes, freeNodes, freeNodes - 1) != freeNodes)
goto begin;
usize writeIndex = Atomic::increment(_writeIndex);
Node* node = &_queue[writeIndex & _capacityMask];
ASSERT(node->state == Node::free);
new (&node->data)T(data);
Atomic::store(node->state, Node::set);
commit:
usize safeWriteIndex = _safeWriteIndex;
usize nextSafeWriteIndex = safeWriteIndex + 1;
commitNext:
node = &_queue[nextSafeWriteIndex & _capacityMask];
if(node->state == Node::set && Atomic::compareAndSwap(node->state, Node::set, Node::occupied) == Node::set)
{
if(Atomic::compareAndSwap(_safeWriteIndex, safeWriteIndex, nextSafeWriteIndex) == safeWriteIndex)
{
Atomic::increment(_occupiedNodes);
safeWriteIndex = nextSafeWriteIndex;
++nextSafeWriteIndex;
goto commitNext;
}
else
node->state = Node::set;
goto commit;
}
return true;
}
bool pop(T& result)
{
begin:
usize occupiedNodes = _occupiedNodes;
if(occupiedNodes == 0)
return false; // queue is empty
if(Atomic::compareAndSwap(_occupiedNodes, occupiedNodes, occupiedNodes - 1) != occupiedNodes)
goto begin;
usize readIndex = Atomic::increment(_readIndex);
Node* node = &_queue[readIndex & _capacityMask];
ASSERT(node->state == Node::occupied);
result = node->data;
(&node->data)->~T();
Atomic::store(node->state, Node::unset);
release:
usize safeReadIndex = _safeReadIndex;
usize nextSafeReadIndex = safeReadIndex + 1;
releaseNext:
node = &_queue[nextSafeReadIndex & _capacityMask];
if(node->state == Node::unset && Atomic::compareAndSwap(node->state, Node::unset, Node::free) == Node::unset)
{
if(Atomic::compareAndSwap(_safeReadIndex, safeReadIndex, nextSafeReadIndex) == safeReadIndex)
{
Atomic::increment(_freeNodes);
safeReadIndex = nextSafeReadIndex;
++nextSafeReadIndex;
goto releaseNext;
}
else
node->state = Node::unset;
goto release;
}
return true;
}
private:
struct Node
{
T data;
enum State
{
free,
set,
occupied,
unset,
};
volatile int state;
};
private:
usize _capacityMask;
Node* _queue;
usize _capacity;
volatile usize _freeNodes;
volatile usize _occupiedNodes;
volatile usize _writeIndex;
volatile usize _safeWriteIndex;
volatile usize _readIndex;
volatile usize _safeReadIndex;
};