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 Duration, class Predicate>
20 //     bool
21 //     wait_until(Lock& lock,
22 //                const chrono::time_point<Clock, Duration>& abs_time,
23 //                Predicate pred);
24 
25 #include <condition_variable>
26 #include <mutex>
27 #include <thread>
28 #include <chrono>
29 #include <cassert>
30 
31 #include "make_test_thread.h"
32 #include "test_macros.h"
33 
34 struct Clock
35 {
36     typedef std::chrono::milliseconds duration;
37     typedef duration::rep             rep;
38     typedef duration::period          period;
39     typedef std::chrono::time_point<Clock> time_point;
40     static const bool is_steady =  true;
41 
42     static time_point now()
43     {
44         using namespace std::chrono;
45         return time_point(duration_cast<duration>(
46                 steady_clock::now().time_since_epoch()
47                                                  ));
48     }
49 };
50 
51 class Pred
52 {
53     int& i_;
54 public:
55     explicit Pred(int& i) : i_(i) {}
56 
57     bool operator()() {return i_ != 0;}
58 };
59 
60 std::condition_variable_any cv;
61 
62 typedef std::timed_mutex L0;
63 typedef std::unique_lock<L0> L1;
64 
65 L0 m0;
66 
67 int test1 = 0;
68 int test2 = 0;
69 
70 int runs = 0;
71 
72 void f()
73 {
74     L1 lk(m0);
75     assert(test2 == 0);
76     test1 = 1;
77     cv.notify_one();
78     Clock::time_point t0 = Clock::now();
79     Clock::time_point t = t0 + Clock::duration(250);
80     bool r = cv.wait_until(lk, t, Pred(test2));
81     Clock::time_point t1 = Clock::now();
82     if (runs == 0)
83     {
84         assert(t1 - t0 < Clock::duration(250));
85         assert(test2 != 0);
86         assert(r);
87     }
88     else
89     {
90         assert(t1 - t0 - Clock::duration(250) < Clock::duration(50));
91         assert(test2 == 0);
92         assert(!r);
93     }
94     ++runs;
95 }
96 
97 int main(int, char**)
98 {
99     {
100         L1 lk(m0);
101         std::thread t = support::make_test_thread(f);
102         assert(test1 == 0);
103         while (test1 == 0)
104             cv.wait(lk);
105         assert(test1 != 0);
106         test2 = 1;
107         lk.unlock();
108         cv.notify_one();
109         t.join();
110     }
111     test1 = 0;
112     test2 = 0;
113     {
114         L1 lk(m0);
115         std::thread t = support::make_test_thread(f);
116         assert(test1 == 0);
117         while (test1 == 0)
118             cv.wait(lk);
119         assert(test1 != 0);
120         lk.unlock();
121         t.join();
122     }
123 
124   return 0;
125 }
126