JFLib

plat/sync.rs#

Source: lib/plat/sync.rs

sync -- blocking primitives for worker threads (servers, background jobs)

  Mutex<T> (lock / try_lock -> MutexGuard)     Condvar (wait / wait_timeout / notify_*)
  RwLock<T> (read / write; writers first)      channel(cap) -> (Sender<T>, Receiver<T>)

One primitive per OS: wait on a 32-bit word while it holds a value, wake one / all waiters
(Windows WaitOnAddress, Linux and Android private futexes).  Mutex and Condvar are the futex algorithms
of Rust's std; RwLock and the channel are built on them.  A lock is not poisoned by a panic
(panics abort the process here; with jflib's feature unwind a panic the app catches releases
the locks its guards held).
Not here: rt::SpinLock (rt keeps spin locks for the allocator and for modules, where plat is
replaced).
  • struct Mutex<T: ?Sized> a mutual-exclusion lock around a value

impl<T> Mutex<T>

  • const fn new(v: T) -> Mutex<T>
  • fn into_inner(self) -> T

impl<T: ?Sized> Mutex<T>

  • fn lock(&self) -> MutexGuard<'_, T>

  • fn try_lock(&self) -> Option<MutexGuard<'_, T>>

  • fn get_mut(&mut self) -> &mut T the value without locking (the borrow proves exclusivity)

  • struct MutexGuard<'a, T: ?Sized> the locked value; unlocks when dropped

  • struct Condvar a condition variable for Mutex

impl Condvar

  • const fn new() -> Condvar

  • fn wait<'a, T: ?Sized>(&self, g: MutexGuard<'a, T>) -> MutexGuard<'a, T> unlock, sleep until notified (or spuriously), lock again

  • fn wait_timeout<'a, T: ?Sized>(&self, g: MutexGuard<'a, T>, ms: Option<u32>) -> (MutexGuard<'a, T>, bool) as wait, at most ms milliseconds (None: forever) -> (guard, timed out)

  • fn wait_while<'a, T: ?Sized>(&self, mut g: MutexGuard<'a, T>, mut cond: impl FnMut(&mut T) -> bool) -> MutexGuard<'a, T> wait while cond holds (re-checked after every wake-up)

  • fn notify_one(&self)

  • fn notify_all(&self)

  • struct RwLock<T: ?Sized> many readers or one writer; a waiting writer stops new readers

impl<T> RwLock<T>

  • const fn new(v: T) -> RwLock<T>

impl<T: ?Sized> RwLock<T>

  • fn read(&self) -> RwReadGuard<'_, T>

  • fn write(&self) -> RwWriteGuard<'_, T>

  • struct RwReadGuard<'a, T: ?Sized>

  • struct RwWriteGuard<'a, T: ?Sized>

  • fn channel<T>(cap: usize) -> (Sender<T>, Receiver<T>) a bounded multi-producer, single-consumer queue (cap >= 1)

  • struct Sender<T>

  • struct Receiver<T>

  • enum RecvError why recv_timeout returned nothing

    • Timeout
    • Disconnected

impl<T> Sender<T>

  • fn send(&self, v: T) -> Result<(), T> queue a value, waiting while the queue is full -> Err(v) when the receiver is gone
  • fn try_send(&self, v: T) -> Result<(), T> queue a value if there is room -> Err(v) when full or the receiver is gone

impl<T> Receiver<T>

  • fn recv(&self) -> Option<T> the next value, waiting for one -> None when the queue is empty and every sender is gone
  • fn recv_timeout(&self, ms: u32) -> Result<T, RecvError> the next value, waiting at most ms milliseconds
  • fn try_recv(&self) -> Option<T> a value if one is queued
  • fn len(&self) -> usize values queued right now
  • fn is_empty(&self) -> bool

Source: docs/API.md in the JFLib repository.