| ... | ... | @@ -4,9 +4,10 @@ const testing = std.testing; |
| 4 | 4 | const assert = std.debug.assert; |
| 5 | 5 | const Backoff = std.SpinLock.Backoff; |
| 6 | 6 | const c = std.c; |
| 7 | const os = std.os; |
| 7 | 8 | const time = std.time; |
| 8 | | const linux = std.os.linux; |
| 9 | | const windows = std.os.windows; |
| 9 | const linux = os.linux; |
| 10 | const windows = os.windows; |
| 10 | 11 | |
| 11 | 12 | /// A resource object which supports blocking until signaled. |
| 12 | 13 | /// Once finished, the `deinit()` method should be called for correctness. |
| ... | ... | @@ -23,15 +24,15 @@ pub const ResetEvent = struct { |
| 23 | 24 | } |
| 24 | 25 | |
| 25 | 26 | /// Returns whether or not the event is currenetly set |
| 26 | | pub fn isSet(self: *const ResetEvent) bool { |
| 27 | pub fn isSet(self: *ResetEvent) bool { |
| 27 | 28 | return self.os_event.isSet(); |
| 28 | 29 | } |
| 29 | 30 | |
| 30 | 31 | /// Sets the event if not already set and |
| 31 | 32 | /// wakes up AT LEAST one thread waiting the event. |
| 32 | 33 | /// Returns whether or not a thread was woken up. |
| 33 | | pub fn set(self: *ResetEvent) bool { |
| 34 | | return self.os_event.set(); |
| 34 | pub fn set(self: *ResetEvent, auto_reset: bool) bool { |
| 35 | return self.os_event.set(auto_reset); |
| 35 | 36 | } |
| 36 | 37 | |
| 37 | 38 | /// Resets the event to its original, unset state. |
| ... | ... | @@ -73,15 +74,15 @@ const DebugEvent = struct { |
| 73 | 74 | self.* = undefined; |
| 74 | 75 | } |
| 75 | 76 | |
| 76 | | pub fn isSet(self: *const DebugEvent) bool { |
| 77 | pub fn isSet(self: *DebugEvent) bool { |
| 77 | 78 | if (!std.debug.runtime_safety) |
| 78 | 79 | return true; |
| 79 | 80 | return self.is_set; |
| 80 | 81 | } |
| 81 | 82 | |
| 82 | | pub fn set(self: *DebugEvent) bool { |
| 83 | pub fn set(self: *DebugEvent, auto_reset: bool) bool { |
| 83 | 84 | if (std.debug.runtime_safety) |
| 84 | | self.is_set = true; |
| 85 | self.is_set = !auto_reset; |
| 85 | 86 | return false; |
| 86 | 87 | } |
| 87 | 88 | |
| ... | ... | @@ -100,102 +101,87 @@ const DebugEvent = struct { |
| 100 | 101 | } |
| 101 | 102 | }; |
| 102 | 103 | |
| 103 | | fn EventState(comptime TagType: type) type { |
| 104 | | return enum(TagType) { |
| 105 | | Empty, |
| 106 | | Waiting, |
| 107 | | Signaled, |
| 108 | | }; |
| 109 | | } |
| 104 | fn AtomicEvent(comptime FutexImpl: type) type { |
| 105 | return struct { |
| 106 | state: u32, |
| 110 | 107 | |
| 111 | | const SpinEvent = struct { |
| 112 | | state: State, |
| 108 | const IS_SET: u32 = 1 << 0; |
| 109 | const WAIT_MASK = ~IS_SET; |
| 113 | 110 | |
| 114 | | const State = EventState(u8); |
| 111 | pub const Self = @This(); |
| 112 | pub const Futex = FutexImpl; |
| 115 | 113 | |
| 116 | | pub fn init() SpinEvent { |
| 117 | | return SpinEvent{ .state = .Empty }; |
| 118 | | } |
| 114 | pub fn init() Self { |
| 115 | return Self{ .state = 0 }; |
| 116 | } |
| 119 | 117 | |
| 120 | | pub fn deinit(self: *SpinEvent) void { |
| 121 | | self.* = undefined; |
| 122 | | } |
| 118 | pub fn deinit(self: *Self) void { |
| 119 | self.* = undefined; |
| 120 | } |
| 123 | 121 | |
| 124 | | pub fn isSet(self: *const SpinEvent) bool { |
| 125 | | return @atomicLoad(State, &self.state, .Acquire) == .Signaled; |
| 126 | | } |
| 122 | pub fn isSet(self: *const Self) bool { |
| 123 | const state = @atomicLoad(u32, &self.state, .Acquire); |
| 124 | return (state & IS_SET) != 0; |
| 125 | } |
| 127 | 126 | |
| 128 | | pub fn set(self: *SpinEvent) bool { |
| 129 | | return @atomicRmw(State, &self.state, .Xchg, .Signaled, .Release) == .Waiting; |
| 130 | | } |
| 127 | pub fn reset(self: *Self) bool { |
| 128 | const old_state = @atomicRmw(u32, &self.state, .Xchg, 0, .Monotonic); |
| 129 | return (old_state & IS_SET) != 0; |
| 130 | } |
| 131 | 131 | |
| 132 | | pub fn reset(self: *SpinEvent) bool { |
| 133 | | return @atomicRmw(State, &self.state, .Xchg, .Empty, .Monotonic) == .Signaled; |
| 134 | | } |
| 132 | pub fn set(self: *Self, auto_reset: bool) bool { |
| 133 | const new_state = if (auto_reset) 0 else IS_SET; |
| 134 | const old_state = @atomicRmw(u32, &self.state, .Xchg, new_state, .Release); |
| 135 | if ((old_state & WAIT_MASK) == 0) { |
| 136 | return false; |
| 137 | } |
| 135 | 138 | |
| 136 | | pub fn wait(self: *SpinEvent, timeout: ?u64) ResetEvent.WaitError!bool { |
| 137 | | var state = @atomicLoad(State, &self.state, .Monotonic); |
| 138 | | while (true) { |
| 139 | | switch (state) { |
| 140 | | .Empty => state = @cmpxchgWeak(State, &self.state, state, .Waiting, .Acquire, .Monotonic) orelse break, |
| 141 | | .Waiting => break, |
| 142 | | .Signaled => return false, |
| 139 | Futex.wake(&self.state); |
| 140 | return true; |
| 141 | } |
| 142 | |
| 143 | pub fn wait(self: *Self, timeout: ?u64) ResetEvent.WaitError!bool { |
| 144 | var dummy_value: u32 = undefined; |
| 145 | const wait_token = @truncate(u32, @ptrToInt(&dummy_value)); |
| 146 | |
| 147 | var state = @atomicLoad(u32, &self.state, .Monotonic); |
| 148 | while (true) { |
| 149 | if ((state & IS_SET) != 0) |
| 150 | return false; |
| 151 | state = @cmpxchgWeak(u32, &self.state, state, wait_token, .Acquire, .Monotonic) orelse break; |
| 143 | 152 | } |
| 153 | |
| 154 | try Futex.wait(&self.state, wait_token, timeout); |
| 155 | return true; |
| 144 | 156 | } |
| 157 | }; |
| 158 | } |
| 159 | |
| 160 | const SpinEvent = AtomicEvent(struct { |
| 161 | fn wake(ptr: *const u32) void {} |
| 145 | 162 | |
| 146 | | // TODO: handle case for time.Timer.start() fails |
| 163 | fn wait(ptr: *const u32, expected: u32, timeout: ?u64) ResetEvent.WaitError!void { |
| 164 | // TODO: handle platforms where time.Timer.start() fails |
| 147 | 165 | var spin = Backoff.init(); |
| 148 | 166 | var timer = if (timeout == null) null else time.Timer.start() catch unreachable; |
| 149 | | while (@atomicLoad(State, &self.state, .Monotonic) == .Waiting) { |
| 167 | while (@atomicLoad(u32, ptr, .Acquire) == expected) { |
| 150 | 168 | spin.yield(); |
| 151 | 169 | if (timeout) |timeout_ns| { |
| 152 | 170 | if (timer.?.read() > timeout_ns) |
| 153 | 171 | return ResetEvent.WaitError.TimedOut; |
| 154 | 172 | } |
| 155 | 173 | } |
| 156 | | return true; |
| 157 | | } |
| 158 | | }; |
| 159 | | |
| 160 | | const LinuxEvent = struct { |
| 161 | | state: State, |
| 162 | | |
| 163 | | const State = EventState(i32); |
| 164 | | |
| 165 | | pub fn init() LinuxEvent { |
| 166 | | return LinuxEvent{ .state = .Empty }; |
| 167 | | } |
| 168 | | |
| 169 | | pub fn deinit(self: *LinuxEvent) void { |
| 170 | | self.* = undefined; |
| 171 | | } |
| 172 | | |
| 173 | | pub fn isSet(self: *const LinuxEvent) bool { |
| 174 | | return @atomicLoad(State, &self.state, .Acquire) == .Signaled; |
| 175 | 174 | } |
| 175 | }); |
| 176 | 176 | |
| 177 | | pub fn set(self: *LinuxEvent) bool { |
| 178 | | if (@atomicRmw(State, &self.state, .Xchg, .Signaled, .Release) != .Waiting) |
| 179 | | return false; |
| 180 | | const rc = linux.futex_wake(@ptrCast(*const i32, &self.state), linux.FUTEX_WAKE | linux.FUTEX_PRIVATE_FLAG, 1); |
| 177 | const LinuxEvent = AtomicEvent(struct { |
| 178 | fn wake(ptr: *const u32) void { |
| 179 | const key = @ptrCast(*const i32, ptr); |
| 180 | const rc = linux.futex_wake(key, linux.FUTEX_WAKE | linux.FUTEX_PRIVATE_FLAG, 1); |
| 181 | 181 | assert(linux.getErrno(rc) == 0); |
| 182 | | return true; |
| 183 | 182 | } |
| 184 | 183 | |
| 185 | | pub fn reset(self: *LinuxEvent) bool { |
| 186 | | return @atomicRmw(State, &self.state, .Xchg, .Empty, .Monotonic) == .Signaled; |
| 187 | | } |
| 188 | | |
| 189 | | pub fn wait(self: *LinuxEvent, timeout: ?u64) ResetEvent.WaitError!bool { |
| 190 | | var state = @atomicLoad(State, &self.state, .Monotonic); |
| 191 | | while (true) { |
| 192 | | switch (state) { |
| 193 | | .Empty => state = @cmpxchgWeak(State, &self.state, .Empty, .Waiting, .Acquire, .Monotonic) orelse break, |
| 194 | | .Waiting => break, |
| 195 | | .Signaled => return false, |
| 196 | | } |
| 197 | | } |
| 198 | | |
| 184 | fn wait(ptr: *const u32, expected: u32, timeout: ?u64) ResetEvent.WaitError!void { |
| 199 | 185 | var ts: linux.timespec = undefined; |
| 200 | 186 | var ts_ptr: ?*linux.timespec = null; |
| 201 | 187 | if (timeout) |timeout_ns| { |
| ... | ... | @@ -204,28 +190,94 @@ const LinuxEvent = struct { |
| 204 | 190 | ts.tv_nsec = @intCast(isize, timeout_ns % time.ns_per_s); |
| 205 | 191 | } |
| 206 | 192 | |
| 207 | | while (@atomicLoad(State, &self.state, .Monotonic) == .Waiting) { |
| 208 | | const rc = linux.futex_wait(@ptrCast(*const i32, &self.state), linux.FUTEX_WAIT | linux.FUTEX_PRIVATE_FLAG, @enumToInt(State.Waiting), ts_ptr); |
| 193 | const key = @ptrCast(*const i32, ptr); |
| 194 | const key_expect = @bitCast(i32, expected); |
| 195 | while (@atomicLoad(i32, key, .Acquire) == key_expect) { |
| 196 | const rc = linux.futex_wait(key, linux.FUTEX_WAIT | linux.FUTEX_PRIVATE_FLAG, key_expect, ts_ptr); |
| 209 | 197 | switch (linux.getErrno(rc)) { |
| 210 | | 0, linux.EINTR => continue, |
| 211 | | linux.EAGAIN => break, |
| 198 | 0, linux.EAGAIN => break, |
| 199 | linux.EINTR => continue, |
| 212 | 200 | linux.ETIMEDOUT => return ResetEvent.WaitError.TimedOut, |
| 213 | 201 | else => unreachable, |
| 214 | 202 | } |
| 215 | 203 | } |
| 216 | 204 | } |
| 217 | | }; |
| 205 | }); |
| 206 | |
| 207 | const WindowsEvent = AtomicEvent(struct { |
| 208 | fn wake(ptr: *const u32) void { |
| 209 | if (getEventHandle()) |handle| { |
| 210 | const key = @ptrCast(*const c_void, ptr); |
| 211 | const rc = windows.ntdll.NtReleaseKeyedEvent(handle, key, windows.FALSE, null); |
| 212 | assert(rc == 0); |
| 213 | } |
| 214 | } |
| 215 | |
| 216 | fn wait(ptr: *const u32, expected: u32, timeout: ?u64) ResetEvent.WaitError!void { |
| 217 | // fallback to spinlock if NT Keyed Events arent available |
| 218 | const handle = getEventHandle() orelse { |
| 219 | return SpinEvent.Futex.wait(ptr, expected, timeout); |
| 220 | }; |
| 221 | |
| 222 | var timeout_ptr: ?*windows.LARGE_INTEGER = null; |
| 223 | var timeout_value: windows.LARGE_INTEGER = undefined; |
| 224 | if (timeout) |timeout_ns| { |
| 225 | timeout_ptr = &timeout_value; |
| 226 | timeout_value = @intCast(windows.LARGE_INTEGER, @divFloor(timeout_ns, time.millisecond)); |
| 227 | } |
| 228 | |
| 229 | const key = @ptrCast(*const c_void, ptr); |
| 230 | while (@atomicLoad(u32, ptr, .Acquire) == expected) { |
| 231 | const rc = windows.ntdll.NtWaitForKeyedEvent(handle, key, windows.FALSE, timeout_ptr); |
| 232 | assert(rc == 0); |
| 233 | } |
| 234 | } |
| 235 | |
| 236 | var keyed_state = State.Uninitialized; |
| 237 | var keyed_handle: ?windows.HANDLE = null; |
| 238 | |
| 239 | const State = enum(u8) { |
| 240 | Uninitialized, |
| 241 | Intializing, |
| 242 | Initialized, |
| 243 | }; |
| 244 | |
| 245 | fn getEventHandle() ?windows.HANDLE { |
| 246 | var spin = Backoff.init(); |
| 247 | var state = @atomicLoad(State, &keyed_state, .Monotonic); |
| 248 | |
| 249 | while (true) { |
| 250 | switch (state) { |
| 251 | .Initialized => { |
| 252 | return keyed_handle; |
| 253 | }, |
| 254 | .Intializing => { |
| 255 | spin.yield(); |
| 256 | state = @atomicLoad(State, &keyed_state, .Acquire); |
| 257 | }, |
| 258 | .Uninitialized => state = @cmpxchgWeak(State, &keyed_state, state, .Intializing, .Acquire, .Monotonic) orelse { |
| 259 | var handle: windows.HANDLE = undefined; |
| 260 | const access_mask = windows.GENERIC_READ | windows.GENERIC_WRITE; |
| 261 | if (windows.ntdll.NtCreateKeyedEvent(&handle, access_mask, null, 0) == 0) |
| 262 | keyed_handle = handle; |
| 263 | @atomicStore(State, &keyed_state, .Initialized, .Release); |
| 264 | return keyed_handle; |
| 265 | }, |
| 266 | } |
| 267 | } |
| 268 | } |
| 269 | }); |
| 218 | 270 | |
| 219 | 271 | const PosixEvent = struct { |
| 220 | | state: State, |
| 272 | state: u32, |
| 221 | 273 | cond: c.pthread_cond_t, |
| 222 | 274 | mutex: c.pthread_mutex_t, |
| 223 | 275 | |
| 224 | | const State = EventState(u8); |
| 276 | const IS_SET: u32 = 1; |
| 225 | 277 | |
| 226 | 278 | pub fn init() PosixEvent { |
| 227 | 279 | return PosixEvent{ |
| 228 | | .state = .Empty, |
| 280 | .state = .0, |
| 229 | 281 | .cond = c.PTHREAD_COND_INITIALIZER, |
| 230 | 282 | .mutex = c.PTHREAD_MUTEX_INITIALIZER, |
| 231 | 283 | }; |
| ... | ... | @@ -234,107 +286,141 @@ const PosixEvent = struct { |
| 234 | 286 | pub fn deinit(self: *PosixEvent) void { |
| 235 | 287 | // On dragonfly, the destroy functions return EINVAL if they were initialized statically. |
| 236 | 288 | const retm = c.pthread_mutex_destroy(&self.mutex); |
| 237 | | assert(retm == 0 or retm == (if (builtin.os == .dragonfly) std.os.EINVAL else 0)); |
| 289 | assert(retm == 0 or retm == (if (builtin.os == .dragonfly) os.EINVAL else 0)); |
| 238 | 290 | const retc = c.pthread_cond_destroy(&self.cond); |
| 239 | | assert(retc == 0 or retc == (if (builtin.os == .dragonfly) std.os.EINVAL else 0)); |
| 240 | | self.* = undefined; |
| 291 | assert(retc == 0 or retc == (if (builtin.os == .dragonfly) os.EINVAL else 0)); |
| 241 | 292 | } |
| 242 | 293 | |
| 243 | | pub fn isSet(self: *const PosixEvent) bool { |
| 294 | pub fn isSet(self: *PosixEvent) bool { |
| 244 | 295 | assert(c.pthread_mutex_lock(&self.mutex) == 0); |
| 245 | 296 | defer assert(c.pthread_mutex_unlock(&self.mutex) == 0); |
| 246 | 297 | |
| 247 | | return self.state == .Signaled; |
| 298 | return self.state == IS_SET; |
| 248 | 299 | } |
| 249 | 300 | |
| 250 | | pub fn set(self: *PosixEvent) bool { |
| 301 | pub fn reset(self: *PosixEvent) bool { |
| 251 | 302 | assert(c.pthread_mutex_lock(&self.mutex) == 0); |
| 252 | 303 | defer assert(c.pthread_mutex_unlock(&self.mutex) == 0); |
| 253 | 304 | |
| 254 | | const woken = self.state == .Waiting; |
| 255 | | self.state = .Signaled; |
| 256 | | return woken; |
| 305 | const was_set = self.state == IS_SET; |
| 306 | self.state = 0; |
| 307 | return was_set; |
| 257 | 308 | } |
| 258 | 309 | |
| 259 | | pub fn reset(self: *PosixEvent) bool { |
| 310 | pub fn set(self: *PosixEvent, auto_reset: bool) bool { |
| 260 | 311 | assert(c.pthread_mutex_lock(&self.mutex) == 0); |
| 261 | 312 | defer assert(c.pthread_mutex_unlock(&self.mutex) == 0); |
| 262 | 313 | |
| 263 | | const was_set = self.state == .Signaled; |
| 264 | | self.state = .Empty; |
| 265 | | return was_set; |
| 314 | const had_waiter = self.state > IS_SET; |
| 315 | self.state = if (auto_reset) 0 else IS_SET; |
| 316 | if (had_waiter) { |
| 317 | assert(c.pthread_cond_signal(&self.cond) == 0); |
| 318 | } |
| 319 | return had_waiter; |
| 266 | 320 | } |
| 267 | 321 | |
| 268 | 322 | pub fn wait(self: *PosixEvent, timeout: ?u64) ResetEvent.WaitError!bool { |
| 269 | 323 | assert(c.pthread_mutex_lock(&self.mutex) == 0); |
| 270 | 324 | defer assert(c.pthread_mutex_unlock(&self.mutex) == 0); |
| 271 | 325 | |
| 272 | | if (self.state == .Signaled) |
| 326 | if (self.state == IS_SET) |
| 273 | 327 | return false; |
| 274 | 328 | |
| 275 | | var ts: std.os.timespec = undefined; |
| 329 | var ts: os.timespec = undefined; |
| 276 | 330 | var ts_ptr = &ts; |
| 277 | 331 | if (timeout) |timeout_ns| { |
| 278 | | var tv: std.os.timeval = undefined; |
| 332 | var tv: os.timeval = undefined; |
| 279 | 333 | assert(c.gettimeofday(&tv, null) == 0); |
| 280 | | ts.tv_sec = @intCast(isize, tv.tv_sec + (timeout_ns / time.ns_per_s)); |
| 281 | | ts.tv_nsec = @intCast(isize, (tv.tv_usec * time.microsecond) + (timeout_ns % time.ns_per_s)); |
| 334 | ts.tv_sec = tv.tv_sec + @intCast(isize, timeout_ns / time.ns_per_s); |
| 335 | ts.tv_nsec = (tv.tv_usec * time.microsecond) + @intCast(isize, timeout_ns % time.ns_per_s); |
| 282 | 336 | } |
| 283 | 337 | |
| 284 | | self.state = .Waiting; |
| 285 | | while (self.state == .Waiting) { |
| 338 | var dummy_value: u32 = undefined; |
| 339 | var wait_token = @truncate(u32, @ptrToInt(&dummy_value)); |
| 340 | self.state = wait_token; |
| 341 | |
| 342 | while (self.state == wait_token) { |
| 286 | 343 | const rc = switch (timeout == null) { |
| 287 | 344 | true => c.pthread_cond_wait(&self.cond, &self.mutex), |
| 288 | 345 | else => c.pthread_cond_timedwait(&self.cond, &self.mutex, ts_ptr), |
| 289 | 346 | }; |
| 290 | | assert(rc == 0); |
| 347 | // TODO: rc appears to be the positive error code making os.errno() always return 0 on linux |
| 348 | switch (std.math.max(@as(c_int, os.errno(rc)), rc)) { |
| 349 | 0 => {}, |
| 350 | os.ETIMEDOUT => return ResetEvent.WaitError.TimedOut, |
| 351 | os.EINVAL => unreachable, |
| 352 | os.EPERM => unreachable, |
| 353 | else => unreachable, |
| 354 | } |
| 291 | 355 | } |
| 356 | return true; |
| 292 | 357 | } |
| 293 | 358 | }; |
| 294 | 359 | |
| 295 | | const WindowsEvent = struct { |
| 296 | | state: State, |
| 297 | | |
| 298 | | const State = EventState(u32); |
| 299 | | |
| 300 | | pub fn init() WindowsEvent { |
| 301 | | return WindowsEvent{ .state = .Empty }; |
| 302 | | } |
| 303 | | |
| 304 | | pub fn deinit(self: *WindowsEvent) void { |
| 305 | | self.* = undefined; |
| 306 | | } |
| 307 | | |
| 308 | | pub fn isSet(self: *const WindowsEvent) bool { |
| 309 | | return @atomicLoad(State, &self.state, .Acquire) == .Signaled; |
| 310 | | } |
| 311 | | |
| 312 | | pub fn set(self: *WindowsEvent) bool { |
| 313 | | if (@atomicRmw(State, &self.state, .Xchg, .Signaled, .Release) != .Waiting) |
| 314 | | return false; |
| 315 | | |
| 316 | | if (getEventHandle()) |handle| { |
| 317 | | const key = @ptrCast(*const c_void, &self.state); |
| 318 | | const rc = windows.ntdll.NtReleaseKeyedEvent(handle, key, windows.FALSE, null); |
| 319 | | assert(rc == 0); |
| 360 | test "std.ResetEvent" { |
| 361 | // TODO |
| 362 | if (builtin.single_threaded) |
| 363 | return error.SkipZigTest; |
| 364 | |
| 365 | var event = ResetEvent.init(); |
| 366 | defer event.deinit(); |
| 367 | |
| 368 | // test event setting |
| 369 | testing.expect(event.isSet() == false); |
| 370 | testing.expect(event.set(false) == false); |
| 371 | testing.expect(event.isSet() == true); |
| 372 | |
| 373 | // test event resetting |
| 374 | testing.expect(event.reset() == true); |
| 375 | testing.expect(event.isSet() == false); |
| 376 | testing.expect(event.reset() == false); |
| 377 | |
| 378 | // test waiting timeout |
| 379 | const delay = 100 * time.millisecond; |
| 380 | var timer = time.Timer.start() catch unreachable; |
| 381 | testing.expectError(ResetEvent.WaitError.TimedOut, event.wait(delay)); |
| 382 | const elapsed = timer.read(); |
| 383 | testing.expect(elapsed >= delay and elapsed < delay * 2); |
| 384 | |
| 385 | // test cross thread signaling |
| 386 | const Context = struct { |
| 387 | event: ResetEvent, |
| 388 | value: u128, |
| 389 | |
| 390 | fn receiver(self: *@This()) void { |
| 391 | // wait for the sender to notify us with updated value |
| 392 | assert(self.value == 0); |
| 393 | assert((self.event.wait(1 * time.second) catch unreachable) == true); |
| 394 | assert(self.value == 1); |
| 395 | |
| 396 | // wait for sender to sleep, then notify it of new value |
| 397 | time.sleep(50 * time.millisecond); |
| 398 | self.value = 2; |
| 399 | assert(self.event.set(false) == true); |
| 320 | 400 | } |
| 321 | | return true; |
| 322 | | } |
| 323 | 401 | |
| 324 | | pub fn reset(self: *WindowsEvent) bool { |
| 325 | | return @atomicRmw(State, &self.state, .Xchg, .Empty, .Monotonic) == .Signaled; |
| 326 | | } |
| 402 | fn sender(self: *@This()) !void { |
| 403 | // wait for the receiver() to start wait()'ing |
| 404 | time.sleep(50 * time.millisecond); |
| 327 | 405 | |
| 328 | | pub fn wait(self: *WindowsEvent, timeout: ?u64) ResetEvent.WaitError!bool { |
| 329 | | var state = @atomicLoad(State, &self.state, .Monotonic); |
| 330 | | while (true) { |
| 331 | | switch (state) { |
| 332 | | .Empty => state = @cmpxchgWeak(State, &self.state, .Empty, .Waiting, .Acquire, .Monotonic) orelse break, |
| 333 | | .Waiting => break, |
| 334 | | .Signaled => return false, |
| 335 | | } |
| 406 | // update value to 1 and notify the receiver() |
| 407 | assert(self.value == 0); |
| 408 | self.value = 1; |
| 409 | assert(self.event.set(true) == true); |
| 410 | |
| 411 | // wait for the receiver to update the value & notify us |
| 412 | assert((try self.event.wait(1 * time.second)) == true); |
| 413 | assert(self.value == 2); |
| 336 | 414 | } |
| 415 | }; |
| 337 | 416 | |
| 338 | | const timeout_ms = if (timeout @intCast(windows.LARGE_INTEGER, ) |
| 339 | | } |
| 340 | | }; |
| 417 | _ = event.reset(); |
| 418 | var context = Context{ |
| 419 | .event = event, |
| 420 | .value = 0, |
| 421 | }; |
| 422 | |
| 423 | var receiver = try std.Thread.spawn(&context, Context.receiver); |
| 424 | defer receiver.wait(); |
| 425 | try context.sender(); |
| 426 | } |
| | \ No newline at end of file |