Skip to main content

StripedLock

Struct StripedLock 

Source
pub struct StripedLock<const N: usize> { /* private fields */ }
Expand description

A fixed-size array of Mutex stripes for fine-grained write serialization.

Hash a key to select a stripe. Two keys that hash to the same stripe serialize against each other (correctness preserved, slight throughput reduction). 64 stripes is sufficient for typical workloads.

Implementations§

Source§

impl<const N: usize> StripedLock<N>

Source

pub fn new() -> Self

Create a new striped lock with N stripes.

Source

pub fn lock(&self, key: &str) -> MutexGuard<'_, ()>

Acquire the stripe for the given key.

Source

pub fn lock_two( &self, a: &str, b: &str, ) -> (MutexGuard<'_, ()>, Option<MutexGuard<'_, ()>>)

Acquire the stripes for two keys at once, deadlock-free.

Stripes are always taken in ascending index order, so two threads that each need the same pair of stripes can never form a hold-and-wait cycle. When both keys map to the same stripe, a single guard is returned and the second is None — re-locking the same mutex would deadlock (it is not reentrant), and one guard already serializes both keys. The returned guards are in stripe-index order, not argument order; callers should treat them as an opaque “hold both” token.

Trait Implementations§

Source§

impl<const N: usize> Default for StripedLock<N>

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl<const N: usize> Sync for StripedLock<N>

Auto Trait Implementations§

§

impl<const N: usize> !Freeze for StripedLock<N>

§

impl<const N: usize> RefUnwindSafe for StripedLock<N>
where [Mutex<()>; N]: RefUnwindSafe,

§

impl<const N: usize> Send for StripedLock<N>
where [Mutex<()>; N]: Send,

§

impl<const N: usize> Unpin for StripedLock<N>
where [Mutex<()>; N]: Unpin,

§

impl<const N: usize> UnsafeUnpin for StripedLock<N>
where [Mutex<()>; N]: UnsafeUnpin,

§

impl<const N: usize> UnwindSafe for StripedLock<N>
where [Mutex<()>; N]: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.