Skip to content

Commit c5e2025

Browse files
josephlrnewpavlov
authored andcommitted
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 <[email protected]>
1 parent 46963aa commit c5e2025

File tree

2 files changed

+0
-63
lines changed

2 files changed

+0
-63
lines changed

src/util.rs

Lines changed: 0 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,6 @@ impl LazyUsize {
3535

3636
// The initialization is not completed.
3737
pub const UNINIT: usize = usize::max_value();
38-
// The initialization is currently running.
39-
pub const ACTIVE: usize = usize::max_value() - 1;
4038

4139
// Runs the init() function at least once, returning the value of some run
4240
// of init(). Multiple callers can run their init() functions in parallel.
@@ -50,36 +48,6 @@ impl LazyUsize {
5048
}
5149
val
5250
}
53-
54-
// Synchronously runs the init() function. Only one caller will have their
55-
// init() function running at a time, and exactly one successful call will
56-
// be run. init() returning UNINIT or ACTIVE will be considered a failure,
57-
// and future calls to sync_init will rerun their init() function.
58-
pub fn sync_init(&self, init: impl FnOnce() -> usize, mut wait: impl FnMut()) -> usize {
59-
// Common and fast path with no contention. Don't wast time on CAS.
60-
match self.0.load(Relaxed) {
61-
Self::UNINIT | Self::ACTIVE => {}
62-
val => return val,
63-
}
64-
// Relaxed ordering is fine, as we only have a single atomic variable.
65-
loop {
66-
match self.0.compare_and_swap(Self::UNINIT, Self::ACTIVE, Relaxed) {
67-
Self::UNINIT => {
68-
let val = init();
69-
self.0.store(
70-
match val {
71-
Self::UNINIT | Self::ACTIVE => Self::UNINIT,
72-
val => val,
73-
},
74-
Relaxed,
75-
);
76-
return val;
77-
}
78-
Self::ACTIVE => wait(),
79-
val => return val,
80-
}
81-
}
82-
}
8351
}
8452

8553
// Identical to LazyUsize except with bool instead of usize.

src/util_libc.rs

Lines changed: 0 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -98,37 +98,6 @@ impl Weak {
9898
}
9999
}
100100

101-
pub struct LazyFd(LazyUsize);
102-
103-
impl LazyFd {
104-
pub const fn new() -> Self {
105-
Self(LazyUsize::new())
106-
}
107-
108-
// If init() returns Some(x), x should be nonnegative.
109-
pub fn init(&self, init: impl FnOnce() -> Option<libc::c_int>) -> Option<libc::c_int> {
110-
let fd = self.0.sync_init(
111-
|| match init() {
112-
// OK as val >= 0 and val <= c_int::MAX < usize::MAX
113-
Some(val) => val as usize,
114-
None => LazyUsize::UNINIT,
115-
},
116-
|| unsafe {
117-
// We are usually waiting on an open(2) syscall to complete,
118-
// which typically takes < 10us if the file is a device.
119-
// However, we might end up waiting much longer if the entropy
120-
// pool isn't initialized, but even in that case, this loop will
121-
// consume a negligible amount of CPU on most platforms.
122-
libc::usleep(10);
123-
},
124-
);
125-
match fd {
126-
LazyUsize::UNINIT => None,
127-
val => Some(val as libc::c_int),
128-
}
129-
}
130-
}
131-
132101
cfg_if! {
133102
if #[cfg(any(target_os = "linux", target_os = "emscripten"))] {
134103
use libc::open64 as open;

0 commit comments

Comments
 (0)