v0.4.0

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>

Another handle to the same value.

Returns: the handle

Source

Lock

pub func Lock(&self) -> 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) -> 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(&var self)

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) -> &T

The guarded value, borrowed for reading while the guard holds the lock. The guard stays borrowed while the reference is in use, so it cannot be unlocked or dropped under it. Calling this on a guard that was unlocked is a program error.

Returns: a reference to the value

Source

GetMutable

pub func GetMutable(&var self) -> &var T

The guarded value, borrowed for writing, which this thread alone may do while the guard holds the lock. The guard stays borrowed exclusively while the reference is in use. Calling this on a guard that was unlocked is a program error.

Returns: a writable reference to the value

Source

IsLocked

pub func IsLocked(&self) -> bool

Whether the guard still holds the lock.

Returns: false once Unlock was called

Source

Unlock

pub func Unlock(&var self)

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

Source

Destructor

~MutexGuard

func ~MutexGuard(&var self)

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

Another handle to the same condition.

Returns: the handle

Source

Wait

pub func Wait<T>(&self, 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,
    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)

Wakes one thread waiting on the condition, if any.

Source

NotifyAll

pub func NotifyAll(&self)

Wakes every thread waiting on the condition.

Source

Destructor

~Condition

func ~Condition(&var self)

Source