@@ -62,10 +62,6 @@ impl Condvar {
6262 // where we configure condition variable to use monotonic clock (instead of
6363 // default system clock). This approach avoids all problems that result
6464 // from changes made to the system time.
65- #[ cfg( not( any( target_os = "macos" ,
66- target_os = "ios" ,
67- target_os = "android" ,
68- target_os = "hermit" ) ) ) ]
6965 pub unsafe fn wait_timeout ( & self , mutex : & Mutex , dur : Duration ) -> bool {
7066 use crate :: mem;
7167
@@ -92,78 +88,9 @@ impl Condvar {
9288 }
9389
9490
95- // This implementation is modeled after libcxx's condition_variable
96- // https://github.com/llvm-mirror/libcxx/blob/release_35/src/condition_variable.cpp#L46
97- // https://github.com/llvm-mirror/libcxx/blob/release_35/include/__mutex_base#L367
98- #[ cfg( any( target_os = "macos" , target_os = "ios" , target_os = "android" , target_os = "hermit" ) ) ]
99- pub unsafe fn wait_timeout ( & self , mutex : & Mutex , mut dur : Duration ) -> bool {
100- use crate :: ptr;
101- use crate :: time:: Instant ;
102-
103- // 1000 years
104- let max_dur = Duration :: from_secs ( 1000 * 365 * 86400 ) ;
105-
106- if dur > max_dur {
107- // OSX implementation of `pthread_cond_timedwait` is buggy
108- // with super long durations. When duration is greater than
109- // 0x100_0000_0000_0000 seconds, `pthread_cond_timedwait`
110- // in macOS Sierra return error 316.
111- //
112- // This program demonstrates the issue:
113- // https://gist.github.com/stepancheg/198db4623a20aad2ad7cddb8fda4a63c
114- //
115- // To work around this issue, and possible bugs of other OSes, timeout
116- // is clamped to 1000 years, which is allowable per the API of `wait_timeout`
117- // because of spurious wakeups.
118-
119- dur = max_dur;
120- }
121-
122- // First, figure out what time it currently is, in both system and
123- // stable time. pthread_cond_timedwait uses system time, but we want to
124- // report timeout based on stable time.
125- let mut sys_now = libc:: timeval { tv_sec : 0 , tv_usec : 0 } ;
126- let stable_now = Instant :: now ( ) ;
127- let r = libc:: gettimeofday ( & mut sys_now, ptr:: null_mut ( ) ) ;
128- debug_assert_eq ! ( r, 0 ) ;
129-
130- let nsec = dur. subsec_nanos ( ) as libc:: c_long +
131- ( sys_now. tv_usec * 1000 ) as libc:: c_long ;
132- let extra = ( nsec / 1_000_000_000 ) as libc:: time_t ;
133- let nsec = nsec % 1_000_000_000 ;
134- let seconds = saturating_cast_to_time_t ( dur. as_secs ( ) ) ;
135-
136- let timeout = sys_now. tv_sec . checked_add ( extra) . and_then ( |s| {
137- s. checked_add ( seconds)
138- } ) . map ( |s| {
139- libc:: timespec { tv_sec : s, tv_nsec : nsec }
140- } ) . unwrap_or ( TIMESPEC_MAX ) ;
141-
142- // And wait!
143- let r = libc:: pthread_cond_timedwait ( self . inner . get ( ) , mutex:: raw ( mutex) ,
144- & timeout) ;
145- debug_assert ! ( r == libc:: ETIMEDOUT || r == 0 ) ;
146-
147- // ETIMEDOUT is not a totally reliable method of determining timeout due
148- // to clock shifts, so do the check ourselves
149- stable_now. elapsed ( ) < dur
150- }
151-
15291 #[ inline]
153- #[ cfg( not( target_os = "dragonfly" ) ) ]
15492 pub unsafe fn destroy ( & self ) {
15593 let r = libc:: pthread_cond_destroy ( self . inner . get ( ) ) ;
15694 debug_assert_eq ! ( r, 0 ) ;
15795 }
158-
159- #[ inline]
160- #[ cfg( target_os = "dragonfly" ) ]
161- pub unsafe fn destroy ( & self ) {
162- let r = libc:: pthread_cond_destroy ( self . inner . get ( ) ) ;
163- // On DragonFly pthread_cond_destroy() returns EINVAL if called on
164- // a condvar that was just initialized with
165- // libc::PTHREAD_COND_INITIALIZER. Once it is used or
166- // pthread_cond_init() is called, this behaviour no longer occurs.
167- debug_assert ! ( r == 0 || r == libc:: EINVAL ) ;
168- }
16996}
0 commit comments