1 //===----------------------------------------------------------------------===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 // UNSUPPORTED: no-threads, c++03
11 // <condition_variable>
13 // class condition_variable;
15 // template <class Clock, class Duration>
17 // wait_until(unique_lock<mutex>& lock,
18 // const chrono::time_point<Clock, Duration>& abs_time);
20 #include <condition_variable>
27 #include "make_test_thread.h"
28 #include "test_macros.h"
31 typedef std::chrono::milliseconds duration
;
32 typedef duration::rep rep
;
33 typedef duration::period period
;
34 typedef std::chrono::time_point
<TestClock
> time_point
;
35 static const bool is_steady
= true;
37 static time_point
now() {
38 using namespace std::chrono
;
39 return time_point(duration_cast
<duration
>(steady_clock::now().time_since_epoch()));
43 template <class Clock
>
45 // Test unblocking via a call to notify_one() in another thread.
47 // To test this, we set a very long timeout in wait_until() and we wait
48 // again in case we get awoken spuriously. Note that it can actually
49 // happen that we get awoken spuriously and fail to recognize it
50 // (making this test useless), but the likelihood should be small.
52 std::atomic
<bool> ready(false);
53 std::atomic
<bool> likely_spurious(true);
54 auto timeout
= Clock::now() + std::chrono::seconds(3600);
55 std::condition_variable cv
;
58 std::thread t1
= support::make_test_thread([&] {
59 std::unique_lock
<std::mutex
> lock(mutex
);
62 std::cv_status result
= cv
.wait_until(lock
, timeout
);
63 assert(result
== std::cv_status::no_timeout
);
64 } while (likely_spurious
);
66 // This can technically fail if we have many spurious awakenings, but in practice the
67 // tolerance is so high that it shouldn't be a problem.
68 assert(Clock::now() < timeout
);
71 std::thread t2
= support::make_test_thread([&] {
76 // Acquire the same mutex as t1. This blocks the condition variable inside its wait call
77 // so we can notify it while it is waiting.
78 std::unique_lock
<std::mutex
> lock(mutex
);
80 likely_spurious
= false;
88 // Test unblocking via a timeout.
90 // To test this, we create a thread that waits on a condition variable
91 // with a certain timeout, and we never awaken it. To guard against
92 // spurious wakeups, we wait again whenever we are awoken for a reason
93 // other than a timeout.
95 auto timeout
= Clock::now() + std::chrono::milliseconds(250);
96 std::condition_variable cv
;
99 std::thread t1
= support::make_test_thread([&] {
100 std::unique_lock
<std::mutex
> lock(mutex
);
101 std::cv_status result
;
103 result
= cv
.wait_until(lock
, timeout
);
104 if (result
== std::cv_status::timeout
)
105 assert(Clock::now() >= timeout
);
106 } while (result
!= std::cv_status::timeout
);
113 int main(int, char**) {
115 test
<std::chrono::steady_clock
>();