docs

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.

Trait implementations 3
  • impl<T> Debug for Atomic<T>
  • unsafe impl<T> Send for Atomic<bare::ptr::Ptr<T>>
  • unsafe impl<T> Sync for Atomic<bare::ptr::Ptr<T>>

MutexSource

pub struct Mutex<T>
  • pub const fn new(value: T) -> Mutex<T>§

    Source

    An unlocked mutex owning value (handed over: the mutex gets its own copy of the collections value reaches).

  • pub const unsafe fn new_unshared(value: T) -> Mutex<T>§

    Source

    new without the hand-off copy: the caller guarantees that nothing else reaches value's collections (a value just made).

  • pub fn lock(self) -> MutexGuard<T>§

    Source

    Block until the lock is ours. Bind the guard with using.

  • pub fn with<R>(self, f: fn(T) -> R) -> R§

    Source

    f of the protected value, with the lock held for the call (lock and using in one). The value is the guard's scoped view: it may not leave f (returned, stored, sent), so f is not async. Hold the guard itself to keep the lock across statements or an await (lock_async).

  • pub fn try_lock(self) -> MutexGuard<T> | none§

    Source

    The guard if the lock is free now, else none; never blocks.

  • pub const fn lock_async(self) -> LockFuture<T>§

    Source

    await m.lock_async() inside async code: suspends the task (not the thread) until the lock is ours.

Trait implementations 2

LockFutureSource

pub struct LockFuture<T>

Mutex::lock_async.

Trait implementations 2
  • impl<T> Debug for LockFuture<T>
  • impl<T> core::future::Future for LockFuture<T>

MutexGuardSource

pub struct MutexGuard<T>

Holds a Mutex locked until dropped (using).

  • pub const fn get(self) -> T§

    Source

    A scoped view of the protected value (the handle itself for GC types). Like the guard it is neither Send nor Sync and stays inside the using block: returning, throwing or sending it, or passing it to a callee that keeps it, is an error (E0106).

  • pub const fn get_clone(self) -> T where T: Clone§

    Source

    A deep copy of the protected value, free to outlive the lock.

  • pub const fn set(self, value: T)§

    Source

    Replace the protected value (no deref_mut: Nabi has no ref returns).

  • pub const fn update(self, f: fn(T) -> T)§

    Source

    Replace the protected value with f(value).

Trait implementations 3
  • impl<T> Debug for MutexGuard<T>
  • impl<T> !Sync for MutexGuard<T>
  • impl<T> Drop for MutexGuard<T>

RwLockSource

pub struct RwLock<T>
  • pub const fn new(value: T) -> RwLock<T>§

    Source

    An unlocked lock owning value (handed over, as Mutex::new).

  • pub fn read(self) -> RwLockReadGuard<T>§

    Source

    Block until no writer holds the lock, then share it for reading. Bind the guard with using.

  • pub fn write(self) -> RwLockWriteGuard<T>§

    Source

    Block until no reader or writer holds the lock, then take it. Bind the guard with using.

  • pub fn read_with<R>(self, f: fn(T) -> R) -> R§

    Source

    f of the value, with a read lock held for the call. The value is a scoped view, as in Mutex::with.

  • pub fn write_with<R>(self, f: fn(T) -> R) -> R§

    Source

    f of the value, with the write lock held for the call. The value is a scoped view, as in Mutex::with.

  • pub fn try_read(self) -> RwLockReadGuard<T> | none§

    Source

    A read guard if no writer holds the lock now, else none; never blocks.

  • pub fn try_write(self) -> RwLockWriteGuard<T> | none§

    Source

    A write guard if the lock is free now, else none; never blocks.

  • pub const fn read_async(self) -> ReadFuture<T>§

    Source

    await l.read_async(): suspends the task (not the thread) while a writer holds the lock.

  • pub const fn write_async(self) -> WriteFuture<T>§

    Source

    await l.write_async(): suspends the task until no reader or writer holds the lock.

Trait implementations 2

ReadFutureSource

pub struct ReadFuture<T>

RwLock::read_async.

Trait implementations 2
  • impl<T> Debug for ReadFuture<T>
  • impl<T> core::future::Future for ReadFuture<T>

WriteFutureSource

pub struct WriteFuture<T>

RwLock::write_async.

Trait implementations 2
  • impl<T> Debug for WriteFuture<T>
  • impl<T> core::future::Future for WriteFuture<T>

RwLockReadGuardSource

pub struct RwLockReadGuard<T>

Holds an RwLock shared for reading until dropped (using).

  • pub const fn get(self) -> T§

    Source

    A scoped view of the protected value, as MutexGuard::get.

  • pub const fn get_clone(self) -> T where T: Clone§

    Source

    A deep copy of the protected value, free to outlive the lock.

Trait implementations 3
  • impl<T> Debug for RwLockReadGuard<T>
  • impl<T> !Sync for RwLockReadGuard<T>
  • impl<T> Drop for RwLockReadGuard<T>

RwLockWriteGuardSource

pub struct RwLockWriteGuard<T>

Holds an RwLock exclusively until dropped (using).

  • pub const fn get(self) -> T§

    Source

    A scoped view of the protected value, as MutexGuard::get.

  • pub const fn get_clone(self) -> T where T: Clone§

    Source

    A deep copy of the protected value, free to outlive the lock.

  • pub const fn set(self, value: T)§

    Source

    Replace the value under the write lock (handed over, as Mutex::new).

  • pub fn update(self, f: fn(T) -> T)§

    Source

    Replace the protected value with f(value).

Trait implementations 3
  • impl<T> Debug for RwLockWriteGuard<T>
  • impl<T> !Sync for RwLockWriteGuard<T>
  • impl<T> Drop for RwLockWriteGuard<T>

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.

  • pub const fn new(value: T) -> Frozen<T>§

    Source

    Freeze value: keep a copy of its collections nothing else reaches.

  • pub fn get(self) -> T§

    Source

    A copy of the value: the reader's own to change.

Trait implementations 2
  • impl<T> Debug for Frozen<T>
  • unsafe impl<T: Send> Sync for Frozen<T>

OnceSource

pub struct Once
  • pub const fn new() -> Once§

    Source

    A Once that has not run.

  • pub fn call_once(self, f: fn())§

    Source

    Run f if no call has; every caller returns after it finished.

  • pub fn is_completed(self) -> bool§

    Source

    Whether a call_once has finished.

Trait implementations 1

CondvarSource

pub struct Condvar
  • pub const fn new() -> Condvar§

    Source

    A condition variable with no waiter.

  • pub const fn wait_async<T>(self, guard: MutexGuard<T>) -> CondvarWait<T>§

    Source

    g = await cv.wait_async(g): wait that suspends the task (not the thread), both for the notification and to lock again. Spurious wakeups as for wait. A task cancelled while it waits takes the lock back if it is free (its using guard unlocks it); if another task holds it, the guard's unlock is owed instead and does nothing (the worker never blocks).

  • pub fn wait<T>(self, guard: MutexGuard<T>) -> MutexGuard<T>§

    Source

    Unlock guard's mutex, block until notified (or spuriously), lock it again. Re-check the condition in a loop.

  • pub fn notify_one(self)§

    Source

    Wakes the longest-waiting blocked thread. Tasks in wait_async are 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.

  • pub fn notify_all(self)§

    Source

    Wake every thread and task waiting on this condvar.

Trait implementations 1

CondvarWaitSource

pub struct CondvarWait<T>

Condvar::wait_async.

Trait implementations 2
  • impl<T> Debug for CondvarWait<T>
  • impl<T> core::future::Future for CondvarWait<T>

CellSource

pub struct Cell<T>

Single-task interior mutability: neither Send nor Sync.

  • pub const fn new(value: T) -> Cell<T>§

    Source

    A cell holding value.

  • pub const fn get(self) -> T where T: Copy§

    Source

    A copy of the value.

  • pub const fn set(ref self, value: T)§

    Source

    Store value; copies of the cell share it, so they see it too.

  • pub const fn replace(ref self, value: T) -> T§

    Source

    Store value and return the old one.

  • pub const fn take(ref self) -> T§

    Source

    The value, leaving T::default() in its place.

Trait implementations 3
  • impl<T> Debug for Cell<T>
  • impl<T> !Send for Cell<T>
  • impl<T> !Sync for Cell<T>

Enums

LoadOrderingSource

pub enum LoadOrdering

Memory ordering of an atomic load, and of the load a failed compare-exchange does. A load cannot release.

StoreOrderingSource

pub enum StoreOrdering

Memory ordering of an atomic store. A store cannot acquire.

UpdateOrderingSource

pub enum UpdateOrdering

Memory ordering of a read-modify-write: swap, the fetch_* ops and a compare-exchange that succeeds.

Traits

AtomicPrimitiveSource

pub trait AtomicPrimitive

The element types Atomic<T> supports.