Skip to main content

Module sync

Module sync 

Source
Expand description

Synchronization primitives with two-phase semantics.

This module provides cancel-safe synchronization primitives where guards and permits are tracked as obligations that must be released.

§Primitives

  • Mutex: Mutual exclusion with guard obligations
  • RwLock: Read-write lock with cancel-aware acquisition
  • Semaphore: Counting semaphore with permit obligations
  • Pool: Resource pooling with obligation-based return semantics
  • Barrier: N-way rendezvous with leader election
  • Notify: Event signaling (one-shot or broadcast)
  • OnceCell: Lazy initialization cell

§Two-Phase Pattern

All primitives in this module follow a two-phase pattern:

  • Phase 1 (Wait): Wait for the resource to become available. This phase is cancel-safe - cancellation during wait is clean.
  • Phase 2 (Hold): Hold the resource (guard/permit). The guard is an obligation that must be released (via drop).

§Cancel Safety

  • Cancellation during wait: Clean abort, no resource held
  • Cancellation while holding: Guard dropped, resource released
  • Panic while holding: Guard dropped via unwind (unwind safety)

Re-exports§

pub use semaphore::AcquireError;
pub use semaphore::OwnedSemaphorePermit;
pub use semaphore::Semaphore;
pub use semaphore::SemaphorePermit;
pub use semaphore::TryAcquireError;

Modules§

semaphore
Two-phase semaphore with permit obligations.

Structs§

Barrier
Barrier for N-way rendezvous.
BarrierWaitResult
Result of a barrier wait.
ContendedMutex
Zero-cost mutex wrapper (metrics disabled).
ContendedMutexGuard
Zero-cost guard wrapper (metrics disabled).
GenericPool
A generic resource pool with configurable behavior.
LockMetricsSnapshot
Snapshot of lock contention metrics.
Mutex
An async mutex for mutual exclusion.
MutexGuard
A guard that releases the mutex when dropped.
Notified
Future returned by Notify::notified.
Notify
A notify primitive for signaling events.
OnceCell
A cell that can be initialized exactly once.
OwnedMutexGuard
An owned guard that releases the mutex when dropped.
OwnedRwLockReadGuard
Owned read guard that can be moved between tasks.
OwnedRwLockWriteGuard
Owned write guard that can be moved between tasks.
PoolConfig
Configuration for a generic resource pool.
PoolStats
Pool usage statistics.
PooledResource
A resource acquired from a pool.
RwLock
A cancel-aware read-write lock with writer-preference fairness.
RwLockReadGuard
Guard for a read lock.
RwLockWriteGuard
Guard for a write lock.

Enums§

BarrierWaitError
Error returned when waiting on a barrier fails.
DestroyReason
Reason why a resource was destroyed.
LockError
Error returned when mutex locking fails.
OnceCellError
Error returned when a OnceCell operation fails.
PoolError
Error type for GenericPool operations.
PoolReturn
Return messages sent from PooledResource back to a pool implementation.
RwLockError
Error returned when acquiring a read or write lock fails.
TryLockError
Error returned when trying to lock without waiting.
TryReadError
Error returned when trying to read without waiting.
TryWriteError
Error returned when trying to write without waiting.
WarmupStrategy
Strategy for handling partial warmup failures.

Traits§

AsyncResourceFactory
Trait for async resource creation and destruction.
Pool
Trait for resource pools with cancel-safe acquisition.

Type Aliases§

PoolFuture
Boxed future helper for async trait-like APIs.
PoolReturnReceiver
Receiver used to observe resources returning to a pool.
PoolReturnSender
Sender used to return resources back to a pool.