@@ -140,33 +140,43 @@ impl Condvar {
140
140
/// Wait on this condition variable for a notification, timing out after a
141
141
/// specified duration.
142
142
///
143
- /// The semantics of this function are equivalent to `wait()` except that
144
- /// the thread will be blocked for roughly no longer than `dur`. This method
145
- /// should not be used for precise timing due to anomalies such as
146
- /// preemption or platform differences that may not cause the maximum amount
147
- /// of time waited to be precisely `dur`.
143
+ /// The semantics of this function are equivalent to `wait()`
144
+ /// except that the thread will be blocked for roughly no longer
145
+ /// than `ms` milliseconds. This method should not be used for
146
+ /// precise timing due to anomalies such as preemption or platform
147
+ /// differences that may not cause the maximum amount of time
148
+ /// waited to be precisely `ms`.
148
149
///
149
- /// If the wait timed out, then `false` will be returned. Otherwise if a
150
- /// notification was received then `true` will be returned .
150
+ /// The returned boolean is `false` only if the timeout is known
151
+ /// to have elapsed .
151
152
///
152
153
/// Like `wait`, the lock specified will be re-acquired when this function
153
154
/// returns, regardless of whether the timeout elapsed or not.
154
- #[ unstable ( feature = "std_misc " ) ]
155
- pub fn wait_timeout < ' a , T > ( & self , guard : MutexGuard < ' a , T > , dur : Duration )
156
- -> LockResult < ( MutexGuard < ' a , T > , bool ) > {
155
+ #[ stable ( feature = "rust1" , since = "1.0.0 ") ]
156
+ pub fn wait_timeout_ms < ' a , T > ( & self , guard : MutexGuard < ' a , T > , ms : u32 )
157
+ -> LockResult < ( MutexGuard < ' a , T > , bool ) > {
157
158
unsafe {
158
159
let me: & ' static Condvar = & * ( self as * const _ ) ;
159
- me. inner . wait_timeout ( guard, dur )
160
+ me. inner . wait_timeout_ms ( guard, ms )
160
161
}
161
162
}
162
163
164
+ /// Deprecated: use `wait_timeout_ms` instead.
165
+ #[ unstable( feature = "std_misc" ) ]
166
+ #[ deprecated( since = "1.0.0" , reason = "use wait_timeout_ms instead" ) ]
167
+ pub fn wait_timeout < ' a , T > ( & self , guard : MutexGuard < ' a , T > , dur : Duration )
168
+ -> LockResult < ( MutexGuard < ' a , T > , bool ) > {
169
+ self . wait_timeout_ms ( guard, dur. num_milliseconds ( ) as u32 )
170
+ }
171
+
163
172
/// Wait on this condition variable for a notification, timing out after a
164
173
/// specified duration.
165
174
///
166
175
/// The semantics of this function are equivalent to `wait_timeout` except
167
176
/// that the implementation will repeatedly wait while the duration has not
168
177
/// passed and the provided function returns `false`.
169
- #[ unstable( feature = "std_misc" ) ]
178
+ #[ unstable( feature = "wait_timeout_with" ,
179
+ reason = "unsure if this API is broadly needed or what form it should take" ) ]
170
180
pub fn wait_timeout_with < ' a , T , F > ( & self ,
171
181
guard : MutexGuard < ' a , T > ,
172
182
dur : Duration ,
@@ -235,12 +245,12 @@ impl StaticCondvar {
235
245
/// See `Condvar::wait_timeout`.
236
246
#[ unstable( feature = "std_misc" ,
237
247
reason = "may be merged with Condvar in the future" ) ]
238
- pub fn wait_timeout < ' a , T > ( & ' static self , guard : MutexGuard < ' a , T > , dur : Duration )
239
- -> LockResult < ( MutexGuard < ' a , T > , bool ) > {
248
+ pub fn wait_timeout_ms < ' a , T > ( & ' static self , guard : MutexGuard < ' a , T > , ms : u32 )
249
+ -> LockResult < ( MutexGuard < ' a , T > , bool ) > {
240
250
let ( poisoned, success) = unsafe {
241
251
let lock = mutex:: guard_lock ( & guard) ;
242
252
self . verify ( lock) ;
243
- let success = self . inner . wait_timeout ( lock, dur ) ;
253
+ let success = self . inner . wait_timeout ( lock, Duration :: milliseconds ( ms as i64 ) ) ;
244
254
( mutex:: guard_poison ( & guard) . get ( ) , success)
245
255
} ;
246
256
if poisoned {
@@ -275,7 +285,8 @@ impl StaticCondvar {
275
285
let now = SteadyTime :: now ( ) ;
276
286
let consumed = & now - & start;
277
287
let guard = guard_result. unwrap_or_else ( |e| e. into_inner ( ) ) ;
278
- let ( new_guard_result, no_timeout) = match self . wait_timeout ( guard, dur - consumed) {
288
+ let res = self . wait_timeout_ms ( guard, ( dur - consumed) . num_milliseconds ( ) as u32 ) ;
289
+ let ( new_guard_result, no_timeout) = match res {
279
290
Ok ( ( new_guard, no_timeout) ) => ( Ok ( new_guard) , no_timeout) ,
280
291
Err ( err) => {
281
292
let ( new_guard, no_timeout) = err. into_inner ( ) ;
@@ -350,6 +361,7 @@ mod tests {
350
361
use sync:: atomic:: { AtomicUsize , ATOMIC_USIZE_INIT , Ordering } ;
351
362
use thread;
352
363
use time:: Duration ;
364
+ use u32;
353
365
354
366
#[ test]
355
367
fn smoke ( ) {
@@ -418,19 +430,19 @@ mod tests {
418
430
}
419
431
420
432
#[ test]
421
- fn wait_timeout ( ) {
433
+ fn wait_timeout_ms ( ) {
422
434
static C : StaticCondvar = CONDVAR_INIT ;
423
435
static M : StaticMutex = MUTEX_INIT ;
424
436
425
437
let g = M . lock ( ) . unwrap ( ) ;
426
- let ( g, _no_timeout) = C . wait_timeout ( g, Duration :: nanoseconds ( 1000 ) ) . unwrap ( ) ;
438
+ let ( g, _no_timeout) = C . wait_timeout_ms ( g, 1 ) . unwrap ( ) ;
427
439
// spurious wakeups mean this isn't necessarily true
428
440
// assert!(!no_timeout);
429
441
let _t = thread:: spawn ( move || {
430
442
let _g = M . lock ( ) . unwrap ( ) ;
431
443
C . notify_one ( ) ;
432
444
} ) ;
433
- let ( g, no_timeout) = C . wait_timeout ( g, Duration :: days ( 1 ) ) . unwrap ( ) ;
445
+ let ( g, no_timeout) = C . wait_timeout_ms ( g, u32 :: MAX ) . unwrap ( ) ;
434
446
assert ! ( no_timeout) ;
435
447
drop ( g) ;
436
448
unsafe { C . destroy ( ) ; M . destroy ( ) ; }
0 commit comments