@@ -279,11 +279,10 @@ where
279
279
}
280
280
281
281
282
- #[ cfg( all( unix, not( target_os = "emscripten" ) ) ) ]
282
+ #[ cfg( all( unix, feature = "std" , not( target_os = "emscripten" ) ) ) ]
283
283
mod fork {
284
- use core:: sync:: atomic:: { AtomicBool , AtomicUsize , Ordering } ;
285
- #[ allow( deprecated) ] // Required for compatibility with Rust < 1.24.
286
- use core:: sync:: atomic:: { ATOMIC_BOOL_INIT , ATOMIC_USIZE_INIT } ;
284
+ use core:: sync:: atomic:: { AtomicUsize , Ordering } ;
285
+ use std:: sync:: Once ;
287
286
288
287
// Fork protection
289
288
//
@@ -297,31 +296,27 @@ mod fork {
297
296
// don't update `fork_counter`, so a reseed is attempted as soon as
298
297
// possible.
299
298
300
- #[ allow( deprecated) ]
301
- static RESEEDING_RNG_FORK_COUNTER : AtomicUsize = ATOMIC_USIZE_INIT ;
299
+ static RESEEDING_RNG_FORK_COUNTER : AtomicUsize = AtomicUsize :: new ( 0 ) ;
302
300
303
301
pub fn get_fork_counter ( ) -> usize {
304
302
RESEEDING_RNG_FORK_COUNTER . load ( Ordering :: Relaxed )
305
303
}
306
304
307
- #[ allow( deprecated) ]
308
- static FORK_HANDLER_REGISTERED : AtomicBool = ATOMIC_BOOL_INIT ;
309
-
310
305
extern "C" fn fork_handler ( ) {
311
306
// Note: fetch_add is defined to wrap on overflow
312
307
// (which is what we want).
313
308
RESEEDING_RNG_FORK_COUNTER . fetch_add ( 1 , Ordering :: Relaxed ) ;
314
309
}
315
310
316
311
pub fn register_fork_handler ( ) {
317
- if ! FORK_HANDLER_REGISTERED . load ( Ordering :: Relaxed ) {
318
- unsafe { libc :: pthread_atfork ( None , None , Some ( fork_handler ) ) } ;
319
- FORK_HANDLER_REGISTERED . store ( true , Ordering :: Relaxed ) ;
320
- }
312
+ static REGISTER : Once = Once :: new ( ) ;
313
+ REGISTER . call_once ( || unsafe {
314
+ libc :: pthread_atfork ( None , None , Some ( fork_handler ) ) ;
315
+ } ) ;
321
316
}
322
317
}
323
318
324
- #[ cfg( not( all( unix, not( target_os = "emscripten" ) ) ) ) ]
319
+ #[ cfg( not( all( unix, feature = "std" , not( target_os = "emscripten" ) ) ) ) ]
325
320
mod fork {
326
321
pub fn get_fork_counter ( ) -> usize {
327
322
0
0 commit comments