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)
| 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); |