Skip to content
CryoCryo home
Stdlibsync

read_write_lock

import std::sync::read_write_lock; · source

Types

ReadWriteLock<T, A>

type struct ReadWriteLock<T, A = GlobalAllocator> where A: Allocator {
    private inner: mut ReadWriteLockInner<T, A>*;
}

Many concurrent readers or one exclusive writer. Reach for it over a Mutex when the value is read often and written rarely, since the read path does not serialize. read() and write() block; try_read() and try_write() return None when the lock cannot be taken at once. The constructors follow Mutex: new, new_in with an allocator, and the fallible try_new / try_new_in.

import std::alloc::allocator::{ GlobalAllocator };
import std::sync::read_write_lock::{ ReadWriteLock, ReadWriteLockReadGuard };

const table: ReadWriteLock<i64> = ReadWriteLock::<i64>::new(5);
const reader: ReadWriteLockReadGuard<i64, GlobalAllocator> = table.read();
const value: i64 = *reader.as_pointer();

Constructors

static new(value: T) -> ReadWriteLock<T, GlobalAllocator>;

Construct an ReadWriteLock holding value, its block allocated from GlobalAllocator. Panics when the block cannot be allocated or the lock cannot be initialised; use try_new to handle the error.

static new_in(value: T, alloc: A) -> ReadWriteLock<T, A>;

Construct an ReadWriteLock holding value, its block allocated from alloc, which is kept to free it on drop. Panics when the block cannot be allocated or the lock cannot be initialised; use try_new_in to handle the error.

static try_new(value: T) -> Result<ReadWriteLock<T, GlobalAllocator>, AllocationError>;

Construct an ReadWriteLock holding value, its block allocated from GlobalAllocator. Returns the allocator's AllocationError when the block cannot be allocated, and an OutOfMemory error, after freeing the block, when pthread_rwlock_init fails (unix; the Windows initialisation cannot fail).

static try_new_in(value: T, alloc: A) -> Result<ReadWriteLock<T, A>, AllocationError>;

Construct an ReadWriteLock holding value, its block allocated from alloc. Errors as try_new does.

Methods

read(&this) -> ReadWriteLockReadGuard<T, A>;

Acquire a shared read lock, blocking until available.

try_read(&this) -> Option<ReadWriteLockReadGuard<T, A>>;

Take a shared read lock without waiting: Some(guard) when it was taken, None when a writer holds the lock (on unix, also when pthread_rwlock_tryrdlock refuses for any other reason, such as the reader limit).

write(&this) -> ReadWriteLockWriteGuard<T, A>;

Acquire the exclusive write lock, blocking until no other readers or writers hold it.

try_write(&this) -> Option<ReadWriteLockWriteGuard<T, A>>;

Take the exclusive write lock without waiting: Some(guard) when it was taken, None when any reader or writer holds the lock.

The guards

type struct ReadWriteLockReadGuard<T, A> {
    private inner: mut ReadWriteLockInner<T, A>*;
}
type struct ReadWriteLockWriteGuard<T, A> {
    private inner: mut ReadWriteLockInner<T, A>*;
}

The read guard reaches the value through as_pointer(); the write guard adds as_mutable_pointer(). Both release the lock when dropped, and both are !Send. Multiple read guards may coexist; a write guard excludes everything else.

ReadWriteLockReadGuard
Methods

####### as_pointer(&this) -> T*;

Read-only pointer to the guarded value. Valid for the lifetime of this guard; never null.

ReadWriteLockWriteGuard
Methods

####### as_pointer(&this) -> T*;

Read-only pointer to the guarded value.

####### as_mutable_pointer(mut &this) -> mut T*;

Writable pointer to the guarded value, from a guard held writably.

Trait implementations

ReadWriteLock
implement<T, A> trait Drop for struct ReadWriteLock<T, A> where A: Allocator
ReadWriteLockReadGuard
implement<T, A> trait Drop for struct ReadWriteLockReadGuard<T, A>
ReadWriteLockWriteGuard
implement<T, A> trait Drop for struct ReadWriteLockWriteGuard<T, A>