1 | //===----------------------------------------------------------------------===// |
2 | // |
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 |
6 | // |
7 | //===----------------------------------------------------------------------===// |
8 | // |
9 | // UNSUPPORTED: no-threads |
10 | // ALLOW_RETRIES: 2 |
11 | |
12 | // <condition_variable> |
13 | |
14 | // class condition_variable_any; |
15 | |
16 | // template <class Lock, class Rep, class Period, class Predicate> |
17 | // bool |
18 | // wait_for(Lock& lock, const chrono::duration<Rep, Period>& rel_time, |
19 | // Predicate pred); |
20 | |
21 | #include <condition_variable> |
22 | #include <mutex> |
23 | #include <thread> |
24 | #include <chrono> |
25 | #include <cassert> |
26 | |
27 | #include "make_test_thread.h" |
28 | #include "test_macros.h" |
29 | |
30 | class Pred |
31 | { |
32 | int& i_; |
33 | public: |
34 | explicit Pred(int& i) : i_(i) {} |
35 | |
36 | bool operator()() {return i_ != 0;} |
37 | }; |
38 | |
39 | std::condition_variable_any cv; |
40 | |
41 | typedef std::timed_mutex L0; |
42 | typedef std::unique_lock<L0> L1; |
43 | |
44 | L0 m0; |
45 | |
46 | int test1 = 0; |
47 | int test2 = 0; |
48 | |
49 | int runs = 0; |
50 | bool expect_result = false; |
51 | |
52 | void f() |
53 | { |
54 | typedef std::chrono::system_clock Clock; |
55 | typedef std::chrono::milliseconds milliseconds; |
56 | L1 lk(m0); |
57 | assert(test2 == 0); |
58 | test1 = 1; |
59 | cv.notify_one(); |
60 | Clock::time_point t0 = Clock::now(); |
61 | bool result = cv.wait_for(lock&: lk, rtime: milliseconds(250), p: Pred(test2)); |
62 | assert(result == expect_result); |
63 | Clock::time_point t1 = Clock::now(); |
64 | if (runs == 0) |
65 | { |
66 | assert(t1 - t0 < milliseconds(250)); |
67 | assert(test2 != 0); |
68 | } |
69 | else |
70 | { |
71 | assert(t1 - t0 - milliseconds(250) < milliseconds(50)); |
72 | assert(test2 == 0); |
73 | } |
74 | ++runs; |
75 | } |
76 | |
77 | int main(int, char**) |
78 | { |
79 | { |
80 | expect_result = true; |
81 | L1 lk(m0); |
82 | std::thread t = support::make_test_thread(f); |
83 | assert(test1 == 0); |
84 | while (test1 == 0) |
85 | cv.wait(lock&: lk); |
86 | assert(test1 != 0); |
87 | test2 = 1; |
88 | lk.unlock(); |
89 | cv.notify_one(); |
90 | t.join(); |
91 | } |
92 | test1 = 0; |
93 | test2 = 0; |
94 | { |
95 | expect_result = false; |
96 | L1 lk(m0); |
97 | std::thread t = support::make_test_thread(f); |
98 | assert(test1 == 0); |
99 | while (test1 == 0) |
100 | cv.wait(lock&: lk); |
101 | assert(test1 != 0); |
102 | lk.unlock(); |
103 | t.join(); |
104 | } |
105 | |
106 | return 0; |
107 | } |
108 | |