-
-
Save newpavlov/ca9442fd5c1e75e90232db209f7174d8 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use std::io; | |
use std::io::Read; | |
use std::sync::Once; | |
use std::os::unix::io::{RawFd, AsRawFd, FromRawFd}; | |
use std::time::Instant; | |
use std::fs::File; | |
static mut RNG_SOURCE: RawFd = 0; | |
static RNG_INIT: Once = Once::new(); | |
fn get_fd() -> RawFd { | |
unsafe { | |
RNG_INIT.call_once(|| { | |
let f = File::open("/dev/urandom").unwrap(); | |
RNG_SOURCE = f.as_raw_fd(); | |
std::mem::forget(f); | |
}); | |
RNG_SOURCE | |
} | |
} | |
fn fill(buf: &mut [u8]) -> io::Result<()> { | |
unsafe { | |
let mut f = File::from_raw_fd(get_fd()); | |
f.read_exact(buf)?; | |
std::mem::forget(f); | |
} | |
Ok(()) | |
} | |
fn main() { | |
let mut buf = vec![0u8; 1_000_000_000]; | |
let t0 = Instant::now(); | |
let handle = std::thread::spawn(move || { | |
std::thread::sleep(std::time::Duration::from_millis(100)); | |
let t1 = Instant::now(); | |
let mut buf = [0u8; 32]; | |
fill(&mut buf).unwrap(); | |
println!("thread 1: {:?} {:?}", t0.elapsed(), t1.elapsed()); | |
}); | |
fill(&mut buf).unwrap(); | |
println!("thread 0: {:?}", t0.elapsed()); | |
handle.join().unwrap(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment