Lyricdev
GitHub

class Mutex

pub class Mutex<T>

A value that one task at a time holds.

Mutex.lock gives the value to its body only, as a place — m.lock { &n => n += 1; } — which ends with the body, so nothing reaches the value outside the lock. A task that finds the mutex held waits, parked, while the other tasks of its thread run. This holds across threads, and on one thread wherever a body waits.

Methods

pub static fn new(value: T): Mutex<T>

Creates a mutex that holds value.

pub fn lock<R, E :: [Error]>(body: fn(&T) -> R throws E): R throws [E, Cancelled]

Runs body while the running task holds the mutex and returns what body returns.

The task waits until no other holds the mutex. The body takes the value's place, { &v => … }: it reads and writes the value itself, and the place is the call's — it ends with the body, and no closure in it holds it. The mutex is released when the body ends, also by a throw.

Errors

Throws what body throws, and Cancelled when the task is cancelled during the wait.