[PATCH] D27715: libcxx std::condition_variable::wait_for() fix for when tick occurs during wait_for()

Brian Cain via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Tue Dec 13 07:54:38 PST 2016

bcain created this revision.
bcain added reviewers: howard.hinnant, mclow.lists.
bcain added a subscriber: llvm-commits.
bcain set the repository for this revision to rL LLVM.
Herald added a reviewer: EricWF.

During std::condition_variable::wait_for(), we sample the system clock and then we sample the steady clock.

If a tick occurs after the system clock is sampled, but prior to when the steady clock is sampled, then the __do_timed_wait() may elapse the entire duration but the check at the end will fail and we'll return cv_status::no_timeout in error.  In the thread.condition.condvar/wait_for test, this causes the test to fail for my hexagon target.

§ 30.5.1 states “Returns: cv_status::timeout if the relative timeout (30.2.4) specified by rel_time expired, otherwise cv_status::no_timeout.”

This proposed fix would sample the steady clock first.  Now, if the tick elapses between the two the check would yield the same results as the underlying __do_timed_wait().




Index: include/__mutex_base
--- include/__mutex_base
+++ include/__mutex_base
@@ -410,8 +410,10 @@
     typedef time_point<system_clock, duration<long double, nano> > __sys_tpf;
     typedef time_point<system_clock, nanoseconds> __sys_tpi;
     __sys_tpf _Max = __sys_tpi::max();
-    system_clock::time_point __s_now = system_clock::now();
+    // Steady clock must be sampled prior to system clock
     steady_clock::time_point __c_now = steady_clock::now();
+    system_clock::time_point __s_now = system_clock::now();
     if (_Max - __d > __s_now)
         __do_timed_wait(__lk, __s_now + __ceil<nanoseconds>(__d));

-------------- next part --------------
A non-text attachment was scrubbed...
Name: D27715.81233.patch
Type: text/x-patch
Size: 701 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20161213/db583953/attachment.bin>

More information about the llvm-commits mailing list