Skip to main content

DistributedReaderWriterLock

Trait DistributedReaderWriterLock 

Source
pub trait DistributedReaderWriterLock: Send + Sync {
    type ReadHandle: LockHandle + Send;
    type WriteHandle: LockHandle + Send;

    // Required methods
    fn name(&self) -> &str;
    fn acquire_read(
        &self,
        timeout: Option<Duration>,
    ) -> impl Future<Output = Result<Self::ReadHandle, LockError>> + Send;
    fn try_acquire_read(
        &self,
    ) -> impl Future<Output = Result<Option<Self::ReadHandle>, LockError>> + Send;
    fn acquire_write(
        &self,
        timeout: Option<Duration>,
    ) -> impl Future<Output = Result<Self::WriteHandle, LockError>> + Send;
    fn try_acquire_write(
        &self,
    ) -> impl Future<Output = Result<Option<Self::WriteHandle>, LockError>> + Send;
}
Expand description

A distributed reader-writer lock.

Allows multiple concurrent readers OR a single exclusive writer. Writers are given priority to prevent starvation.

§Example

// Multiple readers can hold the lock simultaneously
let read_handle = lock.acquire_read(None).await?;
let data = read_shared_resource().await;
read_handle.release().await?;

// Writers get exclusive access
let write_handle = lock.acquire_write(None).await?;
modify_shared_resource().await;
write_handle.release().await?;

Required Associated Types§

Source

type ReadHandle: LockHandle + Send

Handle type for read (shared) locks.

Source

type WriteHandle: LockHandle + Send

Handle type for write (exclusive) locks.

Required Methods§

Source

fn name(&self) -> &str

Returns the unique name identifying this lock.

Source

fn acquire_read( &self, timeout: Option<Duration>, ) -> impl Future<Output = Result<Self::ReadHandle, LockError>> + Send

Acquires a read (shared) lock.

Multiple readers can hold the lock concurrently. Blocks if a writer holds or is waiting for the lock.

Source

fn try_acquire_read( &self, ) -> impl Future<Output = Result<Option<Self::ReadHandle>, LockError>> + Send

Attempts to acquire a read lock without waiting.

Source

fn acquire_write( &self, timeout: Option<Duration>, ) -> impl Future<Output = Result<Self::WriteHandle, LockError>> + Send

Acquires a write (exclusive) lock.

Only one writer can hold the lock. Blocks all readers.

Source

fn try_acquire_write( &self, ) -> impl Future<Output = Result<Option<Self::WriteHandle>, LockError>> + Send

Attempts to acquire a write lock without waiting.

Dyn Compatibility§

This trait is not dyn compatible.

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

Implementors§