zig

fork of https://codeberg.org/ziglang/zig
Log | Files | Refs | README | LICENSE

commit a45a4230bc2ef67dca4bd3267863695d6b04adfa (tree)
parent a0c0f9ead53dab4f558dbb51cc8a49961fc6984f
Author: Loris Cro <kappaloris@gmail.com>
Date:   Tue, 29 Sep 2020 11:18:35 +0200

Fix std.event.Future

Signed-off-by: Loris Cro <kappaloris@gmail.com>

Diffstat:
Mlib/std/event/future.zig | 2+-
Mlib/std/event/lock.zig | 18+++++++++++-------
2 files changed, 12 insertions(+), 8 deletions(-)

diff --git a/lib/std/event/future.zig b/lib/std/event/future.zig @@ -95,7 +95,7 @@ test "std.event.Future" { // TODO provide a way to run tests in evented I/O mode if (!std.io.is_async) return error.SkipZigTest; - const handle = async testFuture(); + testFuture(); } fn testFuture() void { diff --git a/lib/std/event/lock.zig b/lib/std/event/lock.zig @@ -27,20 +27,24 @@ pub const Lock = struct { const Waiter = struct { // forced Waiter alignment to ensure it doesn't clash with LOCKED - next: ?*Waiter align(2), + next: ?*Waiter align(2), tail: *Waiter, node: Loop.NextTickNode, }; + pub fn initLocked() Lock { + return Lock{ .head = LOCKED }; + } + pub fn acquire(self: *Lock) Held { const held = self.mutex.acquire(); // self.head transitions from multiple stages depending on the value: - // UNLOCKED -> LOCKED: + // UNLOCKED -> LOCKED: // acquire Lock ownership when theres no waiters // LOCKED -> <Waiter head ptr>: // Lock is already owned, enqueue first Waiter - // <head ptr> -> <head ptr>: + // <head ptr> -> <head ptr>: // Lock is owned with pending waiters. Push our waiter to the queue. if (self.head == UNLOCKED) { @@ -51,7 +55,7 @@ pub const Lock = struct { var waiter: Waiter = undefined; waiter.next = null; - waiter.tail = &waiter; + waiter.tail = &waiter; const head = switch (self.head) { UNLOCKED => unreachable, @@ -79,15 +83,15 @@ pub const Lock = struct { } pub const Held = struct { - lock: *Lock, - + lock: *Lock, + pub fn release(self: Held) void { const waiter = blk: { const held = self.lock.mutex.acquire(); defer held.release(); // self.head goes through the reverse transition from acquire(): - // <head ptr> -> <new head ptr>: + // <head ptr> -> <new head ptr>: // pop a waiter from the queue to give Lock ownership when theres still others pending // <head ptr> -> LOCKED: // pop the laster waiter from the queue, while also giving it lock ownership when awaken