DoxigAlpha

tryLockShared

Function parameters

Parameters

#
rwl:*DefaultRwLock

Single-threaded applications use this for deadlock checks in

Types

#
SingleThreadedRwLock
Single-threaded applications use this for deadlock checks in

Attempts to obtain exclusive lock ownership.

Functions

#
tryLock
Attempts to obtain exclusive lock ownership.
lock
Blocks until exclusive lock ownership is acquired.
unlock
Releases a held exclusive lock.
tryLockShared
Attempts to obtain shared lock ownership.
lockShared
Obtains shared lock ownership.
unlockShared
Releases a held shared lock.

Source

Implementation

#
pub fn tryLockShared(rwl: *DefaultRwLock) bool {
    const state = @atomicLoad(usize, &rwl.state, .seq_cst);
    if (state & (IS_WRITING | WRITER_MASK) == 0) {
        _ = @cmpxchgStrong(
            usize,
            &rwl.state,
            state,
            state + READER,
            .seq_cst,
            .seq_cst,
        ) orelse return true;
    }

    if (rwl.mutex.tryLock()) {
        _ = @atomicRmw(usize, &rwl.state, .Add, READER, .seq_cst);
        rwl.mutex.unlock();
        return true;
    }

    return false;
}