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_any;
15 // template <class Lock, class Rep, class Period>
17 // wait_for(Lock& lock, const chrono::duration<Rep, Period>& rel_time);
19 #include <condition_variable>
26 #include "make_test_thread.h"
27 #include "test_macros.h"
29 template <class Mutex
>
30 struct MyLock
: std::unique_lock
<Mutex
> {
31 using std::unique_lock
<Mutex
>::unique_lock
;
34 template <class Function
>
35 std::chrono::microseconds
measure(Function f
) {
36 std::chrono::high_resolution_clock::time_point start
= std::chrono::high_resolution_clock::now();
38 std::chrono::high_resolution_clock::time_point end
= std::chrono::high_resolution_clock::now();
39 return std::chrono::duration_cast
<std::chrono::microseconds
>(end
- start
);
44 using Mutex
= typename
Lock::mutex_type
;
45 // Test unblocking via a call to notify_one() in another thread.
47 // To test this, we set a very long timeout in wait_for() 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
= std::chrono::seconds(3600);
55 std::condition_variable_any cv
;
58 std::thread t1
= support::make_test_thread([&] {
60 auto elapsed
= measure([&] {
63 std::cv_status result
= cv
.wait_for(lock
, timeout
);
64 assert(result
== std::cv_status::no_timeout
);
65 } while (likely_spurious
);
68 // This can technically fail if we have many spurious awakenings, but in practice the
69 // tolerance is so high that it shouldn't be a problem.
70 assert(elapsed
< timeout
);
73 std::thread t2
= support::make_test_thread([&] {
78 // Acquire the same mutex as t1. This blocks the condition variable inside its wait call
79 // so we can notify it while it is waiting.
82 likely_spurious
= false;
90 // Test unblocking via a timeout.
92 // To test this, we create a thread that waits on a condition variable
93 // with a certain timeout, and we never awaken it. To guard against
94 // spurious wakeups, we wait again whenever we are awoken for a reason
95 // other than a timeout.
97 auto timeout
= std::chrono::milliseconds(250);
98 std::condition_variable_any cv
;
101 std::thread t1
= support::make_test_thread([&] {
103 std::cv_status result
;
105 auto elapsed
= measure([&] { result
= cv
.wait_for(lock
, timeout
); });
106 if (result
== std::cv_status::timeout
)
107 assert(elapsed
>= timeout
);
108 } while (result
!= std::cv_status::timeout
);
115 int main(int, char**) {
116 test
<std::unique_lock
<std::mutex
>>();
117 test
<std::unique_lock
<std::timed_mutex
>>();
118 test
<MyLock
<std::mutex
>>();
119 test
<MyLock
<std::timed_mutex
>>();