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 
11 // <condition_variable>
12 
13 // class condition_variable;
14 
15 // template <class Clock, class Duration>
16 //   cv_status
17 //   wait_until(unique_lock<mutex>& lock,
18 //              const chrono::time_point<Clock, Duration>& abs_time);
19 
20 #include <condition_variable>
21 #include <mutex>
22 #include <thread>
23 #include <chrono>
24 #include <cassert>
25 
26 #include "test_macros.h"
27 
28 struct TestClock
29 {
30     typedef std::chrono::milliseconds duration;
31     typedef duration::rep             rep;
32     typedef duration::period          period;
33     typedef std::chrono::time_point<TestClock> time_point;
34     static const bool is_steady =  true;
35 
nowTestClock36     static time_point now()
37     {
38         using namespace std::chrono;
39         return time_point(duration_cast<duration>(
40                 steady_clock::now().time_since_epoch()
41                                                  ));
42     }
43 };
44 
45 std::condition_variable cv;
46 std::mutex mut;
47 
48 int test1 = 0;
49 int test2 = 0;
50 
51 int runs = 0;
52 
53 template <typename Clock>
f()54 void f()
55 {
56     std::unique_lock<std::mutex> lk(mut);
57     assert(test2 == 0);
58     test1 = 1;
59     cv.notify_one();
60     typename Clock::time_point t0 = Clock::now();
61     typename Clock::time_point t = t0 + std::chrono::milliseconds(250);
62     while (test2 == 0 && cv.wait_until(lk, t) == std::cv_status::no_timeout)
63         ;
64     typename Clock::time_point t1 = Clock::now();
65     if (runs == 0)
66     {
67         assert(t1 - t0 < std::chrono::milliseconds(250));
68         assert(test2 != 0);
69     }
70     else
71     {
72         assert(t1 - t0 - std::chrono::milliseconds(250) < std::chrono::milliseconds(50));
73         assert(test2 == 0);
74     }
75     ++runs;
76 }
77 
78 template <typename Clock>
run_test()79 void run_test()
80 {
81     runs = 0;
82     test1 = 0;
83     test2 = 0;
84     {
85         std::unique_lock<std::mutex>lk(mut);
86         std::thread t(f<Clock>);
87         assert(test1 == 0);
88         while (test1 == 0)
89             cv.wait(lk);
90         assert(test1 != 0);
91         test2 = 1;
92         lk.unlock();
93         cv.notify_one();
94         t.join();
95     }
96     test1 = 0;
97     test2 = 0;
98     {
99         std::unique_lock<std::mutex>lk(mut);
100         std::thread t(f<Clock>);
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 
main(int,char **)110 int main(int, char**)
111 {
112     run_test<TestClock>();
113     run_test<std::chrono::steady_clock>();
114     run_test<std::chrono::system_clock>();
115     return 0;
116 }
117