-
Notifications
You must be signed in to change notification settings - Fork 698
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Use Once instead of lazy_static #790
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -208,23 +208,19 @@ mod sysrand { | |
))] | ||
mod urandom { | ||
use crate::error; | ||
use spin::Once; | ||
use std; | ||
|
||
pub fn fill(dest: &mut [u8]) -> Result<(), error::Unspecified> { | ||
use lazy_static::lazy_static; | ||
static OPEN: Once<Result<std::fs::File, std::io::Error>> = Once::new(); | ||
|
||
#[cfg(target_os = "redox")] | ||
static RANDOM_PATH: &str = "rand:"; | ||
#[cfg(unix)] | ||
static RANDOM_PATH: &str = "/dev/urandom"; | ||
|
||
lazy_static! { | ||
static ref FILE: Result<std::fs::File, std::io::Error> = | ||
std::fs::File::open(RANDOM_PATH); | ||
} | ||
|
||
match *FILE { | ||
Ok(ref file) => { | ||
match OPEN.call_once(|| std::fs::File::open(RANDOM_PATH)) { | ||
Ok(file) => { | ||
use std::io::Read; | ||
(&*file).read_exact(dest).map_err(|_| error::Unspecified) | ||
}, | ||
|
@@ -237,29 +233,19 @@ mod urandom { | |
#[cfg(all(target_os = "linux", feature = "dev_urandom_fallback"))] | ||
mod sysrand_or_urandom { | ||
use crate::error; | ||
use spin::Once; | ||
|
||
enum Mechanism { | ||
Sysrand, | ||
DevURandom, | ||
fn sysrand_supported() -> bool { | ||
let mut dummy = [0u8; 1]; | ||
super::sysrand_chunk::chunk(&mut dummy[..]).is_ok() | ||
} | ||
|
||
pub fn fill(dest: &mut [u8]) -> Result<(), error::Unspecified> { | ||
use lazy_static::lazy_static; | ||
|
||
lazy_static! { | ||
static ref MECHANISM: Mechanism = { | ||
let mut dummy = [0u8; 1]; | ||
if super::sysrand_chunk::chunk(&mut dummy[..]).is_err() { | ||
Mechanism::DevURandom | ||
} else { | ||
Mechanism::Sysrand | ||
} | ||
}; | ||
} | ||
|
||
match *MECHANISM { | ||
Mechanism::Sysrand => super::sysrand::fill(dest), | ||
Mechanism::DevURandom => super::urandom::fill(dest), | ||
static SYSRAND_SUPPORTED: Once<bool> = Once::new(); | ||
if *SYSRAND_SUPPORTED.call_once(sysrand_supported) { | ||
super::sysrand::fill(dest) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. On Linux, at least, How about, instead, we keep track of whether if 0, call The effect of this would be to push the mutex down to the kernel, out of this userspace process. Presumably the kernel isn't going to use spin locks in such a way that would cause it to start itself of CPU time needed to initialize the entropy pool. WDYT? |
||
} else { | ||
super::urandom::fill(dest) | ||
} | ||
} | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Here, wouldn't it make more sense to use
std::sync::Once
instead ofspin::Once
? We really don't want threads spinning while we wait for the file open operation to complete (and we can't try to open the file more than once concurrently, because we want to minimize the number of file handles we try to acquire).