| ... | @@ -1,105 +0,0 @@ |
| 1 | const std = @import("std.zig"); |
| 2 | const builtin = @import("builtin"); |
| 3 | const AtomicOrder = builtin.AtomicOrder; |
| 4 | const AtomicRmwOp = builtin.AtomicRmwOp; |
| 5 | const assert = std.debug.assert; |
| 6 | const expect = std.testing.expect; |
| 7 | const windows = std.os.windows; |
| 8 | |
| 9 | /// Lock may be held only once. If the same thread |
| 10 | /// tries to acquire the same mutex twice, it deadlocks. |
| 11 | /// This type is intended to be initialized statically. If you don't |
| 12 | /// require static initialization, use std.Mutex. |
| 13 | /// On Windows, this mutex allocates resources when it is |
| 14 | /// first used, and the resources cannot be freed. |
| 15 | /// On Linux, this is an alias of std.Mutex. |
| 16 | pub const StaticallyInitializedMutex = switch (builtin.os) { |
| 17 | builtin.Os.linux => std.Mutex, |
| 18 | builtin.Os.windows => struct { |
| 19 | lock: windows.CRITICAL_SECTION, |
| 20 | init_once: windows.RTL_RUN_ONCE, |
| 21 | |
| 22 | pub const Held = struct { |
| 23 | mutex: *StaticallyInitializedMutex, |
| 24 | |
| 25 | pub fn release(self: Held) void { |
| 26 | windows.kernel32.LeaveCriticalSection(&self.mutex.lock); |
| 27 | } |
| 28 | }; |
| 29 | |
| 30 | pub fn init() StaticallyInitializedMutex { |
| 31 | return StaticallyInitializedMutex{ |
| 32 | .lock = undefined, |
| 33 | .init_once = windows.INIT_ONCE_STATIC_INIT, |
| 34 | }; |
| 35 | } |
| 36 | |
| 37 | extern fn initCriticalSection( |
| 38 | InitOnce: *windows.RTL_RUN_ONCE, |
| 39 | Parameter: ?*c_void, |
| 40 | Context: ?*c_void, |
| 41 | ) windows.BOOL { |
| 42 | const lock = @ptrCast(*windows.CRITICAL_SECTION, @alignCast(@alignOf(windows.CRITICAL_SECTION), Parameter)); |
| 43 | windows.kernel32.InitializeCriticalSection(lock); |
| 44 | return windows.TRUE; |
| 45 | } |
| 46 | |
| 47 | /// TODO: once https://github.com/ziglang/zig/issues/287 is solved and std.Mutex has a better |
| 48 | /// implementation of a runtime initialized mutex, remove this function. |
| 49 | pub fn deinit(self: *StaticallyInitializedMutex) void { |
| 50 | windows.InitOnceExecuteOnce(&self.init_once, initCriticalSection, &self.lock, null); |
| 51 | windows.kernel32.DeleteCriticalSection(&self.lock); |
| 52 | } |
| 53 | |
| 54 | pub fn acquire(self: *StaticallyInitializedMutex) Held { |
| 55 | windows.InitOnceExecuteOnce(&self.init_once, initCriticalSection, &self.lock, null); |
| 56 | windows.kernel32.EnterCriticalSection(&self.lock); |
| 57 | return Held{ .mutex = self }; |
| 58 | } |
| 59 | }, |
| 60 | else => std.Mutex, |
| 61 | }; |
| 62 | |
| 63 | test "std.StaticallyInitializedMutex" { |
| 64 | const TestContext = struct { |
| 65 | data: i128, |
| 66 | |
| 67 | const TestContext = @This(); |
| 68 | const incr_count = 10000; |
| 69 | |
| 70 | var mutex = StaticallyInitializedMutex.init(); |
| 71 | |
| 72 | fn worker(ctx: *TestContext) void { |
| 73 | var i: usize = 0; |
| 74 | while (i != TestContext.incr_count) : (i += 1) { |
| 75 | const held = mutex.acquire(); |
| 76 | defer held.release(); |
| 77 | |
| 78 | ctx.data += 1; |
| 79 | } |
| 80 | } |
| 81 | }; |
| 82 | |
| 83 | var plenty_of_memory = try std.heap.direct_allocator.alloc(u8, 300 * 1024); |
| 84 | defer std.heap.direct_allocator.free(plenty_of_memory); |
| 85 | |
| 86 | var fixed_buffer_allocator = std.heap.ThreadSafeFixedBufferAllocator.init(plenty_of_memory); |
| 87 | var a = &fixed_buffer_allocator.allocator; |
| 88 | |
| 89 | var context = TestContext{ .data = 0 }; |
| 90 | |
| 91 | if (builtin.single_threaded) { |
| 92 | TestContext.worker(&context); |
| 93 | expect(context.data == TestContext.incr_count); |
| 94 | } else { |
| 95 | const thread_count = 10; |
| 96 | var threads: [thread_count]*std.Thread = undefined; |
| 97 | for (threads) |*t| { |
| 98 | t.* = try std.Thread.spawn(&context, TestContext.worker); |
| 99 | } |
| 100 | for (threads) |t| |
| 101 | t.wait(); |
| 102 | |
| 103 | expect(context.data == thread_count * TestContext.incr_count); |
| 104 | } |
| 105 | } |