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>
impl<const N: usize> StripedLock<N>
Sourcepub fn lock(&self, key: &str) -> MutexGuard<'_, ()>
pub fn lock(&self, key: &str) -> MutexGuard<'_, ()>
Acquire the stripe for the given key.
Sourcepub fn lock_two(
&self,
a: &str,
b: &str,
) -> (MutexGuard<'_, ()>, Option<MutexGuard<'_, ()>>)
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.