From 875dc41cd5791dbea3fccfbdb9ee926b47aa4f2b Mon Sep 17 00:00:00 2001 From: Taiki Endo Date: Thu, 24 Dec 2020 21:22:11 +0900 Subject: [PATCH] Replace deprecated compare_and_swap with compare_exchange --- src/mutex.rs | 30 +++++++++++++++++++++++++----- src/rwlock.rs | 10 +++++++--- 2 files changed, 32 insertions(+), 8 deletions(-) diff --git a/src/mutex.rs b/src/mutex.rs index 8d079e1..d86cb6d 100644 --- a/src/mutex.rs +++ b/src/mutex.rs @@ -117,7 +117,11 @@ impl Mutex { let listener = self.lock_ops.listen(); // Try locking if nobody is being starved. - match self.state.compare_and_swap(0, 1, Ordering::Acquire) { + match self + .state + .compare_exchange(0, 1, Ordering::Acquire, Ordering::Acquire) + .unwrap_or_else(|x| x) + { // Lock acquired! 0 => return, @@ -132,7 +136,11 @@ impl Mutex { listener.await; // Try locking if nobody is being starved. - match self.state.compare_and_swap(0, 1, Ordering::Acquire) { + match self + .state + .compare_exchange(0, 1, Ordering::Acquire, Ordering::Acquire) + .unwrap_or_else(|x| x) + { // Lock acquired! 0 => return, @@ -171,7 +179,11 @@ impl Mutex { let listener = self.lock_ops.listen(); // Try locking if nobody else is being starved. - match self.state.compare_and_swap(2, 2 | 1, Ordering::Acquire) { + match self + .state + .compare_exchange(2, 2 | 1, Ordering::Acquire, Ordering::Acquire) + .unwrap_or_else(|x| x) + { // Lock acquired! 2 => return, @@ -213,7 +225,11 @@ impl Mutex { /// ``` #[inline] pub fn try_lock(&self) -> Option> { - if self.state.compare_and_swap(0, 1, Ordering::Acquire) == 0 { + if self + .state + .compare_exchange(0, 1, Ordering::Acquire, Ordering::Acquire) + .is_ok() + { Some(MutexGuard(self)) } else { None @@ -286,7 +302,11 @@ impl Mutex { /// ``` #[inline] pub fn try_lock_arc(self: &Arc) -> Option> { - if self.state.compare_and_swap(0, 1, Ordering::Acquire) == 0 { + if self + .state + .compare_exchange(0, 1, Ordering::Acquire, Ordering::Acquire) + .is_ok() + { Some(MutexGuardArc(self.clone())) } else { None diff --git a/src/rwlock.rs b/src/rwlock.rs index 4354f28..f3a9c79 100644 --- a/src/rwlock.rs +++ b/src/rwlock.rs @@ -342,7 +342,11 @@ impl RwLock { let lock = self.mutex.try_lock()?; // If there are no readers, grab the write lock. - if self.state.compare_and_swap(0, WRITER_BIT, Ordering::AcqRel) == 0 { + if self + .state + .compare_exchange(0, WRITER_BIT, Ordering::AcqRel, Ordering::Acquire) + .is_ok() + { Some(RwLockWriteGuard { writer: RwLockWriteGuardInner(self), reserved: lock, @@ -555,8 +559,8 @@ impl<'a, T: ?Sized> RwLockUpgradableReadGuard<'a, T> { .reader .0 .state - .compare_and_swap(ONE_READER, WRITER_BIT, Ordering::AcqRel) - == ONE_READER + .compare_exchange(ONE_READER, WRITER_BIT, Ordering::AcqRel, Ordering::Acquire) + .is_ok() { Ok(guard.into_writer()) } else {