3. Batteries Included
While the core philosophy of Resync is modularity, it doesn’t mean you have to build everything from scratch. Resync ships with an impressive, production-ready arsenal of synchronization primitives and backend policies out of the box.
High-Level Primitives
Mutex<T, L, R>: The standard mutual exclusion lock. Protects data and supports lock poisoning.Sharex<T, L, R>: A read-write lock (RwLock) allowing multiple concurrent readers or a single exclusive writer.Gate<L, R>: A controllable barrier. Starts closed (viaNewLocked) and blocks threads until explicitly opened. Perfect for thread pool initialization.Semaphore<L, R>: A counting semaphore for limiting concurrent access to a pool of resources (e.g., DB connections).Condvar: A condition variable for event-based waiting, fully respecting the poisoning semantics of the associatedMutex.Barrier<R>: A synchronization primitive that blocks a set of threads until all of them have reached a certain point.Once<T, L, R, P>: A primitive for one-time lazy initialization (similar tostd::sync::OnceLock). It uses a fast-path atomic check and falls back to theLockPolicyonly during the initialization phase. Fully respects lock poisoning if the initialization closure panics.
Lock Backends (LockPolicy & SharingPolicy)
Atomic: A pure, portable spinlock based onAtomicUsize. Ideal for#![no_std]and extremely short critical sections.Os: OS-specific high-performance locks. Usesfutexon Linux,pthread_rwlock_ton macOS, andSRWLOCKon Windows. Automatically parks threads in the kernel on contention.Fs: A filesystem-based advisory lock usingflock(2). Useful for cross-process synchronization.Irq: A bare-metal lock that disables hardware interrupts (IRQs) upon acquisition. Essential for kernel development to prevent interrupt-handler deadlocks.Nested<L1, L2>: A composite lock that strictly enforces acquisition order (L1thenL2) and reverse release order, preventing deadlocks at compile time.Shield<L>: A wrapper that prevents writer starvation in read-write locks by yielding readers (shared accessors yield) the resource from new readers when a writer is waiting.
Retry Backends (RetryPolicy)
Busy: Executes architecture-specific CPU pause instructions (core::hint::spin_loop()).Yield: Cooperatively yields the current thread to the OS scheduler (std::thread::yield_now()).