[libcxx-commits] [PATCH] D99175: [libcxx] [test] Make the condvar wait_for tests less brittle

Martin Storsjö via Phabricator via libcxx-commits libcxx-commits at lists.llvm.org
Tue Mar 23 05:03:24 PDT 2021


mstorsjo created this revision.
mstorsjo requested review of this revision.
Herald added a project: libc++.
Herald added a reviewer: libc++.

These seem to fail occasionally (they are marked as possibly requiring
a retry).

When doing a condvar wait_for(), it can wake up before the timeout
as a spurious wakeup. In these cases, the wait_for() method returns that
the timeout wasn't hit, and the test reruns another wait_for().

On Windows, it seems like the wait_for() operation often can end up
returning slightly before the intended deadline - when intending to
wait for 250 milliseconds, it can return after e.g. 235 milliseconds.
In these cases, the wait_for() doesn't indicate a timeout.

Previously, the test then reran a new wait_for() for a full 250
milliseconds each time. So for N consecutive wakeups slightly too early,
we'd wait for (N+1)*250 milliseconds. Now it only reruns wait_for() for
the remaining intended wait duration.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D99175

Files:
  libcxx/test/std/thread/thread.condition/thread.condition.condvar/wait_for.pass.cpp
  libcxx/test/std/thread/thread.condition/thread.condition.condvarany/wait_for.pass.cpp


Index: libcxx/test/std/thread/thread.condition/thread.condition.condvarany/wait_for.pass.cpp
===================================================================
--- libcxx/test/std/thread/thread.condition/thread.condition.condvarany/wait_for.pass.cpp
+++ libcxx/test/std/thread/thread.condition/thread.condition.condvarany/wait_for.pass.cpp
@@ -47,8 +47,9 @@
     test1 = 1;
     cv.notify_one();
     Clock::time_point t0 = Clock::now();
+    Clock::time_point wait_end = t0 + milliseconds(250);
     while (test2 == 0 &&
-           cv.wait_for(lk, milliseconds(250)) == std::cv_status::no_timeout)
+           cv.wait_for(lk, wait_end - Clock::now()) == std::cv_status::no_timeout)
         ;
     Clock::time_point t1 = Clock::now();
     if (runs == 0)
Index: libcxx/test/std/thread/thread.condition/thread.condition.condvar/wait_for.pass.cpp
===================================================================
--- libcxx/test/std/thread/thread.condition/thread.condition.condvar/wait_for.pass.cpp
+++ libcxx/test/std/thread/thread.condition/thread.condition.condvar/wait_for.pass.cpp
@@ -44,8 +44,9 @@
     test1 = 1;
     cv.notify_one();
     Clock::time_point t0 = Clock::now();
+    Clock::time_point wait_end = t0 + milliseconds(250);
     while (test2 == 0 &&
-           cv.wait_for(lk, milliseconds(250)) == std::cv_status::no_timeout)
+           cv.wait_for(lk, wait_end - Clock::now()) == std::cv_status::no_timeout)
         ;
     Clock::time_point t1 = Clock::now();
     if (runs == 0)


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D99175.332624.patch
Type: text/x-patch
Size: 1507 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/libcxx-commits/attachments/20210323/cf08a5b1/attachment-0001.bin>


More information about the libcxx-commits mailing list