@@ -8,13 +8,12 @@ use core::{
8
8
9
9
use portable_atomic:: AtomicU32 ;
10
10
11
- // static mut RAND: Option<GnuRand> = None;
12
11
static RAND_STATE : AtomicU32 = AtomicU32 :: new ( 0x0 ) ;
13
12
14
13
/// Rust implementation of C library function `srand`
15
14
#[ cfg_attr( feature = "rand" , no_mangle) ]
16
15
pub extern "C" fn srand ( seed : c_uint ) {
17
- RAND_STATE . store ( seed, Ordering :: Release ) ;
16
+ RAND_STATE . store ( seed, Ordering :: Relaxed ) ;
18
17
}
19
18
20
19
/// Rust implementation of C library function `rand`.
@@ -28,24 +27,20 @@ pub extern "C" fn srand(seed: c_uint) {
28
27
#[ cfg_attr( feature = "rand" , no_mangle) ]
29
28
pub extern "C" fn rand ( ) -> c_int {
30
29
let mut current_state = RAND_STATE . load ( Ordering :: Relaxed ) ;
31
- let mut new_state = current_state;
32
- let mut result = unsafe { crate :: rand_r ( & mut new_state as * mut _ ) } ;
33
30
34
31
loop {
32
+ let mut new_state = current_state;
33
+ let result = unsafe { crate :: rand_r ( & mut new_state as * mut _ ) } ;
35
34
match RAND_STATE . compare_exchange_weak (
36
35
current_state,
37
36
new_state,
38
37
Ordering :: SeqCst ,
39
38
Ordering :: Relaxed ,
40
39
) {
41
- Ok ( _) => break ,
40
+ Ok ( _) => return result as _ ,
42
41
Err ( c) => current_state = c,
43
42
}
44
- new_state = current_state;
45
- result = unsafe { crate :: rand_r ( & mut new_state as * mut _ ) } ;
46
43
}
47
-
48
- result as _
49
44
}
50
45
51
46
#[ cfg( test) ]
0 commit comments