From fd36326ee720972396891c3c7d26d43ff7f11d8d Mon Sep 17 00:00:00 2001 From: Zhu Zihao Date: Sun, 4 Jul 2021 23:26:34 +0800 Subject: [PATCH] Simplify the implmentation of SpinLock. Use 'store' instead of CAS to unlock spinlock. --- src/spinlock.rs | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/src/spinlock.rs b/src/spinlock.rs index c760a429..6440ba01 100644 --- a/src/spinlock.rs +++ b/src/spinlock.rs @@ -69,12 +69,9 @@ impl<'mutex, T: ?Sized> DerefMut for SpinLockGuard<'mutex, T> { impl<'a, T: ?Sized> Drop for SpinLockGuard<'a, T> { #[inline] fn drop(&mut self) { - while self - .__lock - .locked - .compare_exchange(true, false, Ordering::SeqCst, Ordering::SeqCst) - .is_err() - {} + // Safety: While a SpinLock is locked, othre thread cannot acquire the + // lock, its internal status cannot be changed, no CAS needed. + self.__lock.locked.store(false, Ordering::SeqCst); } }