This repository has been archived by the owner on Sep 27, 2024. It is now read-only.
forked from cekongnetcv/pike
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwaker.zig
355 lines (286 loc) · 11.4 KB
/
waker.zig
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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
const std = @import("std");
const pike = @import("pike.zig");
const mem = std.mem;
const meta = std.meta;
const math = std.math;
const testing = std.testing;
const assert = std.debug.assert;
pub const Waker = struct {
const EMPTY = 0;
const NOTIFIED: usize = 1;
const SHUTDOWN: usize = 2;
state: usize = EMPTY,
pub const Node = struct {
dead: bool = false,
task: pike.Task,
};
pub fn wait(self: *Waker, args: anytype) !void {
var node: Node = .{ .task = pike.Task.init(@frame()) };
suspend {
var state = @atomicLoad(usize, &self.state, .Monotonic);
while (true) {
const new_state = switch (state) {
EMPTY => @ptrToInt(&node),
NOTIFIED => EMPTY,
SHUTDOWN => {
node.dead = true;
pike.dispatch(&node.task, .{ .use_lifo = true });
break;
},
else => unreachable,
};
state = @cmpxchgWeak(
usize,
&self.state,
state,
new_state,
.Release,
.Monotonic,
) orelse {
if (new_state == EMPTY) pike.dispatch(&node.task, args);
break;
};
}
}
if (node.dead) return error.OperationCancelled;
}
pub fn notify(self: *Waker) ?*pike.Task {
var state = @atomicLoad(usize, &self.state, .Monotonic);
while (true) {
const new_state = switch (state) {
EMPTY => NOTIFIED,
NOTIFIED, SHUTDOWN => return null,
else => EMPTY,
};
state = @cmpxchgWeak(usize, &self.state, state, new_state, .Acquire, .Monotonic) orelse {
if (new_state == NOTIFIED) return null;
const node = @intToPtr(*Node, state);
return &node.task;
};
}
}
pub fn shutdown(self: *Waker) ?*pike.Task {
return switch (@atomicRmw(usize, &self.state, .Xchg, SHUTDOWN, .AcqRel)) {
EMPTY, NOTIFIED, SHUTDOWN => null,
else => |state| {
const node = @intToPtr(*Node, state);
node.dead = true;
return &node.task;
},
};
}
};
test "Waker.wait() / Waker.notify() / Waker.shutdown()" {
var waker: Waker = .{};
{
var frame = async waker.wait(.{});
pike.dispatch(waker.notify().?, .{});
try nosuspend await frame;
}
{
testing.expect(waker.shutdown() == null);
testing.expectError(error.OperationCancelled, nosuspend waker.wait(.{}));
}
}
pub fn PackedWaker(comptime Frame: type, comptime Set: type) type {
const set_fields = meta.fields(Set);
const set_count = set_fields.len;
return struct {
pub const FrameList = PackedList(Frame, Set);
pub const FrameNode = FrameList.Node;
const Self = @This();
ready: [set_count]bool = [_]bool{false} ** set_count,
heads: [set_count]?*FrameNode = [_]?*FrameNode{null} ** set_count,
pub fn wait(self: *Self, set: Set) bool {
var any_ready = false;
inline for (set_fields) |field, field_index| {
if (@field(set, field.name) and self.ready[field_index]) {
if (self.ready[field_index]) {
self.ready[field_index] = false;
any_ready = true;
}
}
}
return any_ready;
}
pub fn wake(self: *Self, set: Set) ?*FrameList.Node {
return FrameList.pop(&self.heads, set) orelse blk: {
inline for (set_fields) |field, field_index| {
if (@field(set, field.name) and self.heads[field_index] == null) {
self.ready[field_index] = true;
}
}
break :blk null;
};
}
pub fn next(self: *Self, set: Set) ?*FrameList.Node {
inline for (set_fields) |field, field_index| {
if (@field(set, field.name) and self.heads[field_index] == null) {
return null;
}
}
return FrameList.pop(&self.heads, set);
}
};
}
test "PackedWaker.wake() / PackedWaker.wait()" {
const Set = struct {
a: bool = false,
b: bool = false,
c: bool = false,
d: bool = false,
};
const Scope = struct {
inner: anyframe,
};
const Test = struct {
fn do(waker: *PackedWaker(*Scope, Set), set: Set, completed: *bool) callconv(.Async) void {
defer completed.* = true;
if (waker.wait(set)) return;
suspend {
var scope = Scope{ .inner = @frame() };
var node = meta.Child(@TypeOf(waker)).FrameNode{ .data = &scope };
meta.Child(@TypeOf(waker)).FrameList.append(&waker.heads, set, &node);
}
}
};
var waker: PackedWaker(*Scope, Set) = .{};
testing.expect(waker.wake(.{ .a = true, .b = true, .c = true, .d = true }) == null);
testing.expect(mem.allEqual(bool, &waker.ready, true));
testing.expect(waker.wait(.{ .a = true, .b = true, .c = true, .d = true }));
testing.expect(mem.allEqual(bool, &waker.ready, false));
var A_done = false;
var B_done = false;
var C_done = false;
var D_done = false;
var A = async Test.do(&waker, .{ .a = true, .c = true }, &A_done);
var B = async Test.do(&waker, .{ .a = true, .b = true, .c = true }, &B_done);
var C = async Test.do(&waker, .{ .a = true, .b = true, .d = true }, &C_done);
var D = async Test.do(&waker, .{ .d = true }, &D_done);
resume waker.wake(.{ .b = true }).?.data.inner;
nosuspend await B;
testing.expect(B_done);
resume waker.wake(.{ .b = true }).?.data.inner;
nosuspend await C;
testing.expect(C_done);
resume waker.wake(.{ .a = true }).?.data.inner;
nosuspend await A;
testing.expect(A_done);
resume waker.wake(.{ .d = true }).?.data.inner;
nosuspend await D;
testing.expect(D_done);
}
fn PackedList(comptime T: type, comptime U: type) type {
const set_fields = meta.fields(U);
const set_count = set_fields.len;
return struct {
const Self = @This();
pub const Node = struct {
data: T,
next: [set_count]?*Self.Node = [_]?*Node{null} ** set_count,
prev: [set_count]?*Self.Node = [_]?*Node{null} ** set_count,
};
pub fn append(heads: *[set_count]?*Self.Node, set: U, node: *Self.Node) void {
assert(mem.allEqual(?*Self.Node, &node.prev, null));
assert(mem.allEqual(?*Self.Node, &node.next, null));
inline for (set_fields) |field, i| {
if (@field(set, field.name)) {
if (heads[i]) |head| {
const tail = head.prev[i] orelse unreachable;
node.prev[i] = tail;
tail.next[i] = node;
head.prev[i] = node;
} else {
node.prev[i] = node;
heads[i] = node;
}
}
}
}
pub fn prepend(heads: *[set_count]?*Self.Node, set: U, node: *Self.Node) void {
assert(mem.allEqual(?*Self.Node, &node.prev, null));
assert(mem.allEqual(?*Self.Node, &node.next, null));
inline for (set_fields) |field, i| {
if (@field(set, field.name)) {
if (heads[i]) |head| {
node.prev[i] = head;
node.next[i] = head;
head.prev[i] = node;
heads[i] = node;
} else {
node.prev[i] = node;
heads[i] = node;
}
}
}
}
pub fn pop(heads: *[set_count]?*Self.Node, set: U) ?*Self.Node {
inline for (set_fields) |field, field_index| {
if (@field(set, field.name) and heads[field_index] != null) {
const head = heads[field_index] orelse unreachable;
comptime var j = 0;
inline while (j < set_count) : (j += 1) {
if (head.prev[j]) |prev| prev.next[j] = if (prev != head.next[j]) head.next[j] else null;
if (head.next[j]) |next| next.prev[j] = head.prev[j];
if (heads[j] == head) heads[j] = head.next[j];
}
return head;
}
}
return null;
}
};
}
test "PackedList.append() / PackedList.prepend() / PackedList.pop()" {
const Set = struct {
a: bool = false,
b: bool = false,
c: bool = false,
d: bool = false,
};
const U8List = PackedList(u8, Set);
const Node = U8List.Node;
var heads: [@sizeOf(Set)]?*Node = [_]?*Node{null} ** @sizeOf(Set);
var A = Node{ .data = 'A' };
var B = Node{ .data = 'B' };
var C = Node{ .data = 'C' };
var D = Node{ .data = 'D' };
U8List.append(&heads, .{ .a = true, .c = true }, &A);
U8List.append(&heads, .{ .a = true, .b = true, .c = true }, &B);
U8List.prepend(&heads, .{ .a = true, .b = true, .d = true }, &C);
U8List.append(&heads, .{ .d = true }, &D);
testing.expect(U8List.pop(&heads, .{ .b = true }).?.data == C.data);
testing.expect(U8List.pop(&heads, .{ .b = true }).?.data == B.data);
testing.expect(U8List.pop(&heads, .{ .a = true }).?.data == A.data);
testing.expect(U8List.pop(&heads, .{ .d = true }).?.data == D.data);
testing.expect(mem.allEqual(?*Node, &heads, null));
}
test "PackedList.append() / PackedList.prepend() / PackedList.pop()" {
const Set = struct {
a: bool = false,
b: bool = false,
c: bool = false,
d: bool = false,
};
const U8List = PackedList(u8, Set);
const Node = U8List.Node;
var heads: [@sizeOf(Set)]?*Node = [_]?*Node{null} ** @sizeOf(Set);
var A = Node{ .data = 'A' };
var B = Node{ .data = 'B' };
U8List.append(&heads, .{ .a = true, .b = true }, &A);
testing.expect(heads[0] == &A and heads[0].?.prev[0] == &A and heads[0].?.next[0] == null);
testing.expect(heads[1] == &A and heads[1].?.prev[1] == &A and heads[1].?.next[1] == null);
testing.expect(heads[2] == null);
testing.expect(heads[3] == null);
U8List.prepend(&heads, .{ .a = true, .c = true }, &B);
testing.expect(heads[0] == &B and heads[0].?.prev[0] == &A and heads[0].?.prev[0].?.next[0] == null);
testing.expect(heads[1] == &A and heads[1].?.prev[1] == &A and heads[1].?.prev[1].?.next[1] == null);
testing.expect(heads[2] == &B and heads[2].?.prev[2] == &B and heads[2].?.prev[2].?.next[2] == null);
testing.expect(heads[3] == null);
testing.expect(U8List.pop(&heads, .{ .a = true }).?.data == B.data);
testing.expect(heads[0] == &A);
testing.expect(heads[0].?.prev[0] == &A);
testing.expect(mem.allEqual(?*Node, &heads[0].?.next, null));
testing.expect(U8List.pop(&heads, .{ .a = true }).?.data == A.data);
testing.expect(mem.allEqual(?*Node, &heads, null));
}