| ... | ... | @@ -34,6 +34,14 @@ pub const ResetEvent = struct { |
| 34 | 34 | self.os_event.deinit(); |
| 35 | 35 | } |
| 36 | 36 | |
| 37 | /// When `wait` would return without blocking, this returns `true`. |
| 38 | /// Note that the value may be immediately invalid upon this function's |
| 39 | /// return, because another thread may call `wait` in between, changing |
| 40 | /// the event's set/cleared status. |
| 41 | pub fn isSet(self: *ResetEvent) bool { |
| 42 | return self.os_event.isSet(); |
| 43 | } |
| 44 | |
| 37 | 45 | /// Sets the event if not already set and |
| 38 | 46 | /// wakes up all the threads waiting on the event. |
| 39 | 47 | pub fn set(self: *ResetEvent) void { |
| ... | ... | @@ -77,6 +85,10 @@ const DebugEvent = struct { |
| 77 | 85 | self.* = undefined; |
| 78 | 86 | } |
| 79 | 87 | |
| 88 | fn isSet(self: *DebugEvent) bool { |
| 89 | return self.is_set; |
| 90 | } |
| 91 | |
| 80 | 92 | fn reset(self: *DebugEvent) void { |
| 81 | 93 | self.is_set = false; |
| 82 | 94 | } |
| ... | ... | @@ -122,6 +134,13 @@ const PosixEvent = struct { |
| 122 | 134 | self.* = undefined; |
| 123 | 135 | } |
| 124 | 136 | |
| 137 | fn isSet(self: *PosixEvent) bool { |
| 138 | const sem = self.getInitializedSem(); |
| 139 | var val: c_int = undefined; |
| 140 | assert(c.sem_getvalue(sem, &val) == 0); |
| 141 | return val > 0; |
| 142 | } |
| 143 | |
| 125 | 144 | fn reset(self: *PosixEvent) void { |
| 126 | 145 | const sem = self.getInitializedSem(); |
| 127 | 146 | while (true) { |
| ... | ... | @@ -208,6 +227,10 @@ const AtomicEvent = struct { |
| 208 | 227 | self.* = undefined; |
| 209 | 228 | } |
| 210 | 229 | |
| 230 | fn isSet(self: *const AtomicEvent) bool { |
| 231 | return @atomicLoad(u32, &self.waiters, .Acquire) == WAKE; |
| 232 | } |
| 233 | |
| 211 | 234 | fn reset(self: *AtomicEvent) void { |
| 212 | 235 | @atomicStore(u32, &self.waiters, 0, .Monotonic); |
| 213 | 236 | } |
| ... | ... | @@ -374,10 +397,13 @@ test "ResetEvent" { |
| 374 | 397 | defer event.deinit(); |
| 375 | 398 | |
| 376 | 399 | // test event setting |
| 400 | testing.expect(!event.isSet()); |
| 377 | 401 | event.set(); |
| 402 | testing.expect(event.isSet()); |
| 378 | 403 | |
| 379 | 404 | // test event resetting |
| 380 | 405 | event.reset(); |
| 406 | testing.expect(!event.isSet()); |
| 381 | 407 | |
| 382 | 408 | // test event waiting (non-blocking) |
| 383 | 409 | event.set(); |