File tree Expand file tree Collapse file tree 1 file changed +14
-3
lines changed Expand file tree Collapse file tree 1 file changed +14
-3
lines changed Original file line number Diff line number Diff line change @@ -24,17 +24,28 @@ pub fn timestamp_cycles() -> u64 {
24
24
25
25
#[ cfg( not( target_arch = "x86_64" ) ) ]
26
26
{
27
+ use std:: sync:: atomic:: { AtomicU64 , Ordering } ;
28
+ static TIMESTAMP : AtomicU64 = AtomicU64 :: new ( 0 ) ;
27
29
const MONOTONIC_CLOCK_MULTPIPLIER : u64 = 1_000_000_000 ;
28
30
29
31
let mut ts = libc:: timespec {
30
32
tv_sec : 0 ,
31
33
tv_nsec : 0 ,
32
34
} ;
33
35
34
- unsafe {
35
- libc:: clock_gettime ( libc:: CLOCK_MONOTONIC , & mut ts) ;
36
+ // clock_gettime(libc::CLOCK_MONOTONIC) may return the same value, so retry...
37
+ loop {
38
+ unsafe { libc:: clock_gettime ( libc:: CLOCK_MONOTONIC , & mut ts) } ;
39
+ let curr = ( ts. tv_sec as u64 ) * MONOTONIC_CLOCK_MULTPIPLIER + ( ts. tv_nsec as u64 ) ;
40
+ let prev = TIMESTAMP . load ( Ordering :: Acquire ) ;
41
+ if prev < curr
42
+ && TIMESTAMP
43
+ . compare_exchange ( prev, curr, Ordering :: AcqRel , Ordering :: Relaxed )
44
+ . is_ok ( )
45
+ {
46
+ return curr;
47
+ }
36
48
}
37
- ( ts. tv_sec as u64 ) * MONOTONIC_CLOCK_MULTPIPLIER + ( ts. tv_nsec as u64 )
38
49
}
39
50
}
40
51
You can’t perform that action at this time.
0 commit comments