File tree 1 file changed +14
-7
lines changed
1 file changed +14
-7
lines changed Original file line number Diff line number Diff line change @@ -14,17 +14,24 @@ pub fn hashmap_random_keys() -> (u64, u64) {
14
14
mod imp {
15
15
use libc;
16
16
use crate :: io;
17
-
18
- extern "C" {
19
- fn randBytes ( randBuf : * mut libc:: c_uchar ,
20
- numOfBytes : libc:: c_int ) -> libc:: c_int ;
21
- }
17
+ use core:: sync:: atomic:: { AtomicBool , Ordering :: Relaxed } ;
22
18
23
19
pub fn fill_bytes ( v : & mut [ u8 ] ) {
20
+ static RNG_INIT : AtomicBool = AtomicBool :: new ( false ) ;
21
+ while !RNG_INIT . load ( Relaxed ) {
22
+ let ret = unsafe { libc:: randSecure ( ) } ;
23
+ if ret < 0 {
24
+ panic ! ( "couldn't generate random bytes: {}" , io:: Error :: last_os_error( ) ) ;
25
+ } else if ret > 0 {
26
+ RNG_INIT . store ( true , Relaxed ) ;
27
+ break ;
28
+ }
29
+ unsafe { libc:: usleep ( 10 ) } ;
30
+ }
24
31
let ret = unsafe {
25
- randBytes ( v. as_mut_ptr ( ) as * mut libc:: c_uchar , v. len ( ) as libc:: c_int )
32
+ libc :: randABytes ( v. as_mut_ptr ( ) as * mut libc:: c_uchar , v. len ( ) as libc:: c_int )
26
33
} ;
27
- if ret == - 1 {
34
+ if ret < 0 {
28
35
panic ! ( "couldn't generate random bytes: {}" , io:: Error :: last_os_error( ) ) ;
29
36
}
30
37
}
You can’t perform that action at this time.
0 commit comments