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