authorgravatar for kbutcher6200@gmail.comkprotty <kbutcher6200@gmail.com> 2019-11-23 14:04:31-06:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2019-11-26 20:40:04-05:00
logef208fee3cf7eb25ae08e1a896eba58b91e56d50
treea12918583436add3b756045dd3b96e0e0fdb3482
parent9bce97a479e70f2d8e09047c9a0c93690cd8fd99
signaturelock-open Commit is signed but in an unrecognized format.

Definition fixups & ResetEvent test cases


2 files changed, 237 insertions(+), 151 deletions(-)

lib/std/c.zig+1-1
...@@ -220,7 +220,7 @@ pub extern "c" fn pthread_mutex_destroy(mutex: *pthread_mutex_t) c_int;...@@ -220,7 +220,7 @@ pub extern "c" fn pthread_mutex_destroy(mutex: *pthread_mutex_t) c_int;
220220
221pub const PTHREAD_COND_INITIALIZER = pthread_cond_t{};221pub const PTHREAD_COND_INITIALIZER = pthread_cond_t{};
222pub extern "c" fn pthread_cond_wait(noalias cond: *pthread_cond_t, noalias mutex: *pthread_mutex_t) c_int;222pub extern "c" fn pthread_cond_wait(noalias cond: *pthread_cond_t, noalias mutex: *pthread_mutex_t) c_int;
223pub extern "c" fn pthread_cond_timedwait(noalias: cond: *pthread_cond_t, noalias: mutex: *pthread_mutex_t, noalias abstime: *const timespec) c_int;223pub extern "c" fn pthread_cond_timedwait(noalias cond: *pthread_cond_t, noalias mutex: *pthread_mutex_t, noalias abstime: *const timespec) c_int;
224pub extern "c" fn pthread_cond_signal(cond: *pthread_cond_t) c_int;224pub extern "c" fn pthread_cond_signal(cond: *pthread_cond_t) c_int;
225pub extern "c" fn pthread_cond_destroy(cond: *pthread_cond_t) c_int;225pub extern "c" fn pthread_cond_destroy(cond: *pthread_cond_t) c_int;
226226
lib/std/reset_event.zig+236-150
...@@ -4,9 +4,10 @@ const testing = std.testing;...@@ -4,9 +4,10 @@ const testing = std.testing;
4const assert = std.debug.assert;4const assert = std.debug.assert;
5const Backoff = std.SpinLock.Backoff;5const Backoff = std.SpinLock.Backoff;
6const c = std.c;6const c = std.c;
7const os = std.os;
7const time = std.time;8const time = std.time;
8const linux = std.os.linux;9const linux = os.linux;
9const windows = std.os.windows;10const windows = os.windows;
1011
11/// A resource object which supports blocking until signaled.12/// A resource object which supports blocking until signaled.
12/// Once finished, the `deinit()` method should be called for correctness.13/// Once finished, the `deinit()` method should be called for correctness.
...@@ -23,15 +24,15 @@ pub const ResetEvent = struct {...@@ -23,15 +24,15 @@ pub const ResetEvent = struct {
23 }24 }
2425
25 /// Returns whether or not the event is currenetly set26 /// 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 return self.os_event.isSet();28 return self.os_event.isSet();
28 }29 }
29 30
30 /// Sets the event if not already set and31 /// Sets the event if not already set and
31 /// wakes up AT LEAST one thread waiting the event.32 /// wakes up AT LEAST one thread waiting the event.
32 /// Returns whether or not a thread was woken up.33 /// Returns whether or not a thread was woken up.
33 pub fn set(self: *ResetEvent) bool {34 pub fn set(self: *ResetEvent, auto_reset: bool) bool {
34 return self.os_event.set();35 return self.os_event.set(auto_reset);
35 }36 }
3637
37 /// Resets the event to its original, unset state.38 /// Resets the event to its original, unset state.
...@@ -73,15 +74,15 @@ const DebugEvent = struct {...@@ -73,15 +74,15 @@ const DebugEvent = struct {
73 self.* = undefined;74 self.* = undefined;
74 }75 }
7576
76 pub fn isSet(self: *const DebugEvent) bool {77 pub fn isSet(self: *DebugEvent) bool {
77 if (!std.debug.runtime_safety)78 if (!std.debug.runtime_safety)
78 return true;79 return true;
79 return self.is_set;80 return self.is_set;
80 }81 }
8182
82 pub fn set(self: *DebugEvent) bool {83 pub fn set(self: *DebugEvent, auto_reset: bool) bool {
83 if (std.debug.runtime_safety)84 if (std.debug.runtime_safety)
84 self.is_set = true;85 self.is_set = !auto_reset;
85 return false;86 return false;
86 }87 }
8788
...@@ -100,102 +101,87 @@ const DebugEvent = struct {...@@ -100,102 +101,87 @@ const DebugEvent = struct {
100 }101 }
101};102};
102103
103fn EventState(comptime TagType: type) type {104fn AtomicEvent(comptime FutexImpl: type) type {
104 return enum(TagType) {105 return struct {
105 Empty,106 state: u32,
106 Waiting,
107 Signaled,
108 };
109}
110107
111const SpinEvent = struct {108 const IS_SET: u32 = 1 << 0;
112 state: State,109 const WAIT_MASK = ~IS_SET;
113110
114 const State = EventState(u8);111 pub const Self = @This();
112 pub const Futex = FutexImpl;
115113
116 pub fn init() SpinEvent {114 pub fn init() Self {
117 return SpinEvent{ .state = .Empty };115 return Self{ .state = 0 };
118 }116 }
119117
120 pub fn deinit(self: *SpinEvent) void {118 pub fn deinit(self: *Self) void {
121 self.* = undefined;119 self.* = undefined;
122 }120 }
123121
124 pub fn isSet(self: *const SpinEvent) bool {122 pub fn isSet(self: *const Self) bool {
125 return @atomicLoad(State, &self.state, .Acquire) == .Signaled;123 const state = @atomicLoad(u32, &self.state, .Acquire);
126 }124 return (state & IS_SET) != 0;
125 }
127126
128 pub fn set(self: *SpinEvent) bool {127 pub fn reset(self: *Self) bool {
129 return @atomicRmw(State, &self.state, .Xchg, .Signaled, .Release) == .Waiting;128 const old_state = @atomicRmw(u32, &self.state, .Xchg, 0, .Monotonic);
130 }129 return (old_state & IS_SET) != 0;
130 }
131131
132 pub fn reset(self: *SpinEvent) bool {132 pub fn set(self: *Self, auto_reset: bool) bool {
133 return @atomicRmw(State, &self.state, .Xchg, .Empty, .Monotonic) == .Signaled;133 const new_state = if (auto_reset) 0 else IS_SET;
134 }134 const old_state = @atomicRmw(u32, &self.state, .Xchg, new_state, .Release);
135 if ((old_state & WAIT_MASK) == 0) {
136 return false;
137 }
135138
136 pub fn wait(self: *SpinEvent, timeout: ?u64) ResetEvent.WaitError!bool {139 Futex.wake(&self.state);
137 var state = @atomicLoad(State, &self.state, .Monotonic);140 return true;
138 while (true) {141 }
139 switch (state) {142
140 .Empty => state = @cmpxchgWeak(State, &self.state, state, .Waiting, .Acquire, .Monotonic) orelse break,143 pub fn wait(self: *Self, timeout: ?u64) ResetEvent.WaitError!bool {
141 .Waiting => break,144 var dummy_value: u32 = undefined;
142 .Signaled => return false,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
160const SpinEvent = AtomicEvent(struct {
161 fn wake(ptr: *const u32) void {}
145162
146 // TODO: handle case for time.Timer.start() fails163 fn wait(ptr: *const u32, expected: u32, timeout: ?u64) ResetEvent.WaitError!void {
164 // TODO: handle platforms where time.Timer.start() fails
147 var spin = Backoff.init();165 var spin = Backoff.init();
148 var timer = if (timeout == null) null else time.Timer.start() catch unreachable;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 spin.yield();168 spin.yield();
151 if (timeout) |timeout_ns| {169 if (timeout) |timeout_ns| {
152 if (timer.?.read() > timeout_ns)170 if (timer.?.read() > timeout_ns)
153 return ResetEvent.WaitError.TimedOut;171 return ResetEvent.WaitError.TimedOut;
154 }172 }
155 }173 }
156 return true;
157 }
158};
159
160const 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});
176176
177 pub fn set(self: *LinuxEvent) bool {177const LinuxEvent = AtomicEvent(struct {
178 if (@atomicRmw(State, &self.state, .Xchg, .Signaled, .Release) != .Waiting)178 fn wake(ptr: *const u32) void {
179 return false;179 const key = @ptrCast(*const i32, ptr);
180 const rc = linux.futex_wake(@ptrCast(*const i32, &self.state), linux.FUTEX_WAKE | linux.FUTEX_PRIVATE_FLAG, 1);180 const rc = linux.futex_wake(key, linux.FUTEX_WAKE | linux.FUTEX_PRIVATE_FLAG, 1);
181 assert(linux.getErrno(rc) == 0);181 assert(linux.getErrno(rc) == 0);
182 return true;
183 }182 }
184183
185 pub fn reset(self: *LinuxEvent) bool {184 fn wait(ptr: *const u32, expected: u32, timeout: ?u64) ResetEvent.WaitError!void {
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
199 var ts: linux.timespec = undefined;185 var ts: linux.timespec = undefined;
200 var ts_ptr: ?*linux.timespec = null;186 var ts_ptr: ?*linux.timespec = null;
201 if (timeout) |timeout_ns| {187 if (timeout) |timeout_ns| {
...@@ -204,28 +190,94 @@ const LinuxEvent = struct {...@@ -204,28 +190,94 @@ const LinuxEvent = struct {
204 ts.tv_nsec = @intCast(isize, timeout_ns % time.ns_per_s);190 ts.tv_nsec = @intCast(isize, timeout_ns % time.ns_per_s);
205 }191 }
206192
207 while (@atomicLoad(State, &self.state, .Monotonic) == .Waiting) {193 const key = @ptrCast(*const i32, ptr);
208 const rc = linux.futex_wait(@ptrCast(*const i32, &self.state), linux.FUTEX_WAIT | linux.FUTEX_PRIVATE_FLAG, @enumToInt(State.Waiting), ts_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 switch (linux.getErrno(rc)) {197 switch (linux.getErrno(rc)) {
210 0, linux.EINTR => continue,198 0, linux.EAGAIN => break,
211 linux.EAGAIN => break,199 linux.EINTR => continue,
212 linux.ETIMEDOUT => return ResetEvent.WaitError.TimedOut,200 linux.ETIMEDOUT => return ResetEvent.WaitError.TimedOut,
213 else => unreachable,201 else => unreachable,
214 }202 }
215 }203 }
216 }204 }
217};205});
206
207const 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});
218270
219const PosixEvent = struct {271const PosixEvent = struct {
220 state: State,272 state: u32,
221 cond: c.pthread_cond_t,273 cond: c.pthread_cond_t,
222 mutex: c.pthread_mutex_t,274 mutex: c.pthread_mutex_t,
223275
224 const State = EventState(u8);276 const IS_SET: u32 = 1;
225277
226 pub fn init() PosixEvent {278 pub fn init() PosixEvent {
227 return PosixEvent{279 return PosixEvent{
228 .state = .Empty,280 .state = .0,
229 .cond = c.PTHREAD_COND_INITIALIZER,281 .cond = c.PTHREAD_COND_INITIALIZER,
230 .mutex = c.PTHREAD_MUTEX_INITIALIZER,282 .mutex = c.PTHREAD_MUTEX_INITIALIZER,
231 };283 };
...@@ -234,107 +286,141 @@ const PosixEvent = struct {...@@ -234,107 +286,141 @@ const PosixEvent = struct {
234 pub fn deinit(self: *PosixEvent) void {286 pub fn deinit(self: *PosixEvent) void {
235 // On dragonfly, the destroy functions return EINVAL if they were initialized statically.287 // On dragonfly, the destroy functions return EINVAL if they were initialized statically.
236 const retm = c.pthread_mutex_destroy(&self.mutex);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 const retc = c.pthread_cond_destroy(&self.cond);290 const retc = c.pthread_cond_destroy(&self.cond);
239 assert(retc == 0 or retc == (if (builtin.os == .dragonfly) std.os.EINVAL else 0));291 assert(retc == 0 or retc == (if (builtin.os == .dragonfly) os.EINVAL else 0));
240 self.* = undefined;
241 }292 }
242293
243 pub fn isSet(self: *const PosixEvent) bool {294 pub fn isSet(self: *PosixEvent) bool {
244 assert(c.pthread_mutex_lock(&self.mutex) == 0);295 assert(c.pthread_mutex_lock(&self.mutex) == 0);
245 defer assert(c.pthread_mutex_unlock(&self.mutex) == 0);296 defer assert(c.pthread_mutex_unlock(&self.mutex) == 0);
246297
247 return self.state == .Signaled;298 return self.state == IS_SET;
248 }299 }
249300
250 pub fn set(self: *PosixEvent) bool {301 pub fn reset(self: *PosixEvent) bool {
251 assert(c.pthread_mutex_lock(&self.mutex) == 0);302 assert(c.pthread_mutex_lock(&self.mutex) == 0);
252 defer assert(c.pthread_mutex_unlock(&self.mutex) == 0);303 defer assert(c.pthread_mutex_unlock(&self.mutex) == 0);
253304
254 const woken = self.state == .Waiting;305 const was_set = self.state == IS_SET;
255 self.state = .Signaled;306 self.state = 0;
256 return woken;307 return was_set;
257 }308 }
258309
259 pub fn reset(self: *PosixEvent) bool {310 pub fn set(self: *PosixEvent, auto_reset: bool) bool {
260 assert(c.pthread_mutex_lock(&self.mutex) == 0);311 assert(c.pthread_mutex_lock(&self.mutex) == 0);
261 defer assert(c.pthread_mutex_unlock(&self.mutex) == 0);312 defer assert(c.pthread_mutex_unlock(&self.mutex) == 0);
262313
263 const was_set = self.state == .Signaled;314 const had_waiter = self.state > IS_SET;
264 self.state = .Empty;315 self.state = if (auto_reset) 0 else IS_SET;
265 return was_set;316 if (had_waiter) {
317 assert(c.pthread_cond_signal(&self.cond) == 0);
318 }
319 return had_waiter;
266 }320 }
267321
268 pub fn wait(self: *PosixEvent, timeout: ?u64) ResetEvent.WaitError!bool {322 pub fn wait(self: *PosixEvent, timeout: ?u64) ResetEvent.WaitError!bool {
269 assert(c.pthread_mutex_lock(&self.mutex) == 0);323 assert(c.pthread_mutex_lock(&self.mutex) == 0);
270 defer assert(c.pthread_mutex_unlock(&self.mutex) == 0);324 defer assert(c.pthread_mutex_unlock(&self.mutex) == 0);
271325
272 if (self.state == .Signaled)326 if (self.state == IS_SET)
273 return false;327 return false;
274328
275 var ts: std.os.timespec = undefined;329 var ts: os.timespec = undefined;
276 var ts_ptr = &ts;330 var ts_ptr = &ts;
277 if (timeout) |timeout_ns| {331 if (timeout) |timeout_ns| {
278 var tv: std.os.timeval = undefined;332 var tv: os.timeval = undefined;
279 assert(c.gettimeofday(&tv, null) == 0);333 assert(c.gettimeofday(&tv, null) == 0);
280 ts.tv_sec = @intCast(isize, tv.tv_sec + (timeout_ns / time.ns_per_s));334 ts.tv_sec = tv.tv_sec + @intCast(isize, timeout_ns / time.ns_per_s);
281 ts.tv_nsec = @intCast(isize, (tv.tv_usec * time.microsecond) + (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 }
283337
284 self.state = .Waiting;338 var dummy_value: u32 = undefined;
285 while (self.state == .Waiting) {339 var wait_token = @truncate(u32, @ptrToInt(&dummy_value));
340 self.state = wait_token;
341
342 while (self.state == wait_token) {
286 const rc = switch (timeout == null) {343 const rc = switch (timeout == null) {
287 true => c.pthread_cond_wait(&self.cond, &self.mutex),344 true => c.pthread_cond_wait(&self.cond, &self.mutex),
288 else => c.pthread_cond_timedwait(&self.cond, &self.mutex, ts_ptr),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};
294359
295const WindowsEvent = struct {360test "std.ResetEvent" {
296 state: State,361 // TODO
297362 if (builtin.single_threaded)
298 const State = EventState(u32);363 return error.SkipZigTest;
299364
300 pub fn init() WindowsEvent {365 var event = ResetEvent.init();
301 return WindowsEvent{ .state = .Empty };366 defer event.deinit();
302 }367
303368 // test event setting
304 pub fn deinit(self: *WindowsEvent) void {369 testing.expect(event.isSet() == false);
305 self.* = undefined;370 testing.expect(event.set(false) == false);
306 }371 testing.expect(event.isSet() == true);
307372
308 pub fn isSet(self: *const WindowsEvent) bool {373 // test event resetting
309 return @atomicLoad(State, &self.state, .Acquire) == .Signaled;374 testing.expect(event.reset() == true);
310 }375 testing.expect(event.isSet() == false);
311376 testing.expect(event.reset() == false);
312 pub fn set(self: *WindowsEvent) bool {377
313 if (@atomicRmw(State, &self.state, .Xchg, .Signaled, .Release) != .Waiting)378 // test waiting timeout
314 return false;379 const delay = 100 * time.millisecond;
315380 var timer = time.Timer.start() catch unreachable;
316 if (getEventHandle()) |handle| {381 testing.expectError(ResetEvent.WaitError.TimedOut, event.wait(delay));
317 const key = @ptrCast(*const c_void, &self.state);382 const elapsed = timer.read();
318 const rc = windows.ntdll.NtReleaseKeyedEvent(handle, key, windows.FALSE, null);383 testing.expect(elapsed >= delay and elapsed < delay * 2);
319 assert(rc == 0);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 }
323401
324 pub fn reset(self: *WindowsEvent) bool {402 fn sender(self: *@This()) !void {
325 return @atomicRmw(State, &self.state, .Xchg, .Empty, .Monotonic) == .Signaled;403 // wait for the receiver() to start wait()'ing
326 }404 time.sleep(50 * time.millisecond);
327405
328 pub fn wait(self: *WindowsEvent, timeout: ?u64) ResetEvent.WaitError!bool {406 // update value to 1 and notify the receiver()
329 var state = @atomicLoad(State, &self.state, .Monotonic);407 assert(self.value == 0);
330 while (true) {408 self.value = 1;
331 switch (state) {409 assert(self.event.set(true) == true);
332 .Empty => state = @cmpxchgWeak(State, &self.state, .Empty, .Waiting, .Acquire, .Monotonic) orelse break,410
333 .Waiting => break,411 // wait for the receiver to update the value & notify us
334 .Signaled => return false,412 assert((try self.event.wait(1 * time.second)) == true);
335 }413 assert(self.value == 2);
336 }414 }
415 };
337416
338 const timeout_ms = if (timeout @intCast(windows.LARGE_INTEGER, )417 _ = event.reset();
339 }418 var context = Context{
340};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