pub struct FabricOnce { /* private fields */ }Expand description
One-time initialization primitive backed by a fabric memory lease.
Ensures a value is computed and stored exactly once, even if multiple callers race to initialize it. The first writer wins; subsequent callers read the existing value.
§Example
use grafos_sync::FabricOnce;
use grafos_std::mem::MemBuilder;
let lease = MemBuilder::new().acquire().unwrap();
let once = FabricOnce::new(lease, 0).unwrap();
let val = once.call_once(|| b"computed".to_vec()).unwrap();
assert_eq!(&val, b"computed");
// Second call returns the stored value without recomputing
let val2 = once.call_once(|| b"ignored".to_vec()).unwrap();
assert_eq!(&val2, b"computed");Implementations§
Source§impl FabricOnce
impl FabricOnce
Sourcepub fn new(lease: MemLease, base_offset: u64) -> Result<Self>
pub fn new(lease: MemLease, base_offset: u64) -> Result<Self>
Create a new FabricOnce at base_offset in the given lease.
Initializes the initialized flag to 0 (not yet initialized).
Sourcepub fn call_once<F>(&self, f: F) -> Result<Vec<u8>>
pub fn call_once<F>(&self, f: F) -> Result<Vec<u8>>
Initialize the value by calling f, or return the existing value
if already initialized.
If the initialized flag is already set, f is not called and
the stored value is returned. Otherwise, f is called, the result
is written to leased memory (data first, then length, then flag),
and the value is returned.
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.
Sourcepub fn is_initialized(&self) -> Result<bool>
pub fn is_initialized(&self) -> Result<bool>
Check if the value has been initialized.
Returns true if call_once has completed at least
once (the initialized flag is set in leased memory).