pub struct FabricMutex<T> { /* private fields */ }Expand description
Distributed mutex backed by a fabric memory lease.
The lock state is stored in leased fabric memory. When a holder crashes without releasing the lock, the lease expires and the lock auto-releases.
T must be Copy — the value is stored via raw byte copy in the leased
arena. For complex types, use the serde feature on grafos-std.
§Example
use grafos_sync::FabricMutex;
use grafos_std::mem::MemBuilder;
let lease = MemBuilder::new().acquire().unwrap();
let mtx = FabricMutex::new(lease, 0, 42u64).unwrap();
// Acquire with holder_id=1, up to 100 spin attempts
let guard = mtx.lock(1, 100).unwrap();
assert_eq!(*guard, 42);
guard.unlock().unwrap();
assert_eq!(mtx.holder().unwrap(), 0); // unlocked
assert_eq!(mtx.generation().unwrap(), 1); // one acquire happenedImplementations§
Source§impl<T: Copy> FabricMutex<T>
impl<T: Copy> FabricMutex<T>
Sourcepub fn new(lease: MemLease, base_offset: u64, initial: T) -> Result<Self>
pub fn new(lease: MemLease, base_offset: u64, initial: T) -> Result<Self>
Create a new mutex with the given initial value, stored at base_offset
in the provided lease’s memory arena.
Initializes holder_id to 0 (unlocked) and generation to 0. The caller
is responsible for ensuring that distinct mutexes use non-overlapping
offset ranges. The total space consumed is 28 + size_of::<T>() bytes.
Sourcepub fn lock(
&self,
holder_id: u128,
max_attempts: u32,
) -> Result<FabricMutexGuard<'_, T>>
pub fn lock( &self, holder_id: u128, max_attempts: u32, ) -> Result<FabricMutexGuard<'_, T>>
Acquire the lock, spinning up to max_attempts times.
On each iteration, reads the holder_id from leased memory. If it is
zero (unlocked), writes holder_id, increments the generation counter,
and returns an RAII FabricMutexGuard with Deref/DerefMut access
to the protected value.
Returns FabricError::LeaseExpired if max_attempts is exhausted
without acquiring the lock.
Sourcepub fn try_lock(
&self,
holder_id: u128,
) -> Result<Option<FabricMutexGuard<'_, T>>>
pub fn try_lock( &self, holder_id: u128, ) -> Result<Option<FabricMutexGuard<'_, T>>>
Non-blocking lock attempt.
Reads the holder_id once. Returns Ok(Some(guard)) if the lock was
free and successfully acquired, Ok(None) if it was already held, or
Err on an underlying I/O failure.
Sourcepub fn generation(&self) -> Result<u64>
pub fn generation(&self) -> Result<u64>
Sourcepub fn holder(&self) -> Result<u128>
pub fn holder(&self) -> Result<u128>
Read the current holder_id (0 = unlocked).
Returns the u128 identifier of the party currently holding the lock,
or 0 if the lock is not held. This is a snapshot; the value may change
between the read and any action taken on it.
Sourcepub fn lease_id(&self) -> u128
pub fn lease_id(&self) -> u128
Returns the lease ID of the underlying memory lease for external
renewal management (e.g. via [grafos_leasekit::RenewalManager]).
Sourcepub fn expires_at_unix_secs(&self) -> u64
pub fn expires_at_unix_secs(&self) -> u64
Returns the expiry time (unix seconds) of the underlying memory lease for external renewal management.