Skip to content

Commit

Permalink
test: fix excessive loom branches with Backoff (#472)
Browse files Browse the repository at this point in the history
This fixes an issue where the `util::Backoff` helper in `maitake_sync`
would emit too many spins in loom mode, hitting the branch limit.
  • Loading branch information
hawkw committed Jan 27, 2024
1 parent f27ad29 commit c80686d
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 12 deletions.
16 changes: 8 additions & 8 deletions maitake-sync/src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,6 @@
//!
//! - [`Backoff`]: exponential backoff for spin loops
//! - [`CachePadded`]: pads and aligns a value to the size of a cache line
mod backoff;
mod cache_pad;
pub(crate) mod fmt;
mod maybe_uninit;
mod wake_batch;

pub use self::{backoff::Backoff, cache_pad::CachePadded};
pub(crate) use self::{maybe_uninit::CheckedMaybeUninit, wake_batch::WakeBatch};
#[cfg(any(test, feature = "tracing"))]
macro_rules! trace {
Expand Down Expand Up @@ -196,8 +188,16 @@ macro_rules! unreachable_unchecked {
});
}

mod backoff;
mod cache_pad;
pub(crate) mod fmt;
mod maybe_uninit;
mod wake_batch;

#[cfg(all(test, not(loom)))]
pub(crate) use self::test::trace_init;
pub use self::{backoff::Backoff, cache_pad::CachePadded};
pub(crate) use self::{maybe_uninit::CheckedMaybeUninit, wake_batch::WakeBatch};

#[cfg(test)]
pub(crate) mod test {
Expand Down
14 changes: 10 additions & 4 deletions maitake-sync/src/util/backoff.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
use crate::loom::hint;

/// An [exponential backoff] for spin loops.
///
/// This is a helper struct for spinning in a busy loop, with an exponentially
Expand Down Expand Up @@ -53,8 +51,16 @@ impl Backoff {
#[inline(always)]
pub fn spin(&mut self) {
// Issue 2^exp pause instructions.
for _ in 0..1 << self.exp {
hint::spin_loop();
let spins = 1 << self.exp;
#[cfg(not(loom))]
for _ in 0..spins {
crate::loom::hint::spin_loop();
}

#[cfg(loom)]
{
test_debug!("would back off for {spins} spins");
loom::thread::yield_now();
}

if self.exp < self.max {
Expand Down

0 comments on commit c80686d

Please sign in to comment.