structstd.Thread.RwLock.SingleThreadedRwLock[src]

Single-threaded applications use this for deadlock checks in debug mode, and no-ops in release modes.

Fields

state: enum { unlocked, locked_exclusive, locked_shared } = .unlocked
shared_count: usize = 0

Functions

FunctiontryLock[src]

pub fn tryLock(rwl: *SingleThreadedRwLock) bool

Attempts to obtain exclusive lock ownership. Returns true if the lock is obtained, false otherwise.

Parameters

Source Code

Source code
pub fn tryLock(rwl: *SingleThreadedRwLock) bool {
    switch (rwl.state) {
        .unlocked => {
            assert(rwl.shared_count == 0);
            rwl.state = .locked_exclusive;
            return true;
        },
        .locked_exclusive, .locked_shared => return false,
    }
}

Functionlock[src]

pub fn lock(rwl: *SingleThreadedRwLock) void

Blocks until exclusive lock ownership is acquired.

Parameters

Source Code

Source code
pub fn lock(rwl: *SingleThreadedRwLock) void {
    assert(rwl.state == .unlocked); // deadlock detected
    assert(rwl.shared_count == 0); // corrupted state detected
    rwl.state = .locked_exclusive;
}

Functionunlock[src]

pub fn unlock(rwl: *SingleThreadedRwLock) void

Releases a held exclusive lock. Asserts the lock is held exclusively.

Parameters

Source Code

Source code
pub fn unlock(rwl: *SingleThreadedRwLock) void {
    assert(rwl.state == .locked_exclusive);
    assert(rwl.shared_count == 0); // corrupted state detected
    rwl.state = .unlocked;
}

FunctiontryLockShared[src]

pub fn tryLockShared(rwl: *SingleThreadedRwLock) bool

Attempts to obtain shared lock ownership. Returns true if the lock is obtained, false otherwise.

Parameters

Source Code

Source code
pub fn tryLockShared(rwl: *SingleThreadedRwLock) bool {
    switch (rwl.state) {
        .unlocked => {
            rwl.state = .locked_shared;
            assert(rwl.shared_count == 0);
            rwl.shared_count = 1;
            return true;
        },
        .locked_shared => {
            rwl.shared_count += 1;
            return true;
        },
        .locked_exclusive => return false,
    }
}

FunctionlockShared[src]

pub fn lockShared(rwl: *SingleThreadedRwLock) void

Blocks until shared lock ownership is acquired.

Parameters

Source Code

Source code
pub fn lockShared(rwl: *SingleThreadedRwLock) void {
    switch (rwl.state) {
        .unlocked => {
            rwl.state = .locked_shared;
            assert(rwl.shared_count == 0);
            rwl.shared_count = 1;
        },
        .locked_shared => {
            rwl.shared_count += 1;
        },
        .locked_exclusive => unreachable, // deadlock detected
    }
}

FunctionunlockShared[src]

pub fn unlockShared(rwl: *SingleThreadedRwLock) void

Releases a held shared lock.

Parameters

Source Code

Source code
pub fn unlockShared(rwl: *SingleThreadedRwLock) void {
    switch (rwl.state) {
        .unlocked => unreachable, // too many calls to `unlockShared`
        .locked_exclusive => unreachable, // exclusively held lock
        .locked_shared => {
            rwl.shared_count -= 1;
            if (rwl.shared_count == 0) {
                rwl.state = .unlocked;
            }
        },
    }
}

Source Code

Source code
pub const SingleThreadedRwLock = struct {
    state: enum { unlocked, locked_exclusive, locked_shared } = .unlocked,
    shared_count: usize = 0,

    /// Attempts to obtain exclusive lock ownership.
    /// Returns `true` if the lock is obtained, `false` otherwise.
    pub fn tryLock(rwl: *SingleThreadedRwLock) bool {
        switch (rwl.state) {
            .unlocked => {
                assert(rwl.shared_count == 0);
                rwl.state = .locked_exclusive;
                return true;
            },
            .locked_exclusive, .locked_shared => return false,
        }
    }

    /// Blocks until exclusive lock ownership is acquired.
    pub fn lock(rwl: *SingleThreadedRwLock) void {
        assert(rwl.state == .unlocked); // deadlock detected
        assert(rwl.shared_count == 0); // corrupted state detected
        rwl.state = .locked_exclusive;
    }

    /// Releases a held exclusive lock.
    /// Asserts the lock is held exclusively.
    pub fn unlock(rwl: *SingleThreadedRwLock) void {
        assert(rwl.state == .locked_exclusive);
        assert(rwl.shared_count == 0); // corrupted state detected
        rwl.state = .unlocked;
    }

    /// Attempts to obtain shared lock ownership.
    /// Returns `true` if the lock is obtained, `false` otherwise.
    pub fn tryLockShared(rwl: *SingleThreadedRwLock) bool {
        switch (rwl.state) {
            .unlocked => {
                rwl.state = .locked_shared;
                assert(rwl.shared_count == 0);
                rwl.shared_count = 1;
                return true;
            },
            .locked_shared => {
                rwl.shared_count += 1;
                return true;
            },
            .locked_exclusive => return false,
        }
    }

    /// Blocks until shared lock ownership is acquired.
    pub fn lockShared(rwl: *SingleThreadedRwLock) void {
        switch (rwl.state) {
            .unlocked => {
                rwl.state = .locked_shared;
                assert(rwl.shared_count == 0);
                rwl.shared_count = 1;
            },
            .locked_shared => {
                rwl.shared_count += 1;
            },
            .locked_exclusive => unreachable, // deadlock detected
        }
    }

    /// Releases a held shared lock.
    pub fn unlockShared(rwl: *SingleThreadedRwLock) void {
        switch (rwl.state) {
            .unlocked => unreachable, // too many calls to `unlockShared`
            .locked_exclusive => unreachable, // exclusively held lock
            .locked_shared => {
                rwl.shared_count -= 1;
                if (rwl.shared_count == 0) {
                    rwl.state = .unlocked;
                }
            },
        }
    }
}