FabricMutex

Struct FabricMutex 

Source
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 happened

Implementations§

Source§

impl<T: Copy> FabricMutex<T>

Source

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.

Source

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.

Source

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.

Source

pub fn generation(&self) -> Result<u64>

Read the current generation counter.

The generation is incremented by one each time the lock is successfully acquired (via lock or try_lock). Starts at 0 when the mutex is created.

Source

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.

Source

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]).

Source

pub fn expires_at_unix_secs(&self) -> u64

Returns the expiry time (unix seconds) of the underlying memory lease for external renewal management.

Auto Trait Implementations§

§

impl<T> Freeze for FabricMutex<T>

§

impl<T> !RefUnwindSafe for FabricMutex<T>

§

impl<T> !Send for FabricMutex<T>

§

impl<T> !Sync for FabricMutex<T>

§

impl<T> Unpin for FabricMutex<T>
where T: Unpin,

§

impl<T> !UnwindSafe for FabricMutex<T>

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, 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.