@@ -41,52 +41,18 @@ unsafe fn wait(cond: *mut libc::pthread_cond_t, lock: *mut libc::pthread_mutex_t
41
41
const TIMESPEC_MAX : libc:: timespec =
42
42
libc:: timespec { tv_sec : <libc:: time_t >:: MAX , tv_nsec : 1_000_000_000 - 1 } ;
43
43
44
- fn saturating_cast_to_time_t ( value : u64 ) -> libc:: time_t {
45
- if value > <libc:: time_t >:: MAX as u64 { <libc:: time_t >:: MAX } else { value as libc:: time_t }
46
- }
47
-
48
- // This implementation is used on systems that support pthread_condattr_setclock
49
- // where we configure the condition variable to use the monotonic clock (instead of
50
- // the default system clock). This approach avoids all problems that result
51
- // from changes made to the system time.
52
- #[ cfg( not( any( target_os = "macos" , target_os = "ios" , target_os = "espidf" ) ) ) ]
53
44
unsafe fn wait_timeout (
54
45
cond : * mut libc:: pthread_cond_t ,
55
46
lock : * mut libc:: pthread_mutex_t ,
56
47
dur : Duration ,
57
48
) {
58
- use crate :: mem;
59
-
60
- let mut now: libc:: timespec = mem:: zeroed ( ) ;
61
- let r = libc:: clock_gettime ( libc:: CLOCK_MONOTONIC , & mut now) ;
62
- assert_eq ! ( r, 0 ) ;
63
- // Nanosecond calculations can't overflow because both values are below 1e9.
64
- let nsec = dur. subsec_nanos ( ) + now. tv_nsec as u32 ;
65
- let sec = saturating_cast_to_time_t ( dur. as_secs ( ) )
66
- . checked_add ( ( nsec / 1_000_000_000 ) as libc:: time_t )
67
- . and_then ( |s| s. checked_add ( now. tv_sec ) ) ;
68
- let nsec = nsec % 1_000_000_000 ;
69
- let timeout =
70
- sec. map ( |s| libc:: timespec { tv_sec : s, tv_nsec : nsec as _ } ) . unwrap_or ( TIMESPEC_MAX ) ;
71
- let r = libc:: pthread_cond_timedwait ( cond, lock, & timeout) ;
72
- assert ! ( r == libc:: ETIMEDOUT || r == 0 ) ;
73
- }
49
+ // Use the system clock on systems that do not support pthread_condattr_setclock.
50
+ // This unfortunately results in problems when the system time changes.
51
+ #[ cfg( any( target_os = "macos" , target_os = "ios" , target_os = "espidf" ) ) ]
52
+ let ( now, dur) = {
53
+ use super :: time:: SystemTime ;
54
+ use crate :: cmp:: min;
74
55
75
- // This implementation is modeled after libcxx's condition_variable
76
- // https://github.com/llvm-mirror/libcxx/blob/release_35/src/condition_variable.cpp#L46
77
- // https://github.com/llvm-mirror/libcxx/blob/release_35/include/__mutex_base#L367
78
- #[ cfg( any( target_os = "macos" , target_os = "ios" , target_os = "espidf" ) ) ]
79
- unsafe fn wait_timeout (
80
- cond : * mut libc:: pthread_cond_t ,
81
- lock : * mut libc:: pthread_mutex_t ,
82
- mut dur : Duration ,
83
- ) {
84
- use crate :: ptr;
85
-
86
- // 1000 years
87
- let max_dur = Duration :: from_secs ( 1000 * 365 * 86400 ) ;
88
-
89
- if dur > max_dur {
90
56
// OSX implementation of `pthread_cond_timedwait` is buggy
91
57
// with super long durations. When duration is greater than
92
58
// 0x100_0000_0000_0000 seconds, `pthread_cond_timedwait`
@@ -98,23 +64,19 @@ unsafe fn wait_timeout(
98
64
// To work around this issue, and possible bugs of other OSes, timeout
99
65
// is clamped to 1000 years, which is allowable per the API of `park_timeout`
100
66
// because of spurious wakeups.
101
- dur = max_dur;
102
- }
103
-
104
- let mut sys_now = libc:: timeval { tv_sec : 0 , tv_usec : 0 } ;
105
- let r = libc:: gettimeofday ( & mut sys_now, ptr:: null_mut ( ) ) ;
106
- debug_assert_eq ! ( r, 0 ) ;
107
- let nsec = dur. subsec_nanos ( ) as libc:: c_long + ( sys_now. tv_usec * 1000 ) as libc:: c_long ;
108
- let extra = ( nsec / 1_000_000_000 ) as libc:: time_t ;
109
- let nsec = nsec % 1_000_000_000 ;
110
- let seconds = saturating_cast_to_time_t ( dur. as_secs ( ) ) ;
111
- let timeout = sys_now
112
- . tv_sec
113
- . checked_add ( extra)
114
- . and_then ( |s| s. checked_add ( seconds) )
115
- . map ( |s| libc:: timespec { tv_sec : s, tv_nsec : nsec } )
116
- . unwrap_or ( TIMESPEC_MAX ) ;
117
- // And wait!
67
+ let dur = min ( dur, Duration :: from_secs ( 1000 * 365 * 86400 ) ) ;
68
+ let now = SystemTime :: now ( ) . t ;
69
+ ( now, dur)
70
+ } ;
71
+ // Use the monotonic clock on other systems.
72
+ #[ cfg( not( any( target_os = "macos" , target_os = "ios" , target_os = "espidf" ) ) ) ]
73
+ let ( now, dur) = {
74
+ use super :: time:: Timespec ;
75
+
76
+ ( Timespec :: now ( libc:: CLOCK_MONOTONIC ) , dur)
77
+ } ;
78
+
79
+ let timeout = now. checked_add_duration ( & dur) . map ( |t| t. t ) . unwrap_or ( TIMESPEC_MAX ) ;
118
80
let r = libc:: pthread_cond_timedwait ( cond, lock, & timeout) ;
119
81
debug_assert ! ( r == libc:: ETIMEDOUT || r == 0 ) ;
120
82
}
0 commit comments