| ... | ... | @@ -78,12 +78,12 @@ const WindowsParker = struct { |
| 78 | 78 | |
| 79 | 79 | pub fn unpark(self: *WindowsParker, ptr: *const u32) void { |
| 80 | 80 | const key = @ptrCast(*const c_void, ptr); |
| 81 | | const handle_ptr = getEventHandlePtr() orelse return; |
| 81 | const handle = getEventHandle() orelse return; |
| 82 | 82 | |
| 83 | 83 | var waiting = @atomicLoad(u32, &self.waiters, .Monotonic); |
| 84 | 84 | while (waiting != 0) { |
| 85 | 85 | waiting = @cmpxchgWeak(u32, &self.waiters, waiting, waiting - 1, .Acquire, .Monotonic) orelse { |
| 86 | | const rc = windows.ntdll.NtReleaseKeyedEvent(handle_ptr.*, key, windows.FALSE, null); |
| 86 | const rc = windows.ntdll.NtReleaseKeyedEvent(handle, key, windows.FALSE, null); |
| 87 | 87 | assert(rc == 0); |
| 88 | 88 | return; |
| 89 | 89 | }; |
| ... | ... | @@ -92,12 +92,13 @@ const WindowsParker = struct { |
| 92 | 92 | |
| 93 | 93 | pub fn park(self: *WindowsParker, ptr: *const u32, expected: u32) void { |
| 94 | 94 | var spin = SpinLock.Backoff.init(); |
| 95 | const ev_handle = getEventHandle(); |
| 95 | 96 | const key = @ptrCast(*const c_void, ptr); |
| 96 | 97 | |
| 97 | | while (@atomicLoad(u32, ptr, .Acquire) == expected) { |
| 98 | | if (getEventHandlePtr()) |handle_ptr| { |
| 98 | while (@atomicLoad(u32, ptr, .Monotonic) == expected) { |
| 99 | if (ev_handle) |handle| { |
| 99 | 100 | _ = @atomicRmw(u32, &self.waiters, .Add, 1, .Release); |
| 100 | | const rc = windows.ntdll.NtWaitForKeyedEvent(handle_ptr.*, key, windows.FALSE, null); |
| 101 | const rc = windows.ntdll.NtWaitForKeyedEvent(handle, key, windows.FALSE, null); |
| 101 | 102 | assert(rc == 0); |
| 102 | 103 | } else { |
| 103 | 104 | spin.yield(); |
| ... | ... | @@ -107,14 +108,15 @@ const WindowsParker = struct { |
| 107 | 108 | |
| 108 | 109 | var event_handle = std.lazyInit(windows.HANDLE); |
| 109 | 110 | |
| 110 | | fn getEventHandlePtr() ?*const windows.HANDLE { |
| 111 | | return event_handle.get() orelse { |
| 112 | | const access_mask = windows.GENERIC_READ | windows.GENERIC_WRITE; |
| 113 | | if (windows.ntdll.NtCreateKeyedEvent(&event_handle.data, access_mask, null, 0) != 0) |
| 114 | | return null; |
| 115 | | event_handle.resolve(); |
| 116 | | return &event_handle.data; |
| 117 | | }; |
| 111 | fn getEventHandle() ?windows.HANDLE { |
| 112 | if (event_handle.get()) |handle_ptr| |
| 113 | return handle_ptr.*; |
| 114 | defer event_handle.resolve(); |
| 115 | |
| 116 | const access_mask = windows.GENERIC_READ | windows.GENERIC_WRITE; |
| 117 | if (windows.ntdll.NtCreateKeyedEvent(&event_handle.data, access_mask, null, 0) != 0) |
| 118 | return null; |
| 119 | return event_handle.data; |
| 118 | 120 | } |
| 119 | 121 | }; |
| 120 | 122 | |