Skip to content

Commit e20bcc7

Browse files
committed
updates
1 parent 109b54e commit e20bcc7

10 files changed

+135
-303
lines changed

src/dragonfly_haiku_emscripten.rs

Lines changed: 0 additions & 87 deletions
This file was deleted.

src/lib.rs

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -133,10 +133,6 @@ extern crate wasm_bindgen;
133133
#[allow(unused)]
134134
#[cfg(not(feature = "log"))] macro_rules! error { ($($x:tt)*) => () }
135135

136-
#[cfg(any(
137-
target_arch = "wasm32",
138-
))]
139-
mod utils;
140136
mod error;
141137
pub use error::Error;
142138

@@ -156,17 +152,17 @@ macro_rules! mod_use {
156152
mod_use!(cfg(target_os = "android"), linux_android);
157153
mod_use!(cfg(target_os = "bitrig"), openbsd_bitrig);
158154
mod_use!(cfg(target_os = "cloudabi"), cloudabi);
159-
mod_use!(cfg(target_os = "dragonfly"), dragonfly_haiku_emscripten);
160-
mod_use!(cfg(target_os = "emscripten"), dragonfly_haiku_emscripten);
155+
mod_use!(cfg(target_os = "dragonfly"), use_file);
156+
mod_use!(cfg(target_os = "emscripten"), use_file);
161157
mod_use!(cfg(target_os = "freebsd"), freebsd);
162158
mod_use!(cfg(target_os = "fuchsia"), fuchsia);
163-
mod_use!(cfg(target_os = "haiku"), dragonfly_haiku_emscripten);
159+
mod_use!(cfg(target_os = "haiku"), use_file);
164160
mod_use!(cfg(target_os = "ios"), macos);
165161
mod_use!(cfg(target_os = "linux"), linux_android);
166162
mod_use!(cfg(target_os = "macos"), macos);
167-
mod_use!(cfg(target_os = "netbsd"), netbsd);
163+
mod_use!(cfg(target_os = "netbsd"), use_file);
168164
mod_use!(cfg(target_os = "openbsd"), openbsd_bitrig);
169-
mod_use!(cfg(target_os = "redox"), redox);
165+
mod_use!(cfg(target_os = "redox"), use_file);
170166
mod_use!(cfg(target_os = "solaris"), solaris);
171167
mod_use!(cfg(target_os = "illumos"), solaris);
172168
mod_use!(cfg(windows), windows);

src/linux_android.rs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,16 @@ extern crate libc;
1111

1212
use Error;
1313
use std::fs::File;
14-
use std::{io, mem, thread};
1514
use std::io::Read;
1615
use std::num::NonZeroU32;
17-
use std::sync::atomic::{AtomicUsize, Ordering};
16+
use std::sync::atomic::{AtomicIsize, AtomicUsize, Ordering};
1817
use std::os::unix::io::{AsRawFd, FromRawFd};
18+
use std::{io, mem, thread, time};
1919

2020
// replace with AtomicU8 on stabilization and MSRV bump
2121
static RNG_STATE: AtomicUsize = AtomicUsize::new(0);
2222
// replace with AtomicI32 on stabilization and MSRV bump
23-
static RNG_FD: AtomicUsize = AtomicUsize::new(0);
23+
static RNG_FD: AtomicIsize = AtomicIsize::new(-1);
2424

2525
const STATE_INIT_ONGOING: usize = 1 << 0;
2626
const STATE_USE_SYSCALL: usize = 1 << 1;
@@ -42,7 +42,8 @@ fn init_loop(dest: &mut [u8]) -> Result<(), Error> {
4242
let state = RNG_STATE.fetch_or(STATE_INIT_ONGOING, Ordering::AcqRel);
4343

4444
if state & STATE_INIT_ONGOING != 0 {
45-
thread::yield_now();
45+
// initialization is not finished, so wait
46+
thread::sleep(time::Duration::from_nanos(1));
4647
continue;
4748
}
4849
return if state & STATE_USE_SYSCALL != 0 {
@@ -64,8 +65,8 @@ fn init(dest: &mut [u8]) -> Result<(), Error> {
6465
Err(err) if err.code().get() as i32 == libc::ENOSYS => {
6566
match init_fd() {
6667
Ok(fd) => {
67-
RNG_FD.store(fd as usize, Ordering::Release);
68-
RNG_STATE.store(STATE_USE_FD, Ordering::Release);
68+
RNG_FD.store(fd as isize, Ordering::SeqCst);
69+
RNG_STATE.store(STATE_USE_FD, Ordering::SeqCst);
6970
use_fd(dest)
7071
},
7172
Err(err) => {
@@ -79,6 +80,7 @@ fn init(dest: &mut [u8]) -> Result<(), Error> {
7980
}
8081

8182
fn init_fd() -> io::Result<i32> {
83+
// read one byte from "/dev/random" to ensure that OS RNG has initialized
8284
File::open("/dev/random")?.read_exact(&mut [0u8; 1])?;
8385
let f = File::open("/dev/urandom")?;
8486
let fd = f.as_raw_fd();

src/netbsd.rs

Lines changed: 0 additions & 74 deletions
This file was deleted.

src/redox.rs

Lines changed: 0 additions & 75 deletions
This file was deleted.

src/solaris.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,9 @@ use Error;
2323
use std::fs::File;
2424
use std::io::Read;
2525
use std::num::NonZeroU32;
26-
use std::sync::atomic::{AtomicUsize, Ordering};
26+
use std::sync::atomic::{AtomicIsize, AtomicUsize, Ordering};
2727
use std::os::unix::io::{RawFd, AsRawFd, FromRawFd};
28-
use std::{io, mem, thread};
28+
use std::{io, mem, thread, time};
2929

3030
#[cfg(target_os = "illumos")]
3131
type GetRandomFn = unsafe extern "C" fn(*mut u8, libc::size_t, libc::c_uint) -> libc::ssize_t;
@@ -34,7 +34,7 @@ type GetRandomFn = unsafe extern "C" fn(*mut u8, libc::size_t, libc::c_uint) ->
3434

3535
static RNG_FN: AtomicUsize = AtomicUsize::new(0);
3636
// replace with AtomicI32 on stabilization and MSRV bump
37-
static RNG_FD: AtomicUsize = AtomicUsize::new(-1i32 as usize);
37+
static RNG_FD: AtomicIsize = AtomicIsize::new(-1);
3838
// replace with AtomicU8 on stabilization and MSRV bump
3939
static RNG_STATE: AtomicUsize = AtomicUsize::new(0);
4040

@@ -57,7 +57,7 @@ pub fn getrandom_inner(dest: &mut [u8]) -> Result<(), Error> {
5757
if state & STATE_INIT_DONE != 0 { break; }
5858
if state & STATE_INIT_ONGOING != 0 {
5959
// initialization is not finished, so wait
60-
thread::yield_now();
60+
thread::sleep(time::Duration::from_nanos(1));
6161
continue;
6262
}
6363

@@ -70,12 +70,12 @@ pub fn getrandom_inner(dest: &mut [u8]) -> Result<(), Error> {
7070
return Err(err.into());
7171
},
7272
};
73-
RNG_FD.store(f.as_raw_fd() as usize, Ordering::Release);
73+
RNG_FD.store(f.as_raw_fd() as isize, Ordering::SeqCst);
7474
mem::forget(f);
7575
} else {
76-
RNG_FN.store(f, Ordering::Release);
76+
RNG_FN.store(f, Ordering::SeqCst);
7777
}
78-
RNG_STATE.store(STATE_INIT_DONE, Ordering::Release);
78+
RNG_STATE.store(STATE_INIT_DONE, Ordering::SeqCst);
7979
break;
8080
}
8181

0 commit comments

Comments
 (0)