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>;
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>;
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>;
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>;
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>;
read(&this) -> ReadWriteLockReadGuard<T, A>;Acquire a shared read lock, blocking until available.
try_read(&this) -> Option<ReadWriteLockReadGuard<T, A>>;
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>;
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>>;
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*;
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*;
as_pointer(&this) -> T*;Read-only pointer to the guarded value.
####### as_mutable_pointer(mut &this) -> mut T*;
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>
type struct Name { ... }
type enum Name { ... }
type trait Name { ... }
type union Name { ... }Begins a type declaration; the keyword that follows says which kind. On its own, type Name = Existing; declares an alias.
type struct Name {
field: Type;
method(&this) -> Ret { ... }
}A value type with named fields and optional methods. Structs live on the stack and are passed by value. Fields are public by default and may carry = default values; methods take their receiver as &this, mut &this, or this.
type struct ReadWriteLock<T, A = GlobalAllocator> where A: Allocator {}A value of type T that many threads may read at once or one thread may write. read returns a guard for shared reading and write one for exclusive writing; dropping a guard releases its hold. The lock and the value live in one block allocated from A, so the handle can move.
type parameter Ttype parameter Awhere A: Allocator
type struct GlobalAllocator {}Zero-sized handle to the process-wide allocator. Construct with GlobalAllocator::new(); all instances dispatch through the same underlying backend.
where T: Ord + Clone, U: HashConstrains generic parameters to types that implement the listed traits, which is what makes those traits' methods callable on T. Bounds on one parameter join with +; parameters separate with ,. A where on a single method narrows the type's parameter for that method only.
type trait Allocator {
allocate(mut &this, layout: Layout) -> Result<NonNull<u8>, AllocationError>;
deallocate(mut &this, ptr: NonNull<u8>, layout: Layout) -> void;
reallocate(mut &this, ptr: NonNull<u8>, old_layout: Layout, new_layout: Layout) -> Result<NonNull<u8>, AllocationError>;
}A source of heap memory: hands out blocks described by a Layout and takes them back. Collections, Box and String take an A: Allocator type parameter, so the storage strategy is the caller's choice; GlobalAllocator is the default.
private type struct Name { ... }
private:Confined to the module that declares it - for items and members alike. A private field or function reached from another module is E0353, and a private type named there is E0503. private: opens a block of such members.
ReadWriteLock.inner: mut ReadWriteLockInner<T, A>*mut name: Type = value;A mutable binding, reassignable after declaration - mutability is opt-in, const is the default. At module level it declares mutable global state. Before & it marks an exclusive reference or receiver instead.
type parameter Timport a::b;
import a::b::{ X, Y };
import a::b as c;
import a::b::*;Brings a module, or selected items from it, into scope. One path per declaration: use the brace form for several items, as to alias, and * for everything public (prefer the brace form; wildcards invite name collisions).
namespace std::sync::read_write_lock;ReadWriteLock<T>: read/write reader-writer lock around a value.
module pagetype struct ReadWriteLock<T, A = GlobalAllocator> where A: Allocator {}A value of type T that many threads may read at once or one thread may write. read returns a guard for shared reading and write one for exclusive writing; dropping a guard releases its hold. The lock and the value live in one block allocated from A, so the handle can move.
type struct ReadWriteLockReadGuard<T, A> {}Shared-read guard. Many of these may exist concurrently on the same ReadWriteLock. .as_pointer() yields a read-only pointer; only the write guard has as_mutable_pointer(). The guard's own pointer is writable because releasing the shared lock writes the lock word.
const NAME: Type = value;At module level, a true compile-time constant, conventionally SCREAMING_SNAKE_CASE. (mut at module level declares mutable global state instead.)
const table: ReadWriteLock<i64>static ReadWriteLock::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.
const reader: ReadWriteLockReadGuard<i64, GlobalAllocator>const table: ReadWriteLock<i64>ReadWriteLock::read(&this) -> ReadWriteLockReadGuard<T, A>Acquire a shared read lock, blocking until available.
std::sync::read_write_lockconst value: i64const reader: ReadWriteLockReadGuard<i64, GlobalAllocator>ReadWriteLockReadGuard::as_pointer(&this) -> T*Read-only pointer to the guarded value. Valid for the lifetime of this guard; never null.
type struct ReadWriteLockReadGuard<T, A> {}Shared-read guard. Many of these may exist concurrently on the same ReadWriteLock. .as_pointer() yields a read-only pointer; only the write guard has as_mutable_pointer(). The guard's own pointer is writable because releasing the shared lock writes the lock word.
type parameter AReadWriteLockReadGuard.inner: mut ReadWriteLockInner<T, A>*type parameter Ttype parameter Atype struct ReadWriteLockWriteGuard<T, A> {}Exclusive write guard. At most one of these exists on a given ReadWriteLock, and no ReadWriteLockReadGuard can co-exist with it.
ReadWriteLockWriteGuard.inner: mut ReadWriteLockInner<T, A>*type parameter Ttype parameter Atype struct Mutex<T, A = GlobalAllocator> where A: Allocator {}A value of type T that one thread at a time may use. lock waits for the lock and returns a MutexGuard through which the value is read and written; dropping the guard releases the lock. The lock and the value live in one block allocated from A, so the Mutex handle can move while the lock stays put.
ReadWriteLock::write(&this) -> ReadWriteLockWriteGuard<T, A>Acquire the exclusive write lock, blocking until no other readers or writers hold it.
std::sync::read_write_lockReadWriteLock::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).
ReadWriteLock::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.
Option::Nonenew T { field: value }
new Path(args)
new E::Variant
new T[n]Heap allocation. new T { .. } places a struct literal on the heap and yields a T*, and new E::V does the same for a unit variant. new Path(args) places the value a call returns, whatever its type: new Table::new(8), new Option<i64>::Some(41), new make_pair(3, 4). new T[n] reserves n uninitialised T - the typed malloc. The storage comes from the value type's Allocate implementation, or else the global allocator. A type is not a function, so new Point(1, 2) is refused (E0231).
static ReadWriteLock::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 ReadWriteLock::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 ReadWriteLock::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.
static name(param: Type) -> Ret { ... }A method that belongs to the type rather than an instance, called as Type::name(..). For structs, static new(..) returning a struct literal is the idiomatic constructor.
(parameter) value: Tconst value: i64(parameter) alloc: Atype enum Result<T, E> with [Eq, Clone, Debug] {
Ok(T);
Err(E);
}The outcome of an operation that can fail: Ok(value) on success, Err(error) on failure.
type struct AllocationError {
kind: AllocationErrorKind;
layout: Layout;
}The error an Allocator returns: what went wrong (kind) and the layout that was requested when it did (layout; for a reallocate, the new layout).
AllocationErrorKind::OutOfMemoryThe underlying allocator returned null; the system is out of memory or hit an address-space limit.
std::alloc::allocator&this
mut &this
thisThe receiver of a method. Its first parameter declares how the method takes it: &this borrows read-only, mut &this borrows exclusively, and this / mut this move the value in and consume it. In the body, this.field reaches the members.
type enum Option<T> with [Eq, Clone, Debug] {
Some(T);
None;
}A value of type T that may be absent: Some(value) or None.
Option::Some(T)type struct ReadWriteLockWriteGuard<T, A> {}Exclusive write guard. At most one of these exists on a given ReadWriteLock, and no ReadWriteLockReadGuard can co-exist with it.
ReadWriteLockWriteGuard::as_mutable_pointer(mut &this) -> mut T*Writable pointer to the guarded value, from a guard held writably.
std::sync::read_write_locktype trait Send {}Types that are safe to transfer between threads. An owning value of a Send type may cross a thread boundary without violating memory safety.
nullThe null pointer literal, valid in any pointer context - including the primitive string, which is a NUL-terminated u8*. Compare with == null; it is not an Option.
mut &this
mut &TypeAn exclusive, mutating borrow. As a receiver, mut &this lets the method modify the fields - and callers can see that from the signature alone. As a parameter type, mut &T lets the callee mutate through the reference while the caller keeps ownership.
implement struct Name { ... }
implement trait Trait for Type { ... }
implement u64 { ... }Adds methods to a type outside its declaration. implement struct T (or enum, or a bare primitive) opens an inherent block; implement trait Tr for T supplies a trait's methods for one type. Resolution is at compile time - no dynamic dispatch.
type trait Name : Base {
method(&this, other: &This) -> Ret;
}Names a set of methods a type may implement. Inside the body This is the implementing type; methods may carry default bodies, and a trait may require a base trait (type trait Ord : Eq). Generic code asks for a trait with where T: Name.
type trait Drop {
drop(mut &this) -> void;
}A type that releases something when a value of it ends: memory, a file handle, a lock. The compiler calls drop once when an owned value goes out of scope; release a value early with mem::drop(value).
implement trait Trait for Type { ... }Names the type a trait implementation is for. The block that follows supplies the bodies the trait requires.
language reference