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
10 // UNSUPPORTED: c++03, c++11, c++14, c++17
11 // UNSUPPORTED: libcpp-has-no-experimental-stop_token
12 // XFAIL: availability-synchronization_library-missing
14 // <condition_variable>
16 // class condition_variable_any;
18 // template<class Lock, class Rep, class Period, class Predicate>
19 // bool wait_for(Lock& lock, stop_token stoken,
20 // const chrono::duration<Rep, Period>& rel_time, Predicate pred);
25 #include <condition_variable>
28 #include <shared_mutex>
32 #include "make_test_thread.h"
33 #include "test_macros.h"
35 template <class Mutex
, class Lock
>
37 using namespace std::chrono_literals
;
39 // stop_requested before hand
42 std::condition_variable_any cv
;
47 // [Note 4: The returned value indicates whether the predicate evaluated to true
48 // regardless of whether the timeout was triggered or a stop request was made.]
49 std::same_as
<bool> auto r1
= cv
.wait_for(lock
, ss
.get_token(), -1h
, []() { return false; });
52 std::same_as
<bool> auto r2
= cv
.wait_for(lock
, ss
.get_token(), 1h
, []() { return false; });
55 std::same_as
<bool> auto r3
= cv
.wait_for(lock
, ss
.get_token(), -1h
, []() { return true; });
58 std::same_as
<bool> auto r4
= cv
.wait_for(lock
, ss
.get_token(), 1h
, []() { return true; });
61 // Postconditions: lock is locked by the calling thread.
62 assert(lock
.owns_lock());
65 // no stop request, pred was true
68 std::condition_variable_any cv
;
72 std::same_as
<bool> auto r1
= cv
.wait_for(lock
, ss
.get_token(), -1h
, []() { return true; });
75 std::same_as
<bool> auto r2
= cv
.wait_for(lock
, ss
.get_token(), 1h
, []() { return true; });
79 // no stop request, pred was false, abs_time was in the past
82 std::condition_variable_any cv
;
86 std::same_as
<bool> auto r1
= cv
.wait_for(lock
, ss
.get_token(), -1h
, []() { return false; });
90 // no stop request, pred was false until timeout
93 std::condition_variable_any cv
;
97 auto old_time
= std::chrono::steady_clock::now();
99 std::same_as
<bool> auto r1
= cv
.wait_for(lock
, ss
.get_token(), 2ms
, [&]() { return false; });
101 assert((std::chrono::steady_clock::now() - old_time
) >= 2ms
);
105 // no stop request, pred was false, changed to true before timeout
108 std::condition_variable_any cv
;
113 auto thread
= support::make_test_thread([&]() {
114 std::this_thread::sleep_for(2ms
);
120 std::same_as
<bool> auto r1
= cv
.wait_for(lock
, ss
.get_token(), 1h
, [&]() { return flag
; });
127 // stop request comes while waiting
130 std::condition_variable_any cv
;
134 std::atomic_bool start
= false;
135 std::atomic_bool done
= false;
136 auto thread
= support::make_test_thread([&]() {
142 std::this_thread::sleep_for(2ms
);
146 std::same_as
<bool> auto r
= cv
.wait_for(lock
, ss
.get_token(), 1h
, [&]() {
155 assert(lock
.owns_lock());
158 #if !defined(TEST_HAS_NO_EXCEPTIONS)
159 // Throws: Any exception thrown by pred.
162 std::condition_variable_any cv
;
167 cv
.wait_for(lock
, ss
.get_token(), 1h
, []() -> bool { throw 5; });
173 #endif //!defined(TEST_HAS_NO_EXCEPTIONS)
176 int main(int, char**) {
177 test
<std::mutex
, std::unique_lock
<std::mutex
>>();
178 test
<std::shared_mutex
, std::shared_lock
<std::shared_mutex
>>();