| ... | ... | @@ -5,70 +5,125 @@ const AtomicRmwOp = builtin.AtomicRmwOp; |
| 5 | 5 | const assert = std.debug.assert; |
| 6 | 6 | const SpinLock = std.SpinLock; |
| 7 | 7 | const linux = std.os.linux; |
| 8 | const windows = std.os.windows; |
| 8 | 9 | |
| 9 | 10 | /// Lock may be held only once. If the same thread |
| 10 | 11 | /// tries to acquire the same mutex twice, it deadlocks. |
| 11 | 12 | /// The Linux implementation is based on mutex3 from |
| 12 | 13 | /// https://www.akkadia.org/drepper/futex.pdf |
| 13 | | pub const Mutex = struct { |
| 14 | pub const Mutex = switch(builtin.os) { |
| 15 | builtin.Os.linux => MutexLinux, |
| 16 | builtin.Os.windows => MutexWindows, |
| 17 | else => MutexSpinLock, |
| 18 | }; |
| 19 | |
| 20 | const MutexLinux = struct { |
| 14 | 21 | /// 0: unlocked |
| 15 | 22 | /// 1: locked, no waiters |
| 16 | 23 | /// 2: locked, one or more waiters |
| 17 | | linux_lock: @typeOf(linux_lock_init), |
| 18 | | |
| 19 | | /// TODO better implementation than spin lock |
| 20 | | spin_lock: @typeOf(spin_lock_init), |
| 21 | | |
| 22 | | const linux_lock_init = if (builtin.os == builtin.Os.linux) i32(0) else {}; |
| 23 | | const spin_lock_init = if (builtin.os != builtin.Os.linux) SpinLock.init() else {}; |
| 24 | lock: i32, |
| 24 | 25 | |
| 25 | 26 | pub const Held = struct { |
| 26 | 27 | mutex: *Mutex, |
| 27 | 28 | |
| 28 | 29 | pub fn release(self: Held) void { |
| 29 | | if (builtin.os == builtin.Os.linux) { |
| 30 | | const c = @atomicRmw(i32, &self.mutex.linux_lock, AtomicRmwOp.Sub, 1, AtomicOrder.Release); |
| 31 | | if (c != 1) { |
| 32 | | _ = @atomicRmw(i32, &self.mutex.linux_lock, AtomicRmwOp.Xchg, 0, AtomicOrder.Release); |
| 33 | | const rc = linux.futex_wake(&self.mutex.linux_lock, linux.FUTEX_WAKE | linux.FUTEX_PRIVATE_FLAG, 1); |
| 34 | | switch (linux.getErrno(rc)) { |
| 35 | | 0 => {}, |
| 36 | | linux.EINVAL => unreachable, |
| 37 | | else => unreachable, |
| 38 | | } |
| 30 | const c = @atomicRmw(i32, &self.mutex.lock, AtomicRmwOp.Sub, 1, AtomicOrder.Release); |
| 31 | if (c != 1) { |
| 32 | _ = @atomicRmw(i32, &self.mutex.lock, AtomicRmwOp.Xchg, 0, AtomicOrder.Release); |
| 33 | const rc = linux.futex_wake(&self.mutex.lock, linux.FUTEX_WAKE | linux.FUTEX_PRIVATE_FLAG, 1); |
| 34 | switch (linux.getErrno(rc)) { |
| 35 | 0 => {}, |
| 36 | linux.EINVAL => unreachable, |
| 37 | else => unreachable, |
| 39 | 38 | } |
| 40 | | } else { |
| 41 | | SpinLock.Held.release(SpinLock.Held{ .spinlock = &self.mutex.spin_lock }); |
| 42 | 39 | } |
| 43 | 40 | } |
| 44 | 41 | }; |
| 45 | 42 | |
| 46 | 43 | pub fn init() Mutex { |
| 47 | | return Mutex{ |
| 48 | | .linux_lock = linux_lock_init, |
| 49 | | .spin_lock = spin_lock_init, |
| 44 | return Mutex { |
| 45 | .lock = 0, |
| 50 | 46 | }; |
| 51 | 47 | } |
| 52 | 48 | |
| 49 | pub fn deinit(self: *Mutex) void {} |
| 50 | |
| 53 | 51 | pub fn acquire(self: *Mutex) Held { |
| 54 | | if (builtin.os == builtin.Os.linux) { |
| 55 | | var c = @cmpxchgWeak(i32, &self.linux_lock, 0, 1, AtomicOrder.Acquire, AtomicOrder.Monotonic) orelse |
| 56 | | return Held{ .mutex = self }; |
| 57 | | if (c != 2) |
| 58 | | c = @atomicRmw(i32, &self.linux_lock, AtomicRmwOp.Xchg, 2, AtomicOrder.Acquire); |
| 59 | | while (c != 0) { |
| 60 | | const rc = linux.futex_wait(&self.linux_lock, linux.FUTEX_WAIT | linux.FUTEX_PRIVATE_FLAG, 2, null); |
| 61 | | switch (linux.getErrno(rc)) { |
| 62 | | 0, linux.EINTR, linux.EAGAIN => {}, |
| 63 | | linux.EINVAL => unreachable, |
| 64 | | else => unreachable, |
| 65 | | } |
| 66 | | c = @atomicRmw(i32, &self.linux_lock, AtomicRmwOp.Xchg, 2, AtomicOrder.Acquire); |
| 52 | var c = @cmpxchgWeak(i32, &self.lock, 0, 1, AtomicOrder.Acquire, AtomicOrder.Monotonic) orelse |
| 53 | return Held{ .mutex = self }; |
| 54 | if (c != 2) |
| 55 | c = @atomicRmw(i32, &self.lock, AtomicRmwOp.Xchg, 2, AtomicOrder.Acquire); |
| 56 | while (c != 0) { |
| 57 | const rc = linux.futex_wait(&self.lock, linux.FUTEX_WAIT | linux.FUTEX_PRIVATE_FLAG, 2, null); |
| 58 | switch (linux.getErrno(rc)) { |
| 59 | 0, linux.EINTR, linux.EAGAIN => {}, |
| 60 | linux.EINVAL => unreachable, |
| 61 | else => unreachable, |
| 67 | 62 | } |
| 68 | | } else { |
| 69 | | _ = self.spin_lock.acquire(); |
| 63 | c = @atomicRmw(i32, &self.lock, AtomicRmwOp.Xchg, 2, AtomicOrder.Acquire); |
| 70 | 64 | } |
| 71 | | return Held{ .mutex = self }; |
| 65 | return Held { .mutex = self }; |
| 66 | } |
| 67 | }; |
| 68 | |
| 69 | const MutexWindows = struct { |
| 70 | lock: ?windows.RTL_CRITICAL_SECTION, |
| 71 | |
| 72 | pub const Held = struct { |
| 73 | mutex: *Mutex, |
| 74 | |
| 75 | pub fn release(self: Held) void { |
| 76 | windows.LeaveCriticalSection(&self.mutex.lock); |
| 77 | } |
| 78 | }; |
| 79 | |
| 80 | fn initOsData(self: *MutexWindows) void { |
| 81 | if (self.lock == null) { |
| 82 | windows.InitializeCriticalSection(&self.lock); |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | pub fn init() Mutex { |
| 87 | return Mutex { |
| 88 | .lock = null, |
| 89 | }; |
| 90 | } |
| 91 | |
| 92 | pub fn deinit(self: *Mutex) void { |
| 93 | windows.DeleteCriticalSection(&self.lock); |
| 94 | self.lock = null; |
| 95 | } |
| 96 | |
| 97 | pub fn acquire(self: *Mutex) Held { |
| 98 | self.initOsData(); |
| 99 | windows.EnterCriticalSection(&self.lock); |
| 100 | return Held { .mutex = self }; |
| 101 | } |
| 102 | }; |
| 103 | |
| 104 | const MutexSpinLock = struct { |
| 105 | /// TODO better implementation than spin lock |
| 106 | lock: SpinLock, |
| 107 | |
| 108 | pub const Held = struct { |
| 109 | mutex: *Mutex, |
| 110 | |
| 111 | pub fn release(self: Held) void { |
| 112 | SpinLock.Held.release(SpinLock.Held { .spinlock = &self.mutex.lock }); |
| 113 | } |
| 114 | }; |
| 115 | |
| 116 | pub fn init() Mutex { |
| 117 | return Mutex { |
| 118 | .lock = SpinLock.init(), |
| 119 | }; |
| 120 | } |
| 121 | |
| 122 | pub fn deinit(self: *Mutex) void {} |
| 123 | |
| 124 | pub fn acquire(self: *Mutex) Held { |
| 125 | _ = self.spin_lock.acquire(); |
| 126 | return Held { .mutex = self }; |
| 72 | 127 | } |
| 73 | 128 | }; |
| 74 | 129 | |
| ... | ... | @@ -90,6 +145,8 @@ test "std.Mutex" { |
| 90 | 145 | var a = &fixed_buffer_allocator.allocator; |
| 91 | 146 | |
| 92 | 147 | var mutex = Mutex.init(); |
| 148 | defer mutex.deinit(); |
| 149 | |
| 93 | 150 | var context = Context{ |
| 94 | 151 | .mutex = &mutex, |
| 95 | 152 | .data = 0, |