util: Remove unused spin-lock interfaces
We no longer use spin-locks anywhere in getrandom, so remove any interfaces which spin. Signed-off-by: Joe Richey <joerichey@google.com>
This commit is contained in:
committed by
Artyom Pavlov
parent
46963aac74
commit
c5e2025d2c
-32
@@ -35,8 +35,6 @@ impl LazyUsize {
|
||||
|
||||
// The initialization is not completed.
|
||||
pub const UNINIT: usize = usize::max_value();
|
||||
// The initialization is currently running.
|
||||
pub const ACTIVE: usize = usize::max_value() - 1;
|
||||
|
||||
// Runs the init() function at least once, returning the value of some run
|
||||
// of init(). Multiple callers can run their init() functions in parallel.
|
||||
@@ -50,36 +48,6 @@ impl LazyUsize {
|
||||
}
|
||||
val
|
||||
}
|
||||
|
||||
// Synchronously runs the init() function. Only one caller will have their
|
||||
// init() function running at a time, and exactly one successful call will
|
||||
// be run. init() returning UNINIT or ACTIVE will be considered a failure,
|
||||
// and future calls to sync_init will rerun their init() function.
|
||||
pub fn sync_init(&self, init: impl FnOnce() -> usize, mut wait: impl FnMut()) -> usize {
|
||||
// Common and fast path with no contention. Don't wast time on CAS.
|
||||
match self.0.load(Relaxed) {
|
||||
Self::UNINIT | Self::ACTIVE => {}
|
||||
val => return val,
|
||||
}
|
||||
// Relaxed ordering is fine, as we only have a single atomic variable.
|
||||
loop {
|
||||
match self.0.compare_and_swap(Self::UNINIT, Self::ACTIVE, Relaxed) {
|
||||
Self::UNINIT => {
|
||||
let val = init();
|
||||
self.0.store(
|
||||
match val {
|
||||
Self::UNINIT | Self::ACTIVE => Self::UNINIT,
|
||||
val => val,
|
||||
},
|
||||
Relaxed,
|
||||
);
|
||||
return val;
|
||||
}
|
||||
Self::ACTIVE => wait(),
|
||||
val => return val,
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Identical to LazyUsize except with bool instead of usize.
|
||||
|
||||
@@ -98,37 +98,6 @@ impl Weak {
|
||||
}
|
||||
}
|
||||
|
||||
pub struct LazyFd(LazyUsize);
|
||||
|
||||
impl LazyFd {
|
||||
pub const fn new() -> Self {
|
||||
Self(LazyUsize::new())
|
||||
}
|
||||
|
||||
// If init() returns Some(x), x should be nonnegative.
|
||||
pub fn init(&self, init: impl FnOnce() -> Option<libc::c_int>) -> Option<libc::c_int> {
|
||||
let fd = self.0.sync_init(
|
||||
|| match init() {
|
||||
// OK as val >= 0 and val <= c_int::MAX < usize::MAX
|
||||
Some(val) => val as usize,
|
||||
None => LazyUsize::UNINIT,
|
||||
},
|
||||
|| unsafe {
|
||||
// We are usually waiting on an open(2) syscall to complete,
|
||||
// which typically takes < 10us if the file is a device.
|
||||
// However, we might end up waiting much longer if the entropy
|
||||
// pool isn't initialized, but even in that case, this loop will
|
||||
// consume a negligible amount of CPU on most platforms.
|
||||
libc::usleep(10);
|
||||
},
|
||||
);
|
||||
match fd {
|
||||
LazyUsize::UNINIT => None,
|
||||
val => Some(val as libc::c_int),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
cfg_if! {
|
||||
if #[cfg(any(target_os = "linux", target_os = "emscripten"))] {
|
||||
use libc::open64 as open;
|
||||
|
||||
Reference in New Issue
Block a user