Locks v0.1.0

Source
A plain lock, and running an initialization exactly once.

Lock

pub struct Lock {
    // private fields
}

A lock that guards nothing in particular: whatever the code between Acquire and Release touches. Not reentrant: acquiring it again on the thread that holds it waits forever. Releasing it is the holder's job; releasing a lock another thread holds, or one nobody holds, is a program error the lock cannot detect. For data that a lock protects, Mutex<T> keeps the two together and releases the lock for you.

Source

Constructors

Lock

pub func Lock() -> Lock

A lock nobody holds.

Returns: the lock

Source

Methods

Acquire

pub func Acquire(self: &Lock)

Takes the lock, waiting while another thread holds it.

Source

TryAcquire

pub func TryAcquire(self: &Lock) -> bool

Takes the lock if nobody holds it, without waiting.

Returns: whether the lock was taken

Source

Release

pub func Release(self: &Lock)

Releases the lock this thread holds, letting one waiting thread take it.

Source

Constants

Unlocked

pub const Unlocked: Lock = Lock { word: 0 };

A lock nobody holds, as a constant, so a module variable can start as one: var Guard: Lock = Lock::Unlocked;.

Source

Once

pub struct Once {
    // private fields
}

Runs one initialization once, however many threads ask for it at once. The first Call runs its function; every other waits until that has returned and then returns without running its own. Everything the function wrote is visible to every thread Call returns on.

Source

Constructors

Once

pub func Once() -> Once

An initialization that has not run.

Returns: the once

Source

Methods

Call

pub func Call(self: &Once, initialize: func())

Runs initialize if no call has run one yet, and returns once an initialization has finished. An initializer that panics ends the process, so a half-finished one is never observed.

NameTypeDescription
initializefunc()the function to run once

Source

IsCompleted

pub func IsCompleted(self: &Once) -> bool

Whether an initialization has finished.

Returns: true once a Call has run its function to the end

Source

Constants

Incomplete

pub const Incomplete: Once = Once { state: 0 };

An initialization that has not run, as a constant, so a module variable can start as one: var Setup: Once = Once::Incomplete;.

Source