From a9ab528e348a3f07dfaffcdcb1031062b1bfe8bb Mon Sep 17 00:00:00 2001 From: Andrew Kelley Date: Tue, 17 Jul 2018 15:17:06 -0400 Subject: std.event.Loop.onNextTick dispatches work to waiting threads --- std/atomic/queue.zig | 14 ++++++++++++++ 1 file changed, 14 insertions(+) (limited to 'std/atomic') diff --git a/std/atomic/queue.zig b/std/atomic/queue.zig index 1fd07714e8..df31c88d2a 100644 --- a/std/atomic/queue.zig +++ b/std/atomic/queue.zig @@ -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; } -- cgit v1.2.3