DoxigAlpha

lock

Acquires the Mutex, blocking the current thread while the mutex is already held by another thread.

The Mutex can be held multiple times by the same thread.

Once acquired, call unlock on the Mutex to release it, regardless of whether the lock was already held by the same thread.

Function parameters

Parameters

#
r:*Recursive

Acquires the `Mutex` without blocking the caller's thread.

Functions

#
tryLock
Acquires the `Mutex` without blocking the caller's thread.
lock
Acquires the `Mutex`, blocking the current thread while the mutex is
unlock
Releases the `Mutex` which was previously acquired with `lock` or `tryLock`.

= .{ .mutex = .{}, .thread_id = invalid_thread_id, .lock_count = 0, }

Values

#
init
= .{ .mutex = .{}, .thread_id = invalid_thread_id, .lock_count = 0, }

Source

Implementation

#
pub fn lock(r: *Recursive) void {
    const current_thread_id = std.Thread.getCurrentId();
    if (@atomicLoad(std.Thread.Id, &r.thread_id, .unordered) != current_thread_id) {
        r.mutex.lock();
        assert(r.lock_count == 0);
        @atomicStore(std.Thread.Id, &r.thread_id, current_thread_id, .unordered);
    }
    r.lock_count += 1;
}