diff --git a/cros_async/src/sync/spin.rs b/cros_async/src/sync/spin.rs index 4fb6172699..8b7b811939 100644 --- a/cros_async/src/sync/spin.rs +++ b/cros_async/src/sync/spin.rs @@ -54,11 +54,16 @@ impl SpinLock { /// `SpinLockGuard` is dropped. Attempting to call `lock` while already holding the `SpinLock` /// will cause a deadlock. pub fn lock(&self) -> SpinLockGuard { - while self - .lock - .compare_exchange_weak(UNLOCKED, LOCKED, Ordering::Acquire, Ordering::Relaxed) - .is_err() - { + loop { + let state = self.lock.load(Ordering::Relaxed); + if state == UNLOCKED + && self + .lock + .compare_exchange_weak(UNLOCKED, LOCKED, Ordering::Acquire, Ordering::Relaxed) + .is_ok() + { + break; + } spin_loop_hint(); }