authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2024-06-12 18:07:39-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2024-06-12 18:07:39-07:00
log5fc1f8a32bb7d66c0db04e497b89f7e33f408722
treeffbd491bd4ef97b9169dcfc89ba3c14b60cc03ef
parentfad223d92ed34caac163695cc2a32cba80267c32

std.Thread.Mutex.Recursive: alternate implementation

This version is simpler. Thanks King!

1 files changed, 12 insertions(+), 26 deletions(-)

lib/std/Thread/Mutex/Recursive.zig+12-26
......@@ -30,7 +30,13 @@ pub const init: Recursive = .{
3030/// Otherwise, returns `true` and the caller should `unlock()` the Mutex to release it.
3131pub fn tryLock(r: *Recursive) bool {
3232 const current_thread_id = std.Thread.getCurrentId();
33 return tryLockInner(r, current_thread_id);
33 if (@atomicLoad(std.Thread.Id, &r.thread_id, .unordered) != current_thread_id) {
34 if (!r.mutex.tryLock()) return false;
35 assert(r.lock_count == 0);
36 @atomicStore(std.Thread.Id, &r.thread_id, current_thread_id, .unordered);
37 }
38 r.lock_count += 1;
39 return true;
3440}
3541
3642/// Acquires the `Mutex`, blocking the current thread while the mutex is
......@@ -42,12 +48,12 @@ pub fn tryLock(r: *Recursive) bool {
4248/// of whether the lock was already held by the same thread.
4349pub fn lock(r: *Recursive) void {
4450 const current_thread_id = std.Thread.getCurrentId();
45 if (!tryLockInner(r, current_thread_id)) {
51 if (@atomicLoad(std.Thread.Id, &r.thread_id, .unordered) != current_thread_id) {
4652 r.mutex.lock();
4753 assert(r.lock_count == 0);
48 r.lock_count = 1;
49 @atomicStore(std.Thread.Id, &r.thread_id, current_thread_id, .monotonic);
54 @atomicStore(std.Thread.Id, &r.thread_id, current_thread_id, .unordered);
5055 }
56 r.lock_count += 1;
5157}
5258
5359/// Releases the `Mutex` which was previously acquired with `lock` or `tryLock`.
......@@ -57,30 +63,10 @@ pub fn lock(r: *Recursive) void {
5763pub fn unlock(r: *Recursive) void {
5864 r.lock_count -= 1;
5965 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);
66 @atomicStore(std.Thread.Id, &r.thread_id, invalid_thread_id, .unordered);
6467 r.mutex.unlock();
6568 }
6669}
6770
68fn 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
8571/// A value that does not alias any other thread id.
86const invalid_thread_id: std.Thread.Id = 0;
72const invalid_thread_id: std.Thread.Id = std.math.maxInt(std.Thread.Id);