File tree 2 files changed +15
-3
lines changed
2 files changed +15
-3
lines changed Original file line number Diff line number Diff line change @@ -53,7 +53,8 @@ impl LazyUsize {
53
53
54
54
// Synchronously runs the init() function. Only one caller will have their
55
55
// init() function running at a time, and exactly one successful call will
56
- // be run. The init() function should never return LazyUsize::ACTIVE.
56
+ // be run. init() returning UNINIT or ACTIVE will be considered a failure,
57
+ // and future calls to sync_init will rerun their init() function.
57
58
pub fn sync_init ( & self , init : impl FnOnce ( ) -> usize , mut wait : impl FnMut ( ) ) -> usize {
58
59
// Common and fast path with no contention. Don't wast time on CAS.
59
60
match self . 0 . load ( Relaxed ) {
@@ -65,7 +66,13 @@ impl LazyUsize {
65
66
match self . 0 . compare_and_swap ( Self :: UNINIT , Self :: ACTIVE , Relaxed ) {
66
67
Self :: UNINIT => {
67
68
let val = init ( ) ;
68
- self . 0 . store ( val, Relaxed ) ;
69
+ self . 0 . store (
70
+ match val {
71
+ Self :: UNINIT | Self :: ACTIVE => Self :: UNINIT ,
72
+ val => val,
73
+ } ,
74
+ Relaxed ,
75
+ ) ;
69
76
return val;
70
77
}
71
78
Self :: ACTIVE => wait ( ) ,
Original file line number Diff line number Diff line change @@ -80,7 +80,12 @@ impl LazyFd {
80
80
None => LazyUsize :: UNINIT ,
81
81
} ,
82
82
|| unsafe {
83
- libc:: usleep ( 1000 ) ;
83
+ // We are usually waiting on an open(2) syscall to complete,
84
+ // which typically takes < 10us if the file is a device.
85
+ // However, we might end up waiting much longer if the entropy
86
+ // pool isn't initialized, but even in that case, this loop will
87
+ // consume a negligible amount of CPU on most platforms.
88
+ libc:: usleep ( 10 ) ;
84
89
} ,
85
90
) ;
86
91
match fd {
You can’t perform that action at this time.
0 commit comments