core::sync
本頁內容
API 參考只有英文版,由標準函式庫原始碼中的英文文件註解產生。
concurrency primitives.
Every type is a handle over GC buffers: copying it (into a closure,
another thread) shares the one primitive. Blocking goes through the
runtime's address-keyed wait queue (nabi_sync_wait / nabi_sync_wake,
futex semantics; Atomic<i32>::wait / wake expose it); a blocked
thread counts as stopped for the GC.
Structs
AtomicSource
pub struct Atomic<T>A lock-free cell. Send + Sync.
An atomic cell holding
value.
Sourcepub fn load(§self, ordering: LoadOrdering = LoadOrdering::SeqCst) -> T The current value.
Sourcepub fn store(§self, value: T, ordering: StoreOrdering = StoreOrdering::SeqCst) Write
value.
Sourcepub fn swap(§self, value: T, ordering: UpdateOrdering = UpdateOrdering::SeqCst) -> T Write
valueand return the previous one.
Sourcepub fn compare_exchange( self, current: T, new: T, success: UpdateOrdering, failure: LoadOrdering, ) -> Result<T, T>§Ok(previous)if it heldcurrent(nownew), elseErr(actual).failureorders the load of a failed exchange.
Sourcepub fn compare_exchange_weak( self, current: T, new: T, success: UpdateOrdering, failure: LoadOrdering, ) -> Result<T, T>§Never fails spuriously here (same as
compare_exchange).
Sourcepub fn fetch_add(§self, value: T, ordering: UpdateOrdering = UpdateOrdering::SeqCst) -> T Add
value(wrapping) and return the previous value. Integers only: the intrinsic rejectsboolfor add and sub.
Sourcepub fn fetch_sub(§self, value: T, ordering: UpdateOrdering = UpdateOrdering::SeqCst) -> T Subtract
value(wrapping) and return the previous value. Integers only.
Sourcepub fn fetch_and(§self, value: T, ordering: UpdateOrdering = UpdateOrdering::SeqCst) -> T Bitwise-and
valuein and return the previous value.
Sourcepub fn fetch_or(§self, value: T, ordering: UpdateOrdering = UpdateOrdering::SeqCst) -> T Bitwise-or
valuein and return the previous value.
Sourcepub fn fetch_xor(§self, value: T, ordering: UpdateOrdering = UpdateOrdering::SeqCst) -> T Bitwise-xor
valuein and return the previous value.Block while the value is
expected, or untilwake; may return spuriously, so re-check.waitfor at mosttimeout(none left: returns at once).Wake every thread in
waiton this cell.
MutexSource
pub struct Mutex<T>An unlocked mutex owning
value(handed over: the mutex gets its own copy of the collectionsvaluereaches).newwithout the hand-off copy: the caller guarantees that nothing else reachesvalue's collections (a value just made).
Sourcepub fn lock(§self) -> MutexGuard<T> Block until the lock is ours. Bind the guard with
using.fof the protected value, with the lock held for the call (lockandusingin one). The value is the guard's scoped view: it may not leavef(returned, stored, sent), sofis notasync. Hold the guard itself to keep the lock across statements or anawait(lock_async).
Sourcepub fn try_lock(§self) -> MutexGuard<T> | none The guard if the lock is free now, else
none; never blocks.
Sourcepub const fn lock_async(§self) -> LockFuture<T> await m.lock_async()inside async code: suspends the task (not the thread) until the lock is ours.
LockFutureSource
pub struct LockFuture<T>Mutex::lock_async.
MutexGuardSource
pub struct MutexGuard<T>Holds a Mutex locked until dropped (using).
A scoped view of the protected value (the handle itself for GC types). Like the guard it is neither
SendnorSyncand stays inside theusingblock: returning, throwing or sending it, or passing it to a callee that keeps it, is an error (E0106).A deep copy of the protected value, free to outlive the lock.
Replace the protected value (no
deref_mut: Nabi has norefreturns).Replace the protected value with
f(value).
RwLockSource
pub struct RwLock<T>An unlocked lock owning
value(handed over, asMutex::new).
Sourcepub fn read(§self) -> RwLockReadGuard<T> Block until no writer holds the lock, then share it for reading. Bind the guard with
using.
Sourcepub fn write(§self) -> RwLockWriteGuard<T> Block until no reader or writer holds the lock, then take it. Bind the guard with
using.fof the value, with a read lock held for the call. The value is a scoped view, as inMutex::with.fof the value, with the write lock held for the call. The value is a scoped view, as inMutex::with.
Sourcepub fn try_read(§self) -> RwLockReadGuard<T> | none A read guard if no writer holds the lock now, else
none; never blocks.
Sourcepub fn try_write(§self) -> RwLockWriteGuard<T> | none A write guard if the lock is free now, else
none; never blocks.
Sourcepub const fn read_async(§self) -> ReadFuture<T> await l.read_async(): suspends the task (not the thread) while a writer holds the lock.
Sourcepub const fn write_async(§self) -> WriteFuture<T> await l.write_async(): suspends the task until no reader or writer holds the lock.
ReadFutureSource
pub struct ReadFuture<T>RwLock::read_async.
WriteFutureSource
pub struct WriteFuture<T>RwLock::write_async.
RwLockReadGuardSource
pub struct RwLockReadGuard<T>Holds an RwLock shared for reading until dropped (using).
A scoped view of the protected value, as
MutexGuard::get.A deep copy of the protected value, free to outlive the lock.
RwLockWriteGuardSource
pub struct RwLockWriteGuard<T>Holds an RwLock exclusively until dropped (using).
A scoped view of the protected value, as
MutexGuard::get.A deep copy of the protected value, free to outlive the lock.
Replace the value under the write lock (handed over, as
Mutex::new).Replace the protected value with
f(value).
FrozenSource
pub struct Frozen<T>A value made once and only read after: any thread may read it
without a lock. new keeps a copy of the value's collections that
nothing else reaches, and get gives each reader its own copy, so no
one can change what the others read. Sync parts (a Mutex, a cache
that locks itself) stay shared.
Freeze
value: keep a copy of its collections nothing else reaches.A copy of the value: the reader's own to change.
OnceSource
pub struct OnceA
Oncethat has not run.Run
fif no call has; every caller returns after it finished.Whether a
call_oncehas finished.
Trait implementations 1
impl Debug for Once
CondvarSource
pub struct CondvarA condition variable with no waiter.
Sourcepub const fn wait_async<T>(§self, guard: MutexGuard<T>) -> CondvarWait<T> g = await cv.wait_async(g):waitthat suspends the task (not the thread), both for the notification and to lock again. Spurious wakeups as forwait. A task cancelled while it waits takes the lock back if it is free (itsusingguard unlocks it); if another task holds it, the guard's unlock is owed instead and does nothing (the worker never blocks).
Sourcepub fn wait<T>(§self, guard: MutexGuard<T>) -> MutexGuard<T> Unlock
guard's mutex, block until notified (or spuriously), lock it again. Re-check the condition in a loop.Wakes the longest-waiting blocked thread. Tasks in
wait_asyncare all woken (a spurious wakeup for all but one): a parked task's entry may be stale, and waking only it could lose the notify.Wake every thread and task waiting on this condvar.
Trait implementations 1
impl Debug for Condvar
CondvarWaitSource
pub struct CondvarWait<T>Condvar::wait_async.
CellSource
pub struct Cell<T>Single-task interior mutability: neither Send nor Sync.
A cell holding
value.A copy of the value.
Store
value; copies of the cell share it, so they see it too.Store
valueand return the old one.The value, leaving
T::default()in its place.
Enums
LoadOrderingSource
pub enum LoadOrderingMemory ordering of an atomic load, and of the load a failed compare-exchange does. A load cannot release.
StoreOrderingSource
pub enum StoreOrderingMemory ordering of an atomic store. A store cannot acquire.
UpdateOrderingSource
pub enum UpdateOrderingMemory ordering of a read-modify-write: swap, the
fetch_* ops and a compare-exchange that succeeds.
Traits
AtomicPrimitiveSource
pub trait AtomicPrimitiveThe element types Atomic<T> supports.