authorgravatar for kappaloris@gmail.comLoris Cro <kappaloris@gmail.com> 2020-09-29 11:18:35+02:00
committergravatar for kappaloris@gmail.comLoris Cro <kappaloris@gmail.com> 2020-09-29 11:18:35+02:00
loga45a4230bc2ef67dca4bd3267863695d6b04adfa
tree9c4f714a633166bb0d9522cfc562268b779b4fd2
parenta0c0f9ead53dab4f558dbb51cc8a49961fc6984f

Fix std.event.Future

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

2 files changed, 12 insertions(+), 8 deletions(-)

lib/std/event/future.zig+1-1
...@@ -95,7 +95,7 @@ test "std.event.Future" {...@@ -95,7 +95,7 @@ test "std.event.Future" {
95 // TODO provide a way to run tests in evented I/O mode95 // TODO provide a way to run tests in evented I/O mode
96 if (!std.io.is_async) return error.SkipZigTest;96 if (!std.io.is_async) return error.SkipZigTest;
9797
98 const handle = async testFuture();98 testFuture();
99}99}
100100
101fn testFuture() void {101fn testFuture() void {
lib/std/event/lock.zig+11-7
...@@ -27,20 +27,24 @@ pub const Lock = struct {...@@ -27,20 +27,24 @@ pub const Lock = struct {
2727
28 const Waiter = struct {28 const Waiter = struct {
29 // forced Waiter alignment to ensure it doesn't clash with LOCKED29 // forced Waiter alignment to ensure it doesn't clash with LOCKED
30 next: ?*Waiter align(2), 30 next: ?*Waiter align(2),
31 tail: *Waiter,31 tail: *Waiter,
32 node: Loop.NextTickNode,32 node: Loop.NextTickNode,
33 };33 };
3434
35 pub fn initLocked() Lock {
36 return Lock{ .head = LOCKED };
37 }
38
35 pub fn acquire(self: *Lock) Held {39 pub fn acquire(self: *Lock) Held {
36 const held = self.mutex.acquire();40 const held = self.mutex.acquire();
3741
38 // self.head transitions from multiple stages depending on the value:42 // self.head transitions from multiple stages depending on the value:
39 // UNLOCKED -> LOCKED: 43 // UNLOCKED -> LOCKED:
40 // acquire Lock ownership when theres no waiters44 // acquire Lock ownership when theres no waiters
41 // LOCKED -> <Waiter head ptr>:45 // LOCKED -> <Waiter head ptr>:
42 // Lock is already owned, enqueue first Waiter46 // Lock is already owned, enqueue first Waiter
43 // <head ptr> -> <head ptr>: 47 // <head ptr> -> <head ptr>:
44 // Lock is owned with pending waiters. Push our waiter to the queue.48 // Lock is owned with pending waiters. Push our waiter to the queue.
4549
46 if (self.head == UNLOCKED) {50 if (self.head == UNLOCKED) {
...@@ -51,7 +55,7 @@ pub const Lock = struct {...@@ -51,7 +55,7 @@ pub const Lock = struct {
5155
52 var waiter: Waiter = undefined;56 var waiter: Waiter = undefined;
53 waiter.next = null;57 waiter.next = null;
54 waiter.tail = &waiter; 58 waiter.tail = &waiter;
5559
56 const head = switch (self.head) {60 const head = switch (self.head) {
57 UNLOCKED => unreachable,61 UNLOCKED => unreachable,
...@@ -79,15 +83,15 @@ pub const Lock = struct {...@@ -79,15 +83,15 @@ pub const Lock = struct {
79 }83 }
8084
81 pub const Held = struct {85 pub const Held = struct {
82 lock: *Lock, 86 lock: *Lock,
83 87
84 pub fn release(self: Held) void {88 pub fn release(self: Held) void {
85 const waiter = blk: {89 const waiter = blk: {
86 const held = self.lock.mutex.acquire();90 const held = self.lock.mutex.acquire();
87 defer held.release();91 defer held.release();
8892
89 // self.head goes through the reverse transition from acquire():93 // self.head goes through the reverse transition from acquire():
90 // <head ptr> -> <new head ptr>: 94 // <head ptr> -> <new head ptr>:
91 // pop a waiter from the queue to give Lock ownership when theres still others pending95 // pop a waiter from the queue to give Lock ownership when theres still others pending
92 // <head ptr> -> LOCKED:96 // <head ptr> -> LOCKED:
93 // pop the laster waiter from the queue, while also giving it lock ownership when awaken97 // pop the laster waiter from the queue, while also giving it lock ownership when awaken