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