From 8ec47261e193b1f983b898a0d985b85e7fcb0668 Mon Sep 17 00:00:00 2001 From: Tomasz Miąsko Date: Tue, 26 Jul 2016 15:18:44 +0200 Subject: Use monotonic time with condition variables. Configure condition variables to use monotonic time using pthread_condattr_setclock on systems where this is possible. This fixes the issue when thread waiting on condition variable is woken up too late when system time is moved backwards. --- src/libstd/sync/condvar.rs | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) (limited to 'src/libstd/sync') diff --git a/src/libstd/sync/condvar.rs b/src/libstd/sync/condvar.rs index 3c52ebc72f2..ca1a9905ebe 100644 --- a/src/libstd/sync/condvar.rs +++ b/src/libstd/sync/condvar.rs @@ -82,10 +82,14 @@ impl Condvar { /// notified. #[stable(feature = "rust1", since = "1.0.0")] pub fn new() -> Condvar { - Condvar { + let mut c = Condvar { inner: box sys::Condvar::new(), mutex: AtomicUsize::new(0), + }; + unsafe { + c.inner.init(); } + c } /// Blocks the current thread until this condition variable receives a -- cgit 1.4.1-3-g733a5 From 8dae1b662588ed18017a50c2cbc5c6b2880ae27c Mon Sep 17 00:00:00 2001 From: Tomasz Miąsko Date: Fri, 19 Aug 2016 14:00:20 +0200 Subject: Document that Condvar makes the best effort to use monotonic clock. --- src/libstd/sync/condvar.rs | 8 ++++++++ 1 file changed, 8 insertions(+) (limited to 'src/libstd/sync') diff --git a/src/libstd/sync/condvar.rs b/src/libstd/sync/condvar.rs index ca1a9905ebe..5ee42a09e12 100644 --- a/src/libstd/sync/condvar.rs +++ b/src/libstd/sync/condvar.rs @@ -144,6 +144,10 @@ impl Condvar { /// differences that may not cause the maximum amount of time /// waited to be precisely `ms`. /// + /// Note that the best effort is made to ensure that the time waited is + /// measured with a monotonic clock, and not affected by the changes made to + /// the system time. + /// /// The returned boolean is `false` only if the timeout is known /// to have elapsed. /// @@ -168,6 +172,10 @@ impl Condvar { /// preemption or platform differences that may not cause the maximum /// amount of time waited to be precisely `dur`. /// + /// Note that the best effort is made to ensure that the time waited is + /// measured with a monotonic clock, and not affected by the changes made to + /// the system time. + /// /// The returned `WaitTimeoutResult` value indicates if the timeout is /// known to have elapsed. /// -- cgit 1.4.1-3-g733a5