| ... | ... | @@ -5,11 +5,28 @@ const testing = std.testing; |
| 5 | 5 | const SpinLock = std.SpinLock; |
| 6 | 6 | const ResetEvent = std.ResetEvent; |
| 7 | 7 | |
| 8 | | /// Lock may be held only once. If the same thread |
| 9 | | /// tries to acquire the same mutex twice, it deadlocks. |
| 10 | | /// This type supports static initialization and is at most `@sizeOf(usize)` in size. |
| 11 | | /// When an application is built in single threaded release mode, all the functions are |
| 12 | | /// no-ops. In single threaded debug mode, there is deadlock detection. |
| 8 | /// Lock may be held only once. If the same thread tries to acquire |
| 9 | /// the same mutex twice, it deadlocks. This type supports static |
| 10 | /// initialization and is at most `@sizeOf(usize)` in size. When an |
| 11 | /// application is built in single threaded release mode, all the |
| 12 | /// functions are no-ops. In single threaded debug mode, there is |
| 13 | /// deadlock detection. |
| 14 | /// |
| 15 | /// Example usage: |
| 16 | /// var m = Mutex.init(); |
| 17 | /// defer m.deinit(); |
| 18 | /// |
| 19 | /// const lock = m.acquire(); |
| 20 | /// defer lock.release(); |
| 21 | /// ... critical code |
| 22 | /// |
| 23 | /// Non-blocking: |
| 24 | /// if (m.tryAcquire) |lock| { |
| 25 | /// defer lock.release(); |
| 26 | /// // ... critical section |
| 27 | /// } else { |
| 28 | /// // ... lock not acquired |
| 29 | /// } |
| 13 | 30 | pub const Mutex = if (builtin.single_threaded) |
| 14 | 31 | struct { |
| 15 | 32 | lock: @TypeOf(lock_init), |
| ... | ... | @@ -26,14 +43,21 @@ pub const Mutex = if (builtin.single_threaded) |
| 26 | 43 | } |
| 27 | 44 | }; |
| 28 | 45 | |
| 46 | /// Create a new mutex in unlocked state. |
| 29 | 47 | pub fn init() Mutex { |
| 30 | 48 | return Mutex{ .lock = lock_init }; |
| 31 | 49 | } |
| 32 | 50 | |
| 51 | /// Free a mutex created with init. Calling this while the |
| 52 | /// mutex is held may result in safety-checked undefined |
| 53 | /// behavior. |
| 33 | 54 | pub fn deinit(self: *Mutex) void { |
| 34 | 55 | self.* = undefined; |
| 35 | 56 | } |
| 36 | 57 | |
| 58 | /// Try to acquire the mutex without blocking. Returns null if |
| 59 | /// the mutex is unavailable. Otherwise returns Held. Call |
| 60 | /// release on Held. |
| 37 | 61 | pub fn tryAcquire(self: *Mutex) ?Held { |
| 38 | 62 | if (std.debug.runtime_safety) { |
| 39 | 63 | if (self.lock) return null; |
| ... | ... | @@ -42,6 +66,8 @@ pub const Mutex = if (builtin.single_threaded) |
| 42 | 66 | return Held{ .mutex = self }; |
| 43 | 67 | } |
| 44 | 68 | |
| 69 | /// Acquire the mutex. Will deadlock if the mutex is already |
| 70 | /// held by the calling thread. |
| 45 | 71 | pub fn acquire(self: *Mutex) Held { |
| 46 | 72 | return self.tryAcquire() orelse @panic("deadlock detected"); |
| 47 | 73 | } |
| ... | ... | @@ -200,9 +226,12 @@ else if (builtin.link_libc or builtin.os == .linux) |
| 200 | 226 | } |
| 201 | 227 | } |
| 202 | 228 | |
| 229 | /// Returned when the lock is acquired. Call release to |
| 230 | /// release. |
| 203 | 231 | pub const Held = struct { |
| 204 | 232 | mutex: *Mutex, |
| 205 | 233 | |
| 234 | /// Release the held lock. |
| 206 | 235 | pub fn release(self: Held) void { |
| 207 | 236 | // first, remove the lock bit so another possibly parallel acquire() can succeed. |
| 208 | 237 | // use .Sub since it can be usually compiled down more efficiency |