pub struct SharedAtom {
pub cell: Arc<ArcSwap<SharedSlot>>,
pub meta: Mutex<Option<SharedValue>>,
}Expand description
A cross-isolate mutable reference backed by a lock-free ArcSwap.
reset! and swap! promote the new value and perform an atomic pointer
swap — no locking, O(1) for simple resets, O(retries) for CAS-retry
swap! under write contention. deref is a single atomic load plus an
Arc reference-count bump.
Wrapped in Value::SharedAtom(Arc<SharedAtom>). The Arc gives the atom
identity (pointer equality) and allows any number of isolates to hold a
reference.
Each stored value is a SharedSlot that carries its own governor
charge; a replaced slot returns its charge when the last reader drops it.
Fields§
§cell: Arc<ArcSwap<SharedSlot>>§meta: Mutex<Option<SharedValue>>Implementations§
pub fn new(val: SharedValue) -> Self
Sourcepub fn try_new(val: SharedValue) -> Result<Self, MemoryLimitExceeded>
pub fn try_new(val: SharedValue) -> Result<Self, MemoryLimitExceeded>
Fallible Self::new: refuse if the governor cannot admit the slot.
Sourcepub fn deref_val(&self) -> Arc<SharedSlot> ⓘ
pub fn deref_val(&self) -> Arc<SharedSlot> ⓘ
Load the current value. Lock-free atomic load + refcount bump.
Sourcepub fn reset(&self, val: SharedValue) -> Arc<SharedSlot> ⓘ
pub fn reset(&self, val: SharedValue) -> Arc<SharedSlot> ⓘ
Atomically replace the value and return the new Arc.
Sourcepub fn try_reset(
&self,
val: SharedValue,
) -> Result<Arc<SharedSlot>, MemoryLimitExceeded>
pub fn try_reset( &self, val: SharedValue, ) -> Result<Arc<SharedSlot>, MemoryLimitExceeded>
Fallible Self::reset: the slot is reserved before it is
published, so a refusal leaves the current value unchanged.
Sourcepub fn swap<F>(&self, f: F) -> Arc<SharedSlot> ⓘ
pub fn swap<F>(&self, f: F) -> Arc<SharedSlot> ⓘ
CAS-retry swap: apply f to the current value and store the result.
Returns the new value. Retries automatically if another writer races.
Sourcepub fn compare_and_set(
&self,
current: &Arc<SharedSlot>,
new: SharedValue,
) -> bool
pub fn compare_and_set( &self, current: &Arc<SharedSlot>, new: SharedValue, ) -> bool
Single lock-free compare-and-set. Atomically stores new iff the cell
still holds current (by Arc identity). Returns true on success.
This is the primitive used by Clojure-level compare-and-set! and by the
retry loop behind swap!: a caller that needs to run arbitrary
(interpreter) code between the load and the store cannot use the closure
form of [swap], so it loads via deref_val, computes
the next value, then commits with this method, retrying on contention.
Sourcepub fn try_compare_and_set(
&self,
current: &Arc<SharedSlot>,
new: SharedValue,
) -> Result<bool, MemoryLimitExceeded>
pub fn try_compare_and_set( &self, current: &Arc<SharedSlot>, new: SharedValue, ) -> Result<bool, MemoryLimitExceeded>
Fallible Self::compare_and_set: the new slot is reserved before
the swap, so a refusal leaves the current value unchanged.