Skip to content
Merged
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
19 changes: 14 additions & 5 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,26 +101,35 @@ const fn bin_size(array: u32) -> usize {
(1 << 3) << array
}

#[cfg(feature = "std")]
fn spin_wait(failures: &mut usize) {
// Saturate, since a long enough wait would overflow a 32-bit counter.
*failures = failures.saturating_add(1);
if *failures <= 3 || cfg!(not(feature = "std")) {
if *failures <= 3 {
// If there haven't been many failures yet, then we optimistically
// spinloop. Without `std` there is no scheduler to yield to, so we
// always spin.
for _ in 0..(1 << (*failures).min(3)) {
// spinloop.
for _ in 0..(1 << *failures) {
core::hint::spin_loop();
}
} else {
// If there have been many failures, then continuing to spinloop will
// probably just waste CPU, and whoever we are waiting for has been
// preempted then spinning could actively delay completion of the task.
// So instead, we cooperatively yield to the OS scheduler.
#[cfg(feature = "std")]
std::thread::yield_now();
}
}

/// Without `std` there is no scheduler to yield to, so we always spin, backing
/// off up to 8 spins per call.
#[cfg(not(feature = "std"))]
fn spin_wait(failures: &mut usize) {
*failures = (*failures + 1).min(3);
for _ in 0..(1 << *failures) {
core::hint::spin_loop();
}
}

#[test]
fn test_indices() {
for i in 0..32 {
Expand Down
Loading