mutex
import std::sync::mutex; · source
Types
Mutex<T, A>
type struct Mutex<T, A = GlobalAlloc> where A: Allocator {
inner: MutexInner<T, A>*;
static new(value: T) -> Mutex<T, GlobalAlloc>;
static new_in(value: T, alloc: A) -> Mutex<T, A>;
static try_new(value: T) -> Result<Mutex<T, GlobalAlloc>, AllocError>;
static try_new_in(value: T, alloc: A) -> Result<Mutex<T, A>, AllocError>;
lock(&this) -> MutexGuard<T, A>;
try_lock(&this) -> Option<MutexGuard<T, A>>;
raw_pthread_buf(&this) -> u8*;
}
At most one thread at a time can read or mutate the guarded value.
import std::sync::mutex;
mut m: Mutex<Counter> = Mutex::<Counter>::new(counter);
mut g: MutexGuard<Counter, GlobalAlloc> = m.lock();
g.as_ptr().hits = g.as_ptr().hits + 1;
Properties
| Property | Type |
|---|---|
inner | MutexInner<T, A>* |
Constructors
static new(value: T) -> Mutex<T, GlobalAlloc>;
static new(value: T) -> Mutex<T, GlobalAlloc>;Construct a Mutex<T> initialized to hold value, using GlobalAlloc for the inner allocation. Panics on allocation failure; use try_new to handle the error.
static new_in(value: T, alloc: A) -> Mutex<T, A>;
static try_new(value: T) -> Result<Mutex<T, GlobalAlloc>, AllocError>;
static try_new(value: T) -> Result<Mutex<T, GlobalAlloc>, AllocError>;Fallible constructor. Initializes the pthread mutex with default attributes (PTHREAD_MUTEX_NORMAL). Returns Err(AllocError) either when the inner buffer cannot be allocated or when pthread_mutex_init itself fails (ENOMEM or EAGAIN under resource pressure); in the latter case the just-allocated buffer is released before returning so the caller never leaks on the error path.
static try_new_in(value: T, alloc: A) -> Result<Mutex<T, A>, AllocError>;
Methods
lock(&this) -> MutexGuard<T, A>;
lock(&this) -> MutexGuard<T, A>;Acquire the lock, blocking the current thread until it is available. Returns a guard whose drop() will release the lock. Panics only on a programming error reported by pthread_mutex_lock (EINVAL from a recursively-locked non-recursive mutex or a destroyed mutex).
try_lock(&this) -> Option<MutexGuard<T, A>>;
try_lock(&this) -> Option<MutexGuard<T, A>>;Non-blocking lock attempt. Returns Some(guard) when the lock was acquired; None when the lock was already held by another thread.
raw_pthread_buf(&this) -> u8*;
raw_pthread_buf(&this) -> u8*;Raw pointer to the inner pthread buffer. Used by CondVar to pair the mutex with a condition variable in pthread_cond_wait; not a stable public API.
MutexGuard<T, A>
type struct MutexGuard<T, A> {
inner: MutexInner<T, A>*;
as_ptr(&this) -> T*;
}
lock() blocks; try_lock() returns None if the lock is held. The guard dereferences through as_ptr() -> T*, valid for the guard's lifetime, and releases the lock when its drop runs. raw_pthread_buf exposes the underlying primitive for CondVar, which needs to hand it to pthread_cond_wait.
Properties
| Property | Type |
|---|---|
inner | MutexInner<T, A>* |
Methods
as_ptr(&this) -> T*;
as_ptr(&this) -> T*;Pointer to the guarded value. Valid for the lifetime of this guard; never null.
Trait implementations
Mutex
implement<T, A> trait Drop for struct Mutex<T, A> where A: Allocator
MutexGuard
implement<T, A> trait Drop for struct MutexGuard<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 Mutex<T, A = GlobalAlloc> where A: Allocator {
inner: MutexInner<T, A>*;
}Mutex<T>: mutual-exclusion lock around a value.
type parameter Ttype parameter Awhere A: Allocator
type struct GlobalAlloc {}Zero-sized handle to the process-wide allocator. Construct with GlobalAlloc::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>, AllocError>;
deallocate(mut &this, ptr: NonNull<u8>, layout: Layout) -> void;
reallocate(mut &this, ptr: NonNull<u8>, old_layout: Layout, new_layout: Layout) -> Result<NonNull<u8>, AllocError>;
}Allocator: the trait every heap strategy implements.
std::alloc::allocatorMutex.inner: MutexInner<T, A>*type struct MutexInner<T, A> {
pthread_buf: [u8;
40];
value: T;
alloc: A;
}type parameter Tstatic 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.
static Mutex::new(value: T) -> Mutex<T, GlobalAlloc>Construct a Mutex<T> initialized to hold value, using GlobalAlloc for the inner allocation. Panics on allocation failure; use try_new to handle the error.
(parameter) value: Ttype struct Mutex<T, A = GlobalAlloc> where A: Allocator {
inner: MutexInner<T, A>*;
}Mutex<T>: mutual-exclusion lock around a value.
std::sync::mutexstatic Mutex::new_in(value: T, alloc: A) -> Mutex<T, A>(parameter) alloc: Astatic Mutex::try_new(value: T) -> Result<Mutex<T, GlobalAlloc>, AllocError>Fallible constructor. Initializes the pthread mutex with default attributes (PTHREAD_MUTEX_NORMAL).
type struct AllocError {
kind: AllocErrorKind;
layout: Layout;
}static Mutex::try_new_in(value: T, alloc: A) -> Result<Mutex<T, A>, AllocError>Mutex::lock(&this) -> MutexGuard<T, A>Acquire the lock, blocking the current thread until it is available. Returns a guard whose drop() will release the lock. Panics only on a programming error reported by pthread_mutex_lock (EINVAL from a recursively-locked non-recursive mutex or a destroyed mutex).
&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 struct MutexGuard<T, A> {
inner: MutexInner<T, A>*;
}RAII guard returned by Mutex::lock. Carries a raw pointer to the inner so it can release the lock on drop. Deref via as_ptr() to read or mutate the guarded value.
Mutex::try_lock(&this) -> Option<MutexGuard<T, A>>Non-blocking lock attempt. Returns Some(guard) when the lock was acquired; None when the lock was already held by another thread.
Mutex::raw_pthread_buf(&this) -> u8*Raw pointer to the inner pthread buffer. Used by CondVar to pair the mutex with a condition variable in pthread_cond_wait; not a stable public API.
import 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).
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.
mut m: Mutex<Counter>static Mutex::new(value: T) -> Mutex<T, GlobalAlloc>Construct a Mutex<T> initialized to hold value, using GlobalAlloc for the inner allocation. Panics on allocation failure; use try_new to handle the error.
mut g: MutexGuard<Counter, GlobalAlloc>mut m: Mutex<Counter>Mutex::lock(&this) -> MutexGuard<T, A>Acquire the lock, blocking the current thread until it is available. Returns a guard whose drop() will release the lock. Panics only on a programming error reported by pthread_mutex_lock (EINVAL from a recursively-locked non-recursive mutex or a destroyed mutex).
mut g: MutexGuard<Counter, GlobalAlloc>MutexGuard::as_ptr(&this) -> T*Pointer to the guarded value. Valid for the lifetime of this guard; never null.
type struct MutexGuard<T, A> {
inner: MutexInner<T, A>*;
}RAII guard returned by Mutex::lock. Carries a raw pointer to the inner so it can release the lock on drop. Deref via as_ptr() to read or mutate the guarded value.
type parameter AMutexGuard.inner: MutexInner<T, A>*type parameter Ttype parameter AMutexGuard::as_ptr(&this) -> T*Pointer to the guarded value. Valid for the lifetime of this guard; never null.
MutexGuard.inner: MutexInner<T, A>*MutexInner.value: Tstatic Mutex::try_new(value: T) -> Result<Mutex<T, GlobalAlloc>, AllocError>Fallible constructor. Initializes the pthread mutex with default attributes (PTHREAD_MUTEX_NORMAL).
static Mutex::new_in(value: T, alloc: A) -> Mutex<T, A>MutexType::PTHREAD_MUTEX_NORMAL = 0Result::Err(E)static Mutex::try_new_in(value: T, alloc: A) -> Result<Mutex<T, A>, AllocError>Mutex::try_lock(&this) -> Option<MutexGuard<T, A>>Non-blocking lock attempt. Returns Some(guard) when the lock was acquired; None when the lock was already held by another thread.
Option::Some(T)Option::NoneMutex::raw_pthread_buf(&this) -> u8*Raw pointer to the inner pthread buffer. Used by CondVar to pair the mutex with a condition variable in pthread_cond_wait; not a stable public API.
type struct CondVar {
inner: CondVarInner*;
}CondVar: condition variable for blocking until a predicate holds.
std::sync::condvarnullThe 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.
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.
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