Skip to content

Commit

Permalink
document get_parallelism, minor fix
Browse files Browse the repository at this point in the history
  • Loading branch information
fereidani committed May 31, 2023
1 parent 2287500 commit efb8713
Showing 1 changed file with 10 additions and 0 deletions.
10 changes: 10 additions & 0 deletions src/backoff.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,15 +91,24 @@ pub fn randomize(d: usize) -> usize {
d - (d >> 3) + random_u32() as usize % (d >> 2)
}

// Static atomic variable used to store the degree of parallelism.
// Initialized to 0, meaning that the parallelism degree has not been computed yet.
static PARALELLISM: AtomicUsize = AtomicUsize::new(0);

// This function retrieves the available degree of parallelism.
// If the degree of parallelism has not been computed yet, it computes and stores it in the PARALELLISM atomic variable.
// The degree of parallelism typically corresponds to the number of processor cores that can execute threads concurrently.
#[inline(always)]
pub fn get_parallelism() -> usize {
let mut p = PARALELLISM.load(Ordering::Relaxed);
// If the parallelism degree has not been computed yet.
if p == 0 {
// Try to get the degree of parallelism from available_parallelism.
// If it is not available, default to 1.
p = usize::from(available_parallelism().unwrap_or(NonZeroUsize::new(1).unwrap()));
PARALELLISM.store(p, Ordering::SeqCst);
}
// Return the computed degree of parallelism.
p
}

Expand Down Expand Up @@ -134,6 +143,7 @@ pub fn spin_cond<F: Fn() -> bool>(cond: F) {
while !cond() {
yield_now_std();
}
return;
}

const NO_YIELD: usize = 1;
Expand Down

0 comments on commit efb8713

Please sign in to comment.