| ... | ... | @@ -0,0 +1,86 @@ |
| 1 | //! A synchronization primitive enforcing atomic access to a shared region of |
| 2 | //! code known as the "critical section". |
| 3 | //! |
| 4 | //! Equivalent to `std.Mutex` except it allows the same thread to obtain the |
| 5 | //! lock multiple times. |
| 6 | //! |
| 7 | //! A recursive mutex is an abstraction layer on top of a regular mutex; |
| 8 | //! therefore it is recommended to use instead `std.Mutex` unless there is a |
| 9 | //! specific reason a recursive mutex is warranted. |
| 10 | |
| 11 | const std = @import("../../std.zig"); |
| 12 | const Recursive = @This(); |
| 13 | const Mutex = std.Thread.Mutex; |
| 14 | const assert = std.debug.assert; |
| 15 | |
| 16 | mutex: Mutex, |
| 17 | thread_id: std.Thread.Id, |
| 18 | lock_count: usize, |
| 19 | |
| 20 | pub const init: Recursive = .{ |
| 21 | .mutex = .{}, |
| 22 | .thread_id = invalid_thread_id, |
| 23 | .lock_count = 0, |
| 24 | }; |
| 25 | |
| 26 | /// Acquires the `Mutex` without blocking the caller's thread. |
| 27 | /// |
| 28 | /// Returns `false` if the calling thread would have to block to acquire it. |
| 29 | /// |
| 30 | /// Otherwise, returns `true` and the caller should `unlock()` the Mutex to release it. |
| 31 | pub fn tryLock(r: *Recursive) bool { |
| 32 | const current_thread_id = std.Thread.getCurrentId(); |
| 33 | return tryLockInner(r, current_thread_id); |
| 34 | } |
| 35 | |
| 36 | /// Acquires the `Mutex`, blocking the current thread while the mutex is |
| 37 | /// already held by another thread. |
| 38 | /// |
| 39 | /// The `Mutex` can be held multiple times by the same thread. |
| 40 | /// |
| 41 | /// Once acquired, call `unlock` on the `Mutex` to release it, regardless |
| 42 | /// of whether the lock was already held by the same thread. |
| 43 | pub fn lock(r: *Recursive) void { |
| 44 | const current_thread_id = std.Thread.getCurrentId(); |
| 45 | if (!tryLockInner(r, current_thread_id)) { |
| 46 | r.mutex.lock(); |
| 47 | assert(r.lock_count == 0); |
| 48 | r.lock_count = 1; |
| 49 | @atomicStore(std.Thread.Id, &r.thread_id, current_thread_id, .monotonic); |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | /// Releases the `Mutex` which was previously acquired with `lock` or `tryLock`. |
| 54 | /// |
| 55 | /// It is undefined behavior to unlock from a different thread that it was |
| 56 | /// locked from. |
| 57 | pub fn unlock(r: *Recursive) void { |
| 58 | r.lock_count -= 1; |
| 59 | if (r.lock_count == 0) { |
| 60 | // Prevent race where: |
| 61 | // * Thread A obtains lock and has not yet stored the new thread id. |
| 62 | // * Thread B loads the thread id after tryLock() false and observes stale thread id. |
| 63 | @atomicStore(std.Thread.Id, &r.thread_id, invalid_thread_id, .seq_cst); |
| 64 | r.mutex.unlock(); |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | fn tryLockInner(r: *Recursive, current_thread_id: std.Thread.Id) bool { |
| 69 | if (r.mutex.tryLock()) { |
| 70 | assert(r.lock_count == 0); |
| 71 | r.lock_count = 1; |
| 72 | @atomicStore(std.Thread.Id, &r.thread_id, current_thread_id, .monotonic); |
| 73 | return true; |
| 74 | } |
| 75 | |
| 76 | const locked_thread_id = @atomicLoad(std.Thread.Id, &r.thread_id, .monotonic); |
| 77 | if (locked_thread_id == current_thread_id) { |
| 78 | r.lock_count += 1; |
| 79 | return true; |
| 80 | } |
| 81 | |
| 82 | return false; |
| 83 | } |
| 84 | |
| 85 | /// A value that does not alias any other thread id. |
| 86 | const invalid_thread_id: std.Thread.Id = 0; |