Mutexes v0.1.0
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.
Type parameters
| Name | Description |
|---|---|
T | the 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.
| Name | Type | Description |
|---|---|---|
value | T | the value to guard |
Returns: the first handle to it
Methods
Clone
pub func Clone(self: &Mutex<T>) -> Mutex<T>
Another handle to the same value.
Returns: the handle
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
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
Destructor
~Mutex
func ~Mutex(self: &var Mutex<T>)
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.
Type parameters
| Name | Description |
|---|---|
T | the 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
IsLocked
pub func IsLocked(self: &MutexGuard<T>) -> bool
Whether the guard still holds the lock.
Returns: false once Unlock was called
Unlock
pub func Unlock(self: &var MutexGuard<T>)
Releases the lock before the guard is dropped. Unlocking a guard twice does nothing.
Destructor
~MutexGuard
func ~MutexGuard(self: &var MutexGuard<T>)
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.
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
Methods
Clone
pub func Clone(self: &Condition) -> Condition
Another handle to the same condition.
Returns: the handle
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 parameter | Description |
|---|---|
T | the type of the guarded value |
| Name | Type | Description |
|---|---|---|
guard | &var MutexGuard<T> | a guard that holds its lock |
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 parameter | Description |
|---|---|
T | the type of the guarded value |
| Name | Type | Description |
|---|---|---|
guard | &var MutexGuard<T> | a guard that holds its lock |
timeout | Duration | how long to wait at most; a negative one does not wait |
Returns: false when the time ran out without a notification
NotifyOne
pub func NotifyOne(self: &Condition)
Wakes one thread waiting on the condition, if any.
NotifyAll
pub func NotifyAll(self: &Condition)
Wakes every thread waiting on the condition.
Destructor
~Condition
func ~Condition(self: &var Condition)