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) -> TPanics on
Release/AcqRel(not load orderings).pub fn store(self, value: T, ordering: AtomicOrdering = SeqCst)pub fn swap(self, value: T, ordering: AtomicOrdering = SeqCst) -> Tpub fn compare_exchange( self, current: T, new: T, success: AtomicOrdering, failure: AtomicOrdering, ) -> Result<TOk(previous)if it heldcurrent(nownew), elseErr(actual).failureis a load ordering (panics onRelease/AcqRel).pub fn compare_exchange_weak( self, current: T, new: T, success: AtomicOrdering, failure: AtomicOrdering, ) -> Result<TNever fails spuriously here (same as
compare_exchange).pub fn fetch_add(self, value: T, ordering: AtomicOrdering = SeqCst) -> Tpub fn fetch_sub(self, value: T, ordering: AtomicOrdering = SeqCst) -> Tpub fn fetch_and(self, value: T, ordering: AtomicOrdering = SeqCst) -> Tpub fn fetch_or(self, value: T, ordering: AtomicOrdering = SeqCst) -> Tpub fn fetch_xor(self, value: T, ordering: AtomicOrdering = SeqCst) -> Tpub fn wait(self, expected: i32, timeout_ns: i64 = -1)Block while the value is
expected, at mosttimeoutns (< 0: no limit), or untilwake; may return spuriously, so re-check.pub fn wake(self)Wake every thread in
waiton this cell.
Mutex
pub struct Mutex<T>pub const fn new(value: T) -> Mutex<T>pub fn lock(self) -> MutexGuard<T>Block until the lock is ours. Bind the guard with
using.pub fn try_lock(self) -> MutexGuard<T> | nonepub 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) -> TThe 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 norefreturns).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> | nonepub fn try_write(self) -> RwLockWriteGuard<T> | nonepub 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) -> Tpub 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) -> TA copy of the value: the reader's own to change.
Trait implementations 1
unsafe impl<T: Send> Sync for Frozen<T>
Once
pub struct Oncepub const fn new() -> Oncepub fn call_once(self, f: fn())Run
fif no call has; every caller returns after it finished.pub fn is_completed(self) -> bool
Condvar
pub struct Condvarpub const fn new() -> Condvarpub 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).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_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.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: Copypub const fn set(ref self, value: T)pub const fn replace(ref self, value: T) -> Tpub const fn take(ref self) -> TThe 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 AtomicOrderingMemory ordering of an atomic operation (2.16.2). Spec name: Ordering,
which would collide with cmp::Ordering under use rok::core::**.
RelaxedAcquireReleaseAcqRelSeqCst
Traits
AtomicPrimitive
pub trait AtomicPrimitiveThe element types Atomic<T> supports.