@@ -209,7 +209,7 @@ pub use self::poison::{LockResult, PoisonError};
209
209
#[ doc( inline) ]
210
210
pub use self :: poison:: {
211
211
Mutex , MutexGuard , TryLockError , TryLockResult ,
212
- Condvar , WaitTimeoutResult ,
212
+ Condvar ,
213
213
Once , OnceState ,
214
214
RwLock , RwLockReadGuard , RwLockWriteGuard ,
215
215
} ;
@@ -234,3 +234,66 @@ mod barrier;
234
234
mod lazy_lock;
235
235
mod once_lock;
236
236
mod reentrant_lock;
237
+
238
+ /// A type indicating whether a timed wait on a condition variable returned
239
+ /// due to a time out or not.
240
+ ///
241
+ /// It is returned by the [`wait_timeout`] method.
242
+ ///
243
+ /// [`wait_timeout`]: Condvar::wait_timeout
244
+ #[ derive( Debug , PartialEq , Eq , Copy , Clone ) ]
245
+ #[ stable( feature = "wait_timeout" , since = "1.5.0" ) ]
246
+ pub struct WaitTimeoutResult ( bool ) ;
247
+
248
+ impl WaitTimeoutResult {
249
+ /// Returns `true` if the wait was known to have timed out.
250
+ ///
251
+ /// # Examples
252
+ ///
253
+ /// This example spawns a thread which will sleep 20 milliseconds before
254
+ /// updating a boolean value and then notifying the condvar.
255
+ ///
256
+ /// The main thread will wait with a 10 millisecond timeout on the condvar
257
+ /// and will leave the loop upon timeout.
258
+ ///
259
+ /// ```
260
+ /// use std::sync::{Arc, Condvar, Mutex};
261
+ /// use std::thread;
262
+ /// use std::time::Duration;
263
+ ///
264
+ /// let pair = Arc::new((Mutex::new(false), Condvar::new()));
265
+ /// let pair2 = Arc::clone(&pair);
266
+ ///
267
+ /// # let handle =
268
+ /// thread::spawn(move || {
269
+ /// let (lock, cvar) = &*pair2;
270
+ ///
271
+ /// // Let's wait 20 milliseconds before notifying the condvar.
272
+ /// thread::sleep(Duration::from_millis(20));
273
+ ///
274
+ /// let mut started = lock.lock().unwrap();
275
+ /// // We update the boolean value.
276
+ /// *started = true;
277
+ /// cvar.notify_one();
278
+ /// });
279
+ ///
280
+ /// // Wait for the thread to start up.
281
+ /// let (lock, cvar) = &*pair;
282
+ /// loop {
283
+ /// // Let's put a timeout on the condvar's wait.
284
+ /// let result = cvar.wait_timeout(lock.lock().unwrap(), Duration::from_millis(10)).unwrap();
285
+ /// // 10 milliseconds have passed.
286
+ /// if result.1.timed_out() {
287
+ /// // timed out now and we can leave.
288
+ /// break
289
+ /// }
290
+ /// }
291
+ /// # // Prevent leaks for Miri.
292
+ /// # let _ = handle.join();
293
+ /// ```
294
+ #[ must_use]
295
+ #[ stable( feature = "wait_timeout" , since = "1.5.0" ) ]
296
+ pub fn timed_out ( & self ) -> bool {
297
+ self . 0
298
+ }
299
+ }
0 commit comments