MCPcopy Create free account
hub / github.com/NodeDB-Lab/nodedb / try_reserve

Method try_reserve

nodedb-mem/src/governor.rs:72–88  ·  view source on GitHub ↗

Attempt a CAS-based reservation. Returns the `Arc` to the counter on success so the token can hold a reference for drop-release.

(&self, size: usize)

Source from the content-addressed store, hash-verified

70 /// Attempt a CAS-based reservation. Returns the `Arc` to the counter on
71 /// success so the token can hold a reference for drop-release.
72 fn try_reserve(&self, size: usize) -> Option<Arc<AtomicUsize>> {
73 loop {
74 let current = self.allocated.load(Ordering::Relaxed);
75 if current + size > self.limit {
76 return None;
77 }
78 match self.allocated.compare_exchange_weak(
79 current,
80 current + size,
81 Ordering::AcqRel,
82 Ordering::Relaxed,
83 ) {
84 Ok(_) => return Some(Arc::clone(&self.allocated)),
85 Err(_) => continue,
86 }
87 }
88 }
89
90 fn available(&self) -> usize {
91 let alloc = self.allocated.load(Ordering::Relaxed);

Calls 6

try_reserve_arcMethod · 0.80
loadMethod · 0.45
readMethod · 0.45
getMethod · 0.45
availableMethod · 0.45
limitMethod · 0.45