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 = LockResult<Self::ReadHandle>> + Send;
fn try_acquire_read(
&self,
) -> impl Future<Output = LockResult<Option<Self::ReadHandle>>> + Send;
fn acquire_write(
&self,
timeout: Option<Duration>,
) -> impl Future<Output = LockResult<Self::WriteHandle>> + Send;
fn try_acquire_write(
&self,
) -> impl Future<Output = LockResult<Option<Self::WriteHandle>>> + 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§
Sourcetype ReadHandle: LockHandle + Send
type ReadHandle: LockHandle + Send
Handle type for read (shared) locks.
Sourcetype WriteHandle: LockHandle + Send
type WriteHandle: LockHandle + Send
Handle type for write (exclusive) locks.
Required Methods§
Sourcefn acquire_read(
&self,
timeout: Option<Duration>,
) -> impl Future<Output = LockResult<Self::ReadHandle>> + Send
fn acquire_read( &self, timeout: Option<Duration>, ) -> impl Future<Output = LockResult<Self::ReadHandle>> + Send
Acquires a read (shared) lock.
Multiple readers can hold the lock concurrently. Blocks if a writer holds or is waiting for the lock.
Sourcefn try_acquire_read(
&self,
) -> impl Future<Output = LockResult<Option<Self::ReadHandle>>> + Send
fn try_acquire_read( &self, ) -> impl Future<Output = LockResult<Option<Self::ReadHandle>>> + Send
Attempts to acquire a read lock without waiting.
Sourcefn acquire_write(
&self,
timeout: Option<Duration>,
) -> impl Future<Output = LockResult<Self::WriteHandle>> + Send
fn acquire_write( &self, timeout: Option<Duration>, ) -> impl Future<Output = LockResult<Self::WriteHandle>> + Send
Acquires a write (exclusive) lock.
Only one writer can hold the lock. Blocks all readers.
Sourcefn try_acquire_write(
&self,
) -> impl Future<Output = LockResult<Option<Self::WriteHandle>>> + Send
fn try_acquire_write( &self, ) -> impl Future<Output = LockResult<Option<Self::WriteHandle>>> + 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", so this trait is not object safe.