-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathqueue.d
More file actions
240 lines (204 loc) · 5.19 KB
/
queue.d
File metadata and controls
240 lines (204 loc) · 5.19 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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
module util.queue;
import std.traits: hasIndirections;
import core.atomic;
synchronized class GrowableCircularQueue(T)
{
private size_t _length;
private size_t first, last;
private T[] A = [T.init];
this(T[] items...) pure nothrow @safe
{
foreach (x; items)
push(x);
}
@property size_t length() const pure nothrow @safe @nogc
{
return _length;
}
@property bool empty() const pure nothrow @safe @nogc
{
return _length == 0;
}
@property T front() pure nothrow @safe @nogc
{
assert(length != 0);
return A[first];
}
//; Returns true if queue was not empty
bool clear() pure nothrow @safe @nogc
{
bool startedAsEmpty = empty;
while (!empty)
popFront();
return !startedAsEmpty;
}
T opIndex(in size_t i) pure nothrow @safe @nogc
{
assert(i < length);
return A[(first + i) & (A.length - 1)];
}
void push(T item) pure nothrow @safe
{
size_t l = last;
if (length >= A.length)
{ // Double the queue.
immutable oldALen = A.length;
A.length *= 2;
if (last < first)
{
A[oldALen .. oldALen + l + 1] = A[0 .. l + 1];
static if (hasIndirections!T)
A[0 .. last + 1] = T.init; // Help for the GC.
// core.atomic.atomicOp!"+="(last, oldALen);
last = l + oldALen;
l = last;
//last += oldALen;
}
}
last = (l + 1) & (A.length - 1);
A[last] = item;
_length = _length + 1;
}
T popFront() pure nothrow @safe @nogc
{
assert(length != 0);
auto saved = A[first];
static if (hasIndirections!T)
A[first] = T.init; // Help for the GC.
first = (first + 1) & (A.length - 1);
_length = _length - 1;
return saved;
}
alias popFront pop;
}
unittest
{
auto q = new shared GrowableCircularQueue!int;
q.push(10);
q.push(20);
q.push(30);
assert(q.pop() == 10);
assert(q.pop() == 20);
assert(q.pop() == 30);
assert(q.empty);
uint count = 0;
foreach (immutable i; 1 .. 1_000)
{
foreach (immutable j; 0 .. i)
q.push(count++);
foreach (immutable j; 0 .. i)
q.pop();
}
}
/**
* A lock-free single-reader, single-writer FIFO queue.
* https://github.com/MartinNowak/lock-free/blob/master/src/lock_free/rwqueue.d
*/
shared struct RWQueue(T, size_t capacity = roundPow2!(PAGE_SIZE / T.sizeof))
{
static assert(capacity > 0, "Cannot have a capacity of 0.");
static assert(roundPow2!capacity == capacity, "The capacity must be a power of 2");
@property size_t length() shared const
{
return atomicLoad!(MemoryOrder.acq)(_wpos) - atomicLoad!(MemoryOrder.acq)(_rpos);
}
@property bool empty() shared const
{
return !length;
}
@property bool full() const
{
return length == capacity;
}
void pushBusyWait(shared(T) t)
{
while (full)
{
import core.thread;
import core.time;
Thread.sleep(dur!"msecs"(10));
}
push(t);
}
void push(shared(T) t)
in { assert(!full); }
body
{
immutable pos = atomicLoad!(MemoryOrder.acq)(_wpos);
_data[pos & mask] = t;
atomicStore!(MemoryOrder.rel)(_wpos, pos + 1);
}
shared(T) pop()
in { assert(!empty); }
body
{
immutable pos = atomicLoad!(MemoryOrder.acq)(_rpos);
auto res = _data[pos & mask];
atomicStore!(MemoryOrder.rel)(_rpos, pos + 1);
return res;
}
//; Returns true if queue was not empty
bool clear()
{
bool startedAsEmpty = empty;
while (!empty)
pop();
return !startedAsEmpty;
}
private:
// import std.algorithm; // move
enum mask = capacity - 1;
size_t _wpos;
size_t _rpos;
T[capacity] _data;
}
private:
enum PAGE_SIZE = 4096;
template roundPow2(size_t v)
{
import core.bitop : bsr;
enum roundPow2 = v ? cast(size_t)1 << bsr(v) : 0;
}
static assert(roundPow2!0 == 0);
static assert(roundPow2!3 == 2);
static assert(roundPow2!4 == 4);
version (unittest)
{
import core.thread, std.concurrency;
enum amount = 10_000;
void push(T)(ref shared(RWQueue!T) queue)
{
foreach (i; 0 .. amount)
{
while (queue.full)
Thread.yield();
queue.push(cast(shared T)i);
}
}
void pop(T)(ref shared(RWQueue!T) queue)
{
foreach (i; 0 .. amount)
{
while (queue.empty)
Thread.yield();
assert(queue.pop() == cast(shared T)i);
}
}
}
unittest
{
shared(RWQueue!size_t) queue;
auto t0 = new Thread({push(queue);}),
t1 = new Thread({pop(queue);});
t0.start(); t1.start();
t0.join(); t1.join();
}
unittest
{
static struct Data { size_t i; }
shared(RWQueue!Data) queue;
auto t0 = new Thread({push(queue);}),
t1 = new Thread({pop(queue);});
t0.start(); t1.start();
t0.join(); t1.join();
}