Skip to content

Commit

Permalink
tokio: add load and compare_exchange_weak to loom StaticAtomicU64 (#5356
Browse files Browse the repository at this point in the history
)
  • Loading branch information
dtolnay authored Jan 6, 2023
1 parent dfe252d commit 8d8db27
Showing 1 changed file with 21 additions and 0 deletions.
21 changes: 21 additions & 0 deletions tokio/src/loom/std/atomic_u64_static_once_cell.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,34 @@ impl StaticAtomicU64 {
}
}

pub(crate) fn load(&self, order: Ordering) -> u64 {
*self.inner().lock()
}

pub(crate) fn fetch_add(&self, val: u64, order: Ordering) -> u64 {
let mut lock = self.inner().lock();
let prev = *lock;
*lock = prev + val;
prev
}

pub(crate) fn compare_exchange_weak(
&self,
current: u64,
new: u64,
_success: Ordering,
_failure: Ordering,
) -> Result<u64, u64> {
let mut lock = self.inner().lock();

if *lock == current {
*lock = new;
Ok(current)
} else {
Err(*lock)
}
}

fn inner(&self) -> &Mutex<u64> {
self.cell.get(|| Mutex::new(self.init))
}
Expand Down

0 comments on commit 8d8db27

Please sign in to comment.