| author | |
| committer | |
| log | 35d93d22db6f1cdf20011b0db84586107513b462 |
| tree | 50a55d1cb60ed27bfad5566be98b75956125f1f4 |
| parent | 93851270526e71407b4225a23daa66214a4010f9 |
2 files changed, 29 insertions(+), 38 deletions(-)
std/mutex.zig+24-31| ... | @@ -62,57 +62,50 @@ pub const Mutex = switch(builtin.os) { | ... | @@ -62,57 +62,50 @@ pub const Mutex = switch(builtin.os) { |
| 62 | }, | 62 | }, |
| 63 | builtin.Os.windows => struct { | 63 | builtin.Os.windows => struct { |
| 64 | 64 | ||
| 65 | lock: ?windows.CRITICAL_SECTION, | 65 | lock: windows.CRITICAL_SECTION, |
| 66 | init_once: windows.PINIT_ONCE, | 66 | init_once: windows.RTL_RUN_ONCE, |
| 67 | 67 | ||
| 68 | pub const Held = struct { | 68 | pub const Held = struct { |
| 69 | mutex: *Mutex, | 69 | mutex: *Mutex, |
| 70 | 70 | ||
| 71 | pub fn release(self: Held) void { | 71 | pub fn release(self: Held) void { |
| 72 | if (self.mutex.lock) |*lock| { | 72 | windows.LeaveCriticalSection(&self.mutex.lock); |
| 73 | windows.LeaveCriticalSection(lock); | ||
| 74 | } | ||
| 75 | } | 73 | } |
| 76 | }; | 74 | }; |
| 77 | 75 | ||
| 78 | pub fn init() Mutex { | 76 | pub fn init() Mutex { |
| 79 | return Mutex { | 77 | return Mutex { |
| 80 | .lock = null, | 78 | .lock = undefined, |
| 81 | .init_once = undefined, | 79 | .init_once = undefined, |
| 82 | }; | 80 | }; |
| 83 | } | 81 | } |
| 84 | 82 | ||
| 85 | extern fn initCriticalSection(InitOnce: *windows.PINIT_ONCE, Parameter: ?windows.PVOID, Context: ?windows.PVOID) windows.BOOL { | 83 | extern fn initCriticalSection( |
| 86 | if (Context) |ctx| { | 84 | InitOnce: *windows.RTL_RUN_ONCE, |
| 87 | var mutex = @ptrCast(*?windows.CRITICAL_SECTION, @alignCast(8, ctx)); | 85 | Parameter: ?windows.PVOID, |
| 88 | if (mutex.* == null) { | 86 | Context: ?windows.PVOID |
| 89 | var lock: windows.CRITICAL_SECTION = undefined; | 87 | ) windows.BOOL { |
| 90 | windows.InitializeCriticalSection(&lock); | 88 | var lock = @ptrCast( |
| 91 | mutex.* = lock; | 89 | *windows.CRITICAL_SECTION, |
| 92 | } | 90 | @alignCast(@alignOf(*windows.CRITICAL_SECTION), ctx.?) |
| 93 | return windows.TRUE; | 91 | ); |
| 94 | } | 92 | windows.InitializeCriticalSection(lock); |
| 95 | return windows.FALSE; | 93 | return windows.TRUE; |
| 96 | } | 94 | } |
| 97 | 95 | ||
| 98 | pub fn deinit(self: *Mutex) void { | 96 | pub fn deinit(self: *Mutex) void { |
| 99 | if (self.lock) |*lock| { | 97 | windows.DeleteCriticalSection(&self.lock); |
| 100 | windows.DeleteCriticalSection(lock); | ||
| 101 | } | ||
| 102 | } | 98 | } |
| 103 | 99 | ||
| 104 | pub fn acquire(self: *Mutex) Held { | 100 | pub fn acquire(self: *Mutex) Held { |
| 105 | if (self.lock) |*lock| { | 101 | if (windows.InitOnceExecuteOnce( |
| 106 | windows.EnterCriticalSection(lock); | 102 | &self.init_once, |
| 107 | } else { | 103 | initCriticalSection, |
| 108 | var mutex = @ptrCast(?windows.PVOID, self); | 104 | null, @ptrCast(?windows.PVOID, self) |
| 109 | if (windows.InitOnceExecuteOnce(&self.init_once, initCriticalSection, null, mutex) == windows.TRUE) { | 105 | ) == windows.FALSE) { |
| 110 | windows.EnterCriticalSection(&self.lock.?); | 106 | unreachable; |
| 111 | } else { | ||
| 112 | @panic("unable to initialize Mutex"); | ||
| 113 | } | ||
| 114 | } | 107 | } |
| 115 | 108 | windows.EnterCriticalSection(&self.lock); | |
| 116 | return Held { .mutex = self }; | 109 | return Held { .mutex = self }; |
| 117 | } | 110 | } |
| 118 | }, | 111 | }, |
std/os/windows/kernel32.zig+5-7| ... | @@ -222,9 +222,9 @@ pub const FOREGROUND_RED = 4; | ... | @@ -222,9 +222,9 @@ pub const FOREGROUND_RED = 4; |
| 222 | pub const FOREGROUND_INTENSITY = 8; | 222 | pub const FOREGROUND_INTENSITY = 8; |
| 223 | 223 | ||
| 224 | pub extern "kernel32" stdcallcc fn InitializeCriticalSection(lpCriticalSection: *CRITICAL_SECTION) void; | 224 | pub extern "kernel32" stdcallcc fn InitializeCriticalSection(lpCriticalSection: *CRITICAL_SECTION) void; |
| 225 | pub extern "kernel32" stdcallcc fn EnterCriticalSection(lpCriticalSection: *CRITICAL_SECTION) void; | 225 | pub extern "kernel32" stdcallcc fn EnterCriticalSection(lpCriticalSection: *CRITICAL_SECTION) void; |
| 226 | pub extern "kernel32" stdcallcc fn LeaveCriticalSection(lpCriticalSection: *CRITICAL_SECTION) void; | 226 | pub extern "kernel32" stdcallcc fn LeaveCriticalSection(lpCriticalSection: *CRITICAL_SECTION) void; |
| 227 | pub extern "kernel32" stdcallcc fn DeleteCriticalSection(lpCriticalSection: *CRITICAL_SECTION) void; | 227 | pub extern "kernel32" stdcallcc fn DeleteCriticalSection(lpCriticalSection: *CRITICAL_SECTION) void; |
| 228 | 228 | ||
| 229 | pub const LIST_ENTRY = extern struct { | 229 | pub const LIST_ENTRY = extern struct { |
| 230 | Flink: *LIST_ENTRY, | 230 | Flink: *LIST_ENTRY, |
| ... | @@ -254,12 +254,10 @@ pub const RTL_CRITICAL_SECTION = extern struct { | ... | @@ -254,12 +254,10 @@ pub const RTL_CRITICAL_SECTION = extern struct { |
| 254 | 254 | ||
| 255 | pub const CRITICAL_SECTION = RTL_CRITICAL_SECTION; | 255 | pub const CRITICAL_SECTION = RTL_CRITICAL_SECTION; |
| 256 | 256 | ||
| 257 | pub extern "kernel32" stdcallcc fn InitOnceExecuteOnce(InitOnce: *PINIT_ONCE, InitFn: PINIT_ONCE_FN, Context: ?PVOID, Parameter: ?LPVOID) BOOL; | 257 | pub extern "kernel32" stdcallcc fn InitOnceExecuteOnce(InitOnce: *RTL_RUN_ONCE, InitFn: PINIT_ONCE_FN, Context: ?PVOID, Parameter: ?LPVOID) BOOL; |
| 258 | 258 | ||
| 259 | pub const PINIT_ONCE_FN = ?extern fn(InitOnce: *PINIT_ONCE, Parameter: ?PVOID, Context: ?PVOID) BOOL; | 259 | pub const PINIT_ONCE_FN = ?extern fn(InitOnce: *RTL_RUN_ONCE, Parameter: ?PVOID, Context: ?PVOID) BOOL; |
| 260 | 260 | ||
| 261 | pub const RTL_RUN_ONCE = extern struct { | 261 | pub const RTL_RUN_ONCE = extern struct { |
| 262 | Ptr: PVOID, | 262 | Ptr: PVOID, |
| 263 | }; | 263 | }; |
| 264 | |||
| 265 | pub const PINIT_ONCE = RTL_RUN_ONCE; |