#[derive(Debug, Clone, Copy, Eq, PartialEq)]
pub enum FastCasPolicy {
Once,
Spin {
max_attempts: u32,
},
SpinYield {
spin_attempts: u32,
max_attempts: u32,
},
}
impl FastCasPolicy {
#[inline]
pub const fn once() -> Self {
Self::Once
}
#[inline]
pub const fn spin(max_attempts: u32) -> Self {
Self::Spin {
max_attempts: normalize_attempts(max_attempts),
}
}
#[inline]
pub const fn spin_yield(spin_attempts: u32, max_attempts: u32) -> Self {
let max_attempts = normalize_attempts(max_attempts);
let spin_attempts = if spin_attempts < max_attempts {
spin_attempts
} else {
max_attempts
};
Self::SpinYield {
spin_attempts,
max_attempts,
}
}
#[inline]
pub const fn max_attempts(self) -> u32 {
match self {
Self::Once => 1,
Self::Spin { max_attempts } => normalize_attempts(max_attempts),
Self::SpinYield { max_attempts, .. } => normalize_attempts(max_attempts),
}
}
#[inline]
pub const fn should_yield_before(self, next_attempt: u32) -> bool {
match self {
Self::SpinYield { spin_attempts, .. } => next_attempt > spin_attempts,
Self::Once | Self::Spin { .. } => false,
}
}
}
#[inline]
const fn normalize_attempts(attempts: u32) -> u32 {
if attempts == 0 { 1 } else { attempts }
}