diff options
| author | Andrew Kelley <andrew@ziglang.org> | 2020-01-06 14:07:56 -0500 |
|---|---|---|
| committer | Andrew Kelley <andrew@ziglang.org> | 2020-01-06 14:07:56 -0500 |
| commit | 0a9daeb37e997ff75dcd16d1fc3b4cc143314e85 (patch) | |
| tree | 05ca7f6b64b1e40fc16a595816f3a632a986617c /lib/std/reset_event.zig | |
| parent | c30106c90665079f525129e344cc1c13e4db162b (diff) | |
| parent | d09bd3d86c4d36ad608a91b36c9a6eb6208c9626 (diff) | |
| download | zig-0a9daeb37e997ff75dcd16d1fc3b4cc143314e85.tar.gz zig-0a9daeb37e997ff75dcd16d1fc3b4cc143314e85.zip | |
Merge branch 'cc-work' of https://github.com/LemonBoy/zig into LemonBoy-cc-work
Diffstat (limited to 'lib/std/reset_event.zig')
| -rw-r--r-- | lib/std/reset_event.zig | 26 |
1 files changed, 12 insertions, 14 deletions
diff --git a/lib/std/reset_event.zig b/lib/std/reset_event.zig index 86bc5ba5aa..b9d9517c6d 100644 --- a/lib/std/reset_event.zig +++ b/lib/std/reset_event.zig @@ -14,13 +14,12 @@ const windows = os.windows; pub const ResetEvent = struct { os_event: OsEvent, - pub const OsEvent = - if (builtin.single_threaded) - DebugEvent - else if (builtin.link_libc and builtin.os != .windows and builtin.os != .linux) - PosixEvent - else - AtomicEvent; + pub const OsEvent = if (builtin.single_threaded) + DebugEvent + else if (builtin.link_libc and builtin.os != .windows and builtin.os != .linux) + PosixEvent + else + AtomicEvent; pub fn init() ResetEvent { return ResetEvent{ .os_event = OsEvent.init() }; @@ -105,7 +104,7 @@ const PosixEvent = struct { } fn deinit(self: *PosixEvent) void { - // on dragonfly, *destroy() functions can return EINVAL + // on dragonfly, *destroy() functions can return EINVAL // for statically initialized pthread structures const err = if (builtin.os == .dragonfly) os.EINVAL else 0; @@ -212,8 +211,7 @@ const AtomicEvent = struct { fn wait(self: *AtomicEvent, timeout: ?u64) !void { var waiters = @atomicLoad(u32, &self.waiters, .Acquire); while (waiters != WAKE) { - waiters = @cmpxchgWeak(u32, &self.waiters, waiters, waiters + WAIT, .Acquire, .Acquire) - orelse return Futex.wait(&self.waiters, timeout); + waiters = @cmpxchgWeak(u32, &self.waiters, waiters, waiters + WAIT, .Acquire, .Acquire) orelse return Futex.wait(&self.waiters, timeout); } } @@ -281,7 +279,7 @@ const AtomicEvent = struct { pub fn wake(waiters: *u32, wake_count: u32) void { const handle = getEventHandle() orelse return SpinFutex.wake(waiters, wake_count); const key = @ptrCast(*const c_void, waiters); - + var waiting = wake_count; while (waiting != 0) : (waiting -= 1) { const rc = windows.ntdll.NtReleaseKeyedEvent(handle, key, windows.FALSE, null); @@ -408,7 +406,7 @@ test "std.ResetEvent" { // wait for receiver to update value and signal output self.out.wait(); testing.expect(self.value == 2); - + // update value and signal final input self.value = 3; self.in.set(); @@ -418,12 +416,12 @@ test "std.ResetEvent" { // wait for sender to update value and signal input self.in.wait(); assert(self.value == 1); - + // update value and signal output self.in.reset(); self.value = 2; self.out.set(); - + // wait for sender to update value and signal final input self.in.wait(); assert(self.value == 3); |
