| ... | @@ -20,13 +20,14 @@ pub const Lock = struct { | ... | @@ -20,13 +20,14 @@ pub const Lock = struct { |
| 20 | head: usize = UNLOCKED, | 20 | head: usize = UNLOCKED, |
| 21 | | 21 | |
| 22 | const UNLOCKED = 0; | 22 | const UNLOCKED = 0; |
| 23 | const LOCKED = 69; | 23 | const LOCKED = 1; |
| 24 | | 24 | |
| 25 | const global_event_loop = Loop.instance orelse | 25 | const global_event_loop = Loop.instance orelse |
| 26 | @compileError("std.event.Lock currently only works with event-based I/O"); | 26 | @compileError("std.event.Lock currently only works with event-based I/O"); |
| 27 | | 27 | |
| 28 | const Waiter = struct { | 28 | const Waiter = struct { |
| 29 | next: ?*Waiter, | 29 | // forced Waiter alignment to ensure it doesn't clash with LOCKED |
| | 30 | next: ?*Waiter align(2), |
| 30 | tail: *Waiter, | 31 | tail: *Waiter, |
| 31 | node: Loop.NextTickNode, | 32 | node: Loop.NextTickNode, |
| 32 | }; | 33 | }; |
| ... | @@ -34,6 +35,14 @@ pub const Lock = struct { | ... | @@ -34,6 +35,14 @@ pub const Lock = struct { |
| 34 | pub fn acquire(self: *Lock) Held { | 35 | pub fn acquire(self: *Lock) Held { |
| 35 | const held = self.mutex.acquire(); | 36 | const held = self.mutex.acquire(); |
| 36 | | 37 | |
| | 38 | // self.head transitions from multiple stages depending on the value: |
| | 39 | // UNLOCKED -> LOCKED: |
| | 40 | // acquire Lock ownership when theres no waiters |
| | 41 | // LOCKED -> <Waiter head ptr>: |
| | 42 | // Lock is already owned, enqueue first Waiter |
| | 43 | // <head ptr> -> <head ptr>: |
| | 44 | // Lock is owned with pending waiters. Push our waiter to the queue. |
| | 45 | |
| 37 | if (self.head == UNLOCKED) { | 46 | if (self.head == UNLOCKED) { |
| 38 | self.head = LOCKED; | 47 | self.head = LOCKED; |
| 39 | held.release(); | 48 | held.release(); |
| ... | @@ -47,7 +56,7 @@ pub const Lock = struct { | ... | @@ -47,7 +56,7 @@ pub const Lock = struct { |
| 47 | const head = switch (self.head) { | 56 | const head = switch (self.head) { |
| 48 | UNLOCKED => unreachable, | 57 | UNLOCKED => unreachable, |
| 49 | LOCKED => null, | 58 | LOCKED => null, |
| 50 | else => @intToPtr(?*Waiter, self.head), | 59 | else => @intToPtr(*Waiter, self.head), |
| 51 | }; | 60 | }; |
| 52 | | 61 | |
| 53 | if (head) |h| { | 62 | if (head) |h| { |
| ... | @@ -77,9 +86,17 @@ pub const Lock = struct { | ... | @@ -77,9 +86,17 @@ pub const Lock = struct { |
| 77 | const held = self.lock.mutex.acquire(); | 86 | const held = self.lock.mutex.acquire(); |
| 78 | defer held.release(); | 87 | defer held.release(); |
| 79 | | 88 | |
| | 89 | // self.head goes through the reverse transition from acquire(): |
| | 90 | // <head ptr> -> <new head ptr>: |
| | 91 | // pop a waiter from the queue to give Lock ownership when theres still others pending |
| | 92 | // <head ptr> -> LOCKED: |
| | 93 | // pop the laster waiter from the queue, while also giving it lock ownership when awaken |
| | 94 | // LOCKED -> UNLOCKED: |
| | 95 | // last lock owner releases lock while no one else is waiting for it |
| | 96 | |
| 80 | switch (self.lock.head) { | 97 | switch (self.lock.head) { |
| 81 | UNLOCKED => { | 98 | UNLOCKED => { |
| 82 | std.debug.panic("Lock unlocked when already unlocked", .{}); | 99 | unreachable; // Lock unlocked while unlocking |
| 83 | }, | 100 | }, |
| 84 | LOCKED => { | 101 | LOCKED => { |
| 85 | self.lock.head = UNLOCKED; | 102 | self.lock.head = UNLOCKED; |