Reader-writer locks v0.1.0

Source
A value many threads may read at once, or one may write.

RwLock<T>

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

A value guarded by a lock that many readers or one writer may hold, shared between the threads that hold a handle. Clone gives another handle to the same value. 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

RwLock

pub func RwLock(value: T) -> RwLock<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: &RwLock<T>) -> RwLock<T>

Another handle to the same value.

Returns: the handle

Source

Read

pub func Read(self: &RwLock<T>) -> ReadGuard<T>

Takes a shared hold, waiting while a writer holds the lock.

Returns: the read guard

Source

Write

pub func Write(self: &RwLock<T>) -> WriteGuard<T>

Takes the exclusive hold, waiting while any reader or writer holds the lock.

Returns: the write guard

Source

Destructor

~RwLock

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

Source

ReadGuard<T>

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

A shared hold on an RwLock<T>: the value may be read, and other readers may hold it too.

Source

Type parameters

NameDescription
Tthe type of the guarded value

Methods

Get

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

The guarded value, to read while the guard holds the lock.

Returns: the value's address

Source

Destructor

~ReadGuard

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

Source

WriteGuard<T>

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

The exclusive hold on an RwLock<T>: the value may be read and written, and nobody else holds the lock.

Source

Type parameters

NameDescription
Tthe type of the guarded value

Methods

Get

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

The guarded value, which this thread alone may read and write while the guard holds the lock.

Returns: the value's address

Source

Destructor

~WriteGuard

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

Source