Mutexes v0.1.0

Source
A value one thread at a time may reach, and the conditions threads wait on while holding it.

Mutex<T>

pub struct Mutex<T> {
    // private fields
}

A value guarded by a lock, shared between the threads that hold a handle to it. Clone gives another handle to the same value, to move to another thread. The value is destroyed when the last handle and the last guard are gone. A handle is not copied.

Source

Type parameters

NameDescription
Tthe type of the guarded value

Constructors

Mutex

pub func Mutex(value: T) -> Mutex<T>

A new lock guarding value. Running out of memory for it is a program error.

NameTypeDescription
valueTthe value to guard

Returns: the first handle to it

Source

Methods

Clone

pub func Clone(self: &Mutex<T>) -> Mutex<T>

Another handle to the same value.

Returns: the handle

Source

Lock

pub func Lock(self: &Mutex<T>) -> MutexGuard<T>

Takes the lock, waiting while another thread holds it, and returns the guard that holds it. Not reentrant: locking again on the thread that holds the lock waits forever.

Returns: the guard

Source

TryLock

pub func TryLock(self: &Mutex<T>) -> MutexGuard<T>?

Takes the lock if nobody holds it, without waiting.

Returns: the guard, or none when another thread holds the lock

Source

Destructor

~Mutex

func ~Mutex(self: &var Mutex<T>)

Source

MutexGuard<T>

pub struct MutexGuard<T> {
    // private fields
}

The lock on a Mutex<T>, held for as long as the guard lives, and the way to reach the value. Dropping the guard, or calling Unlock, releases the lock. A guard is not copied.

Source

Type parameters

NameDescription
Tthe type of the guarded value

Methods

Get

pub func Get(self: &MutexGuard<T>) -> *var T

The guarded value, which this thread alone may read and write while the guard holds the lock. The address is valid for as long as the guard holds the lock; keeping it longer is a program error.

Returns: the value's address

Source

IsLocked

pub func IsLocked(self: &MutexGuard<T>) -> bool

Whether the guard still holds the lock.

Returns: false once Unlock was called

Source

Unlock

pub func Unlock(self: &var MutexGuard<T>)

Releases the lock before the guard is dropped. Unlocking a guard twice does nothing.

Source

Destructor

~MutexGuard

func ~MutexGuard(self: &var MutexGuard<T>)

Source

Condition

pub struct Condition {
    // private fields
}

A condition threads wait on while holding a Mutex<T>, until another thread says it may hold. A wait releases the lock while it sleeps and takes it again before it returns, so the waiter re-checks the condition under the lock. A wait may also return when nothing notified it, which is why it is always called in a loop that checks the condition. Clone gives another handle to the same condition. A handle is not copied.

Source

Constructors

Condition

pub func Condition() -> Condition

A new condition. Running out of memory for it is a program error.

Returns: the first handle to it

Source

Methods

Clone

pub func Clone(self: &Condition) -> Condition

Another handle to the same condition.

Returns: the handle

Source

Wait

pub func Wait<T>(
    self: &Condition,
    guard: &var MutexGuard<T>
)

Releases the guard's lock, sleeps until notified, and takes the lock again.

Type parameterDescription
Tthe type of the guarded value
NameTypeDescription
guard&var MutexGuard<T>a guard that holds its lock

Source

WaitFor

pub func WaitFor<T>(
    self: &Condition,
    guard: &var MutexGuard<T>,
    timeout: Duration
) -> bool

Releases the guard's lock, sleeps until notified or until timeout has passed, and takes the lock again.

Type parameterDescription
Tthe type of the guarded value
NameTypeDescription
guard&var MutexGuard<T>a guard that holds its lock
timeoutDurationhow long to wait at most; a negative one does not wait

Returns: false when the time ran out without a notification

Source

NotifyOne

pub func NotifyOne(self: &Condition)

Wakes one thread waiting on the condition, if any.

Source

NotifyAll

pub func NotifyAll(self: &Condition)

Wakes every thread waiting on the condition.

Source

Destructor

~Condition

func ~Condition(self: &var Condition)

Source