Skip to main content

AsyncLock

Trait AsyncLock 

Source
pub trait AsyncLock: Send + Sync {
    // Required methods
    fn lock(&self) -> impl Future<Output = Result<(), LockError>> + Send + '_;
    fn try_lock(
        &self,
    ) -> impl Future<Output = Result<bool, LockError>> + Send + '_;
    fn unlock(&self) -> impl Future<Output = Result<(), LockError>> + Send + '_;
}
Expand description

A single lock instance whose operations yield to the executor instead of blocking the OS thread.

Every operation is asynchronous because a lock may be backed by I/O: the in-memory lock resolves immediately, but a durable lock (e.g. the SQLx lease-table PostgresLock / SqliteLock) acquires and releases by talking to the database. lock awaits until the lock becomes free; try_lock attempts a single non-blocking acquire; unlock releases the lock and (for in-memory) wakes any waiters so they can re-contend.

All returned futures are Send so an async QueuedRepository built on this lock keeps its repository futures Send (required by the async repo traits).

Required Methods§

Source

fn lock(&self) -> impl Future<Output = Result<(), LockError>> + Send + '_

Acquire the lock, awaiting until it becomes available.

Source

fn try_lock(&self) -> impl Future<Output = Result<bool, LockError>> + Send + '_

Try to acquire the lock without waiting.

Returns Ok(true) if acquired, Ok(false) if already held.

Source

fn unlock(&self) -> impl Future<Output = Result<(), LockError>> + Send + '_

Release the lock, waking any waiters so they can re-contend.

Idempotent: releasing a lock this caller no longer holds (e.g. an expired distributed lease that was stolen) is a no-op success.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§