rokdocs
v0.1

rok::core::sync

rok::core::sync - concurrency primitives (stdlib spec 2.16).

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 (rok_sync_wait / rok_sync_wake, futex semantics; Atomic<i32>::wait / wake expose it); a blocked thread counts as stopped for the GC.

Not in 2.16 but provided because the blocking APIs need them: Once, Condvar (Rust-shaped).

Modules

    Structs

    Atomic

    pub struct Atomic<T>

    A lock-free cell (2.16.2). Send + Sync.

    • pub const fn new(value: T) -> Atomic<T>
    • pub fn load(self, ordering: AtomicOrdering = SeqCst) -> T

      Panics on Release / AcqRel (not load orderings).

    • pub fn store(self, value: T, ordering: AtomicOrdering = SeqCst)
    • pub fn swap(self, value: T, ordering: AtomicOrdering = SeqCst) -> T
    • pub fn compare_exchange(
          self, current: T, new: T, success: AtomicOrdering, failure: AtomicOrdering,
      ) -> Result<T

      Ok(previous) if it held current (now new), else Err(actual). failure is a load ordering (panics on Release / AcqRel).

    • pub fn compare_exchange_weak(
          self, current: T, new: T, success: AtomicOrdering, failure: AtomicOrdering,
      ) -> Result<T

      Never fails spuriously here (same as compare_exchange).

    • pub fn fetch_add(self, value: T, ordering: AtomicOrdering = SeqCst) -> T
    • pub fn fetch_sub(self, value: T, ordering: AtomicOrdering = SeqCst) -> T
    • pub fn fetch_and(self, value: T, ordering: AtomicOrdering = SeqCst) -> T
    • pub fn fetch_or(self, value: T, ordering: AtomicOrdering = SeqCst) -> T
    • pub fn fetch_xor(self, value: T, ordering: AtomicOrdering = SeqCst) -> T
    • pub fn wait(self, expected: i32, timeout_ns: i64 = -1)

      Block while the value is expected, at most timeout ns (< 0: no limit), or until wake; may return spuriously, so re-check.

    • pub fn wake(self)

      Wake every thread in wait on this cell.

    Mutex

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

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

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

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

    • pub fn try_lock(self) -> MutexGuard<T> | none
    • pub 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.

    Trait implementations 1
    • unsafe impl<T: Send> Sync for Mutex<T>

    LockFuture

    pub struct LockFuture<T>

    Mutex::lock_async.

    Trait implementations 1
    • impl<T> rok::core::future::Future for LockFuture<T>

    MutexGuard

    pub struct MutexGuard<T>

    Holds a Mutex locked until dropped (using).

    • pub const fn get(self) -> T

      The protected value (a copy of the handle for GC types).

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

      Replace the protected value (instead of spec deref_mut: rok has no ref returns).

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

      Replace the protected value with f(value).

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

    RwLock

    pub struct RwLock<T>
    • pub const fn new(value: T) -> RwLock<T>
    • pub fn read(self) -> RwLockReadGuard<T>
    • pub fn write(self) -> RwLockWriteGuard<T>
    • pub fn try_read(self) -> RwLockReadGuard<T> | none
    • pub fn try_write(self) -> RwLockWriteGuard<T> | none
    • pub const fn read_async(self) -> ReadFuture<T>

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

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

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

    Trait implementations 1
    • unsafe impl<T: Send + Sync> Sync for RwLock<T>

    ReadFuture

    pub struct ReadFuture<T>

    RwLock::read_async.

    Trait implementations 1
    • impl<T> rok::core::future::Future for ReadFuture<T>

    WriteFuture

    pub struct WriteFuture<T>

    RwLock::write_async.

    Trait implementations 1
    • impl<T> rok::core::future::Future for WriteFuture<T>

    RwLockReadGuard

    pub struct RwLockReadGuard<T>
    • pub const fn get(self) -> T
    Trait implementations 2
    • impl<T> !Sync for RwLockReadGuard<T>
    • impl<T> Drop for RwLockReadGuard<T>

    RwLockWriteGuard

    pub struct RwLockWriteGuard<T>
    • pub const fn get(self) -> T
    • pub const fn set(self, value: T)
    • pub fn update(self, f: fn(T) -> T)
    Trait implementations 2
    • impl<T> !Sync for RwLockWriteGuard<T>
    • impl<T> Drop for RwLockWriteGuard<T>

    Frozen

    pub struct Frozen<T>

    A value made once and only read after (2.16.4b): 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>
    • pub fn get(self) -> T

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

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

    Once

    pub struct Once
    • pub const fn new() -> Once
    • pub fn call_once(self, f: fn())

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

    • pub fn is_completed(self) -> bool

    Condvar

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

      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>

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

    • pub fn notify_one(self)

      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)

    CondvarWait

    pub struct CondvarWait<T>

    Condvar::wait_async.

    Trait implementations 1
    • impl<T> rok::core::future::Future for CondvarWait<T>

    Cell

    pub struct Cell<T>

    Single-task interior mutability (2.16.5): neither Send nor Sync.

    • pub const fn new(value: T) -> Cell<T>
    • pub const fn get(self) -> T where T: Copy
    • pub const fn set(ref self, value: T)
    • pub const fn replace(ref self, value: T) -> T
    • pub const fn take(ref self) -> T

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

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

    Enums

    AtomicOrdering

    pub enum AtomicOrdering

    Memory ordering of an atomic operation (2.16.2). Spec name: Ordering, which would collide with cmp::Ordering under use rok::core::**.

    • Relaxed
    • Acquire
    • Release
    • AcqRel
    • SeqCst

    Traits

    AtomicPrimitive

    pub trait AtomicPrimitive

    The element types Atomic<T> supports.