authorgravatar for kbutcher6200@gmail.comkprotty <kbutcher6200@gmail.com> 2019-11-03 21:09:51-06:00
committergravatar for kbutcher6200@gmail.comkprotty <kbutcher6200@gmail.com> 2019-11-03 21:09:51-06:00
logbb6ad1a6c21694b4ea9340c552962b5ea60712e4
treea3f2b38adfc46fdee9357e6592f87ccf4d29b193
parent0860b1919b3b6f08540f4148a61dd3cc43d7a9a1

Remove StaticallyInitializedMutex


2 files changed, 0 insertions(+), 106 deletions(-)

lib/std/statically_initialized_mutex.zig deleted-105
......@@ -1,105 +0,0 @@
1const std = @import("std.zig");
2const builtin = @import("builtin");
3const AtomicOrder = builtin.AtomicOrder;
4const AtomicRmwOp = builtin.AtomicRmwOp;
5const assert = std.debug.assert;
6const expect = std.testing.expect;
7const 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.
16pub 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
63test "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}
lib/std/std.zig-1
......@@ -19,7 +19,6 @@ pub const Progress = @import("progress.zig").Progress;
1919pub const SegmentedList = @import("segmented_list.zig").SegmentedList;
2020pub const SinglyLinkedList = @import("linked_list.zig").SinglyLinkedList;
2121pub const SpinLock = @import("spinlock.zig").SpinLock;
22pub const StaticallyInitializedMutex = @import("statically_initialized_mutex.zig").StaticallyInitializedMutex;
2322pub const StringHashMap = @import("hash_map.zig").StringHashMap;
2423pub const TailQueue = @import("linked_list.zig").TailQueue;
2524pub const Target = @import("target.zig").Target;