DoxigAlpha

tryLock

Acquires the Mutex without blocking the caller's thread.

Returns false if the calling thread would have to block to acquire it.

Otherwise, returns true and the caller should unlock() the Mutex to release it.

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 tryLock(r: *Recursive) bool {
    const current_thread_id = std.Thread.getCurrentId();
    if (@atomicLoad(std.Thread.Id, &r.thread_id, .unordered) != current_thread_id) {
        if (!r.mutex.tryLock()) return false;
        assert(r.lock_count == 0);
        @atomicStore(std.Thread.Id, &r.thread_id, current_thread_id, .unordered);
    }
    r.lock_count += 1;
    return true;
}