Mutex
Owns one retained exclusive mutex and its movable payload. Creation may allocate; guards retain payload/native storage independently. Separate readonly published borrows may acquire concurrently. Mutating/moving the owner handle itself requires external synchronization. Blocking acquisition occupies the calling thread; try acquisition reports contention as an optional. Guards release on their acquiring thread and cannot cross await. No fairness, lock upgrade or recursive exclusive acquisition is promised.
Module std.sync
import std.sync;Public declaration contracts and authored examples. A page can document several related symbols. Implementation bodies are excluded.
Mutex
pub final class Mutex<T> where T: MovableOwns one retained exclusive mutex and its movable payload. Creation may allocate; guards retain payload/native storage independently. Separate readonly published borrows may acquire concurrently. Mutating/moving the owner handle itself requires external synchronization. Blocking acquisition occupies the calling thread; try acquisition reports contention as an optional. Guards release on their acquiring thread and cannot cross await. No fairness, lock upgrade or recursive exclusive acquisition is promised. T: Movable payload protected by guard-controlled interior access.
Example
module example.mutex;
from std.sync import Mutex, MutexGuard;
fn Main() -> i32 {
var Owner = {
// Keep writable result storage through the explicit noncopy payload transfer.
var OwnerCreation = Mutex<i32>.TryCreate(40);
switch OwnerCreation {
case .Ok { Value = Value: mut Mutex<i32>& } => break move(Value);
case .Err { Error = Error } => { if Error.Value != 40 { return 2; } return 1; }
}
};
{
var Guard = {
// Keep writable result storage through the explicit noncopy payload transfer.
var GuardCreation = Owner.Lock();
switch GuardCreation {
case .Ok { Value = Value: mut MutexGuard<i32>& } => break move(Value);
case .Err => return 3;
}
};
Guard.Value() += 2;
let Readonly: readonly MutexGuard<i32>& = Guard;
if Readonly.Value() != 42 { return 4; }
switch Owner.TryLock() { case .Ok { Value = Value } => { if Value is .Some { Value = _ } { return 5; } } case .Err => return 6; }
}
var Moved = move(Owner);
if !Owner.IsEmpty { return 7; }
// The writable wrapper owns the optional guard through its transfer.
var LockAttempt = Moved.TryLock();
switch LockAttempt {
case .Ok { Value = Value: mut MutexGuard<i32>?& } => { switch move(Value) { case .Some { Value = Guard } => { if Guard.Value() != 42 { return 8; } } case .None => return 9; } }
case .Err => return 10;
}
return 0;
}Mutex.init
pub init()Creates an inert owner without allocation.
Mutex.init
pub init(Source: readonly Self&) = delete;Rejects copying owner authority. Source: Existing owner.
Mutex.init
pub init(Source: mut Self&&)Transfers owner storage without invalidating independently retained guards. Source: Owner left inert.
Mutex.operator
pub fn operator =(Source: readonly Self&) -> void = delete;Rejects copying owner authority. Source: Existing owner. Return: No runtime result; this assignment is deleted and cannot be invoked.
Mutex.operator
pub fn operator =(Source: mut Self&&) -> voidReleases previous visible ownership and transfers new storage without blocking. Source: Owner left inert; self-move is preserved by shared transport. Return: No value after transferring shared state and releasing displaced visible ownership without waiting.
Mutex.IsEmpty
pub IsEmpty: bool;Reports inert/default/moved ownership.
Mutex.TryCreate
pub static fn TryCreate(mut Value: T) -> Result<Self, LockCreationError<T>>Allocates native/shared storage and moves the input only into successful ownership. Value: Owned payload, returned in LockCreationError on failure. Return: Initialized owner or exact portable error retaining Value.
Mutex.Lock
pub readonly fn Lock() -> Result<MutexGuard<T>, LockError>Blocks this thread until guarded access or provider failure. Return: Retained guard or portable error.
Mutex.TryLock
pub readonly fn TryLock() -> Result<MutexGuard<T>?, LockError>Attempts access without blocking on contention. Return: Optional retained guard, empty for contention, or portable error.