Skip to content

Upgrade lock_api to 0.4.0 #3

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from Jul 6, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,4 @@ repository = "https://github.com/rust-osdev/spinning_top"
nightly = ["lock_api/nightly"]

[dependencies]
lock_api = "0.3.3"
lock_api = "0.4.0"
14 changes: 8 additions & 6 deletions src/spinlock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use lock_api::{GuardSend, RawMutex};
/// let lock = spinning_top::RawSpinlock::INIT;
/// assert_eq!(lock.try_lock(), true); // lock it
/// assert_eq!(lock.try_lock(), false); // can't be locked a second time
/// lock.unlock(); // unlock it
/// unsafe { lock.unlock(); } // unlock it
/// assert_eq!(lock.try_lock(), true); // now it can be locked again
#[derive(Debug)]
pub struct RawSpinlock {
Expand All @@ -38,10 +38,7 @@ unsafe impl RawMutex for RawSpinlock {
while !self.try_lock() {
// Wait until the lock looks unlocked before retrying
// Code from https://github.com/mvdnes/spin-rs/commit/d3e60d19adbde8c8e9d3199c7c51e51ee5a20bf6
//
// Relaxed ordering is fine here because the actual synchronization happens in
// the outer `while` loop.
while self.locked.load(Ordering::Relaxed) {
while self.is_locked() {
// Tell the CPU that we're inside a busy-wait loop
spin_loop_hint();
}
Expand All @@ -63,9 +60,14 @@ unsafe impl RawMutex for RawSpinlock {
.is_ok()
}

fn unlock(&self) {
unsafe fn unlock(&self) {
self.locked.store(false, Ordering::Release);
}

fn is_locked(&self) -> bool {
// Relaxed is sufficient because this operation does not provide synchronization, only atomicity.
self.locked.load(Ordering::Relaxed)
}
}

/// A mutual exclusion (Mutex) type based on busy-waiting.
Expand Down