Skip to content

Commit

Permalink
feat: migrate to upstream spinning-top
Browse files Browse the repository at this point in the history
Signed-off-by: Martin Kröning <[email protected]>
  • Loading branch information
mkroening committed Apr 3, 2024
1 parent 548cc85 commit bca0539
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 715 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ generic_once_cell = "0.1"
interrupts = "0.1"
interrupt-mutex = "0.1"
lock_api = "0.4"
spinning_top = "0.3.0"

[dev-dependencies]
rand = "0.8"
35 changes: 0 additions & 35 deletions src/backoff.rs

This file was deleted.

19 changes: 17 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,9 +88,24 @@
#![cfg_attr(not(test), no_std)]
#![warn(unsafe_op_in_unsafe_fn)]

mod backoff;
pub(crate) mod mutex;
pub(crate) mod rwlock;
pub(crate) mod rwlock {
/// A simple spinning, read-preferring readers-writer lock with exponential backoff.
pub type RawRwSpinLock = spinning_top::RawRwSpinlock<spinning_top::relax::Backoff>;

/// A [`lock_api::RwLock`] based on [`RawRwSpinLock`].
pub type RwSpinLock<T> = lock_api::RwLock<RawRwSpinLock, T>;

/// A [`lock_api::RwLockReadGuard`] based on [`RawRwSpinLock`].
pub type RwSpinLockReadGuard<'a, T> = lock_api::RwLockReadGuard<'a, RawRwSpinLock, T>;

/// A [`lock_api::RwLockUpgradableReadGuard`] based on [`RawRwSpinLock`].
pub type RwSpinLockUpgradableReadGuard<'a, T> =
lock_api::RwLockUpgradableReadGuard<'a, RawRwSpinLock, T>;

/// A [`lock_api::RwLockWriteGuard`] based on [`RawRwSpinLock`].
pub type RwSpinLockWriteGuard<'a, T> = lock_api::RwLockWriteGuard<'a, RawRwSpinLock, T>;
}

pub use exclusive_cell::{CallOnce, CallOnceError, ExclusiveCell};
pub use interrupt_mutex::{InterruptMutex, InterruptMutexGuard, RawInterruptMutex};
Expand Down
11 changes: 10 additions & 1 deletion src/mutex/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,13 @@
pub(crate) mod spin;
pub(crate) mod spin {
/// A simple spinlock with exponential backoff.
pub type RawSpinMutex = spinning_top::RawSpinlock<spinning_top::relax::Backoff>;

/// A [`lock_api::Mutex`] based on [`RawSpinMutex`].
pub type SpinMutex<T> = lock_api::Mutex<RawSpinMutex, T>;

/// A [`lock_api::MutexGuard`] based on [`RawSpinMutex`].
pub type SpinMutexGuard<'a, T> = lock_api::MutexGuard<'a, RawSpinMutex, T>;
}
pub(crate) mod ticket;

use interrupt_mutex::RawInterruptMutex;
Expand Down
218 changes: 0 additions & 218 deletions src/mutex/spin.rs

This file was deleted.

6 changes: 3 additions & 3 deletions src/mutex/ticket.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use core::sync::atomic::{AtomicUsize, Ordering};

use lock_api::{GuardSend, RawMutex, RawMutexFair};

use crate::backoff::Backoff;
use spinning_top::relax::{Backoff, Relax};

/// A [fair] [ticket lock] with [exponential backoff].
///
Expand All @@ -28,9 +28,9 @@ unsafe impl RawMutex for RawTicketMutex {
fn lock(&self) {
let ticket = self.next_ticket.fetch_add(1, Ordering::Relaxed);

let mut backoff = Backoff::new();
let mut backoff = Backoff::default();
while self.next_serving.load(Ordering::Acquire) != ticket {
backoff.snooze();
backoff.relax();
}
}

Expand Down
Loading

0 comments on commit bca0539

Please sign in to comment.