std.event.Loop.onNextTick dispatches work to waiting threads

This commit is contained in:
Andrew Kelley
2018-07-17 15:17:06 -04:00
parent ecf8da00c5
commit a9ab528e34
2 changed files with 113 additions and 106 deletions

View File

@@ -51,6 +51,20 @@ pub fn Queue(comptime T: type) type {
return head;
}
pub fn unget(self: *Self, node: *Node) void {
while (@atomicRmw(u8, &self.lock, builtin.AtomicRmwOp.Xchg, 1, AtomicOrder.SeqCst) != 0) {}
defer assert(@atomicRmw(u8, &self.lock, builtin.AtomicRmwOp.Xchg, 0, AtomicOrder.SeqCst) == 1);
const opt_head = self.head;
self.head = node;
if (opt_head) |head| {
head.next = node;
} else {
assert(self.tail == null);
self.tail = node;
}
}
pub fn isEmpty(self: *Self) bool {
return @atomicLoad(?*Node, &self.head, builtin.AtomicOrder.SeqCst) != null;
}