1 //===----------------------------------------------------------------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is dual licensed under the MIT and the University of Illinois Open
6 // Source Licenses. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // UNSUPPORTED: libcpp-has-no-threads
11 
12 // <condition_variable>
13 
14 // class condition_variable_any;
15 
16 // template <class Lock, class Clock, class Duration>
17 //   cv_status
18 //   wait_until(Lock& lock, 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 struct Clock
27 {
28     typedef std::chrono::milliseconds duration;
29     typedef duration::rep             rep;
30     typedef duration::period          period;
31     typedef std::chrono::time_point<Clock> time_point;
32     static const bool is_steady =  true;
33 
nowClock34     static time_point now()
35     {
36         using namespace std::chrono;
37         return time_point(duration_cast<duration>(
38                 steady_clock::now().time_since_epoch()
39                                                  ));
40     }
41 };
42 
43 std::condition_variable_any cv;
44 
45 typedef std::timed_mutex L0;
46 typedef std::unique_lock<L0> L1;
47 
48 L0 m0;
49 
50 int test1 = 0;
51 int test2 = 0;
52 
53 int runs = 0;
54 
f()55 void f()
56 {
57     L1 lk(m0);
58     assert(test2 == 0);
59     test1 = 1;
60     cv.notify_one();
61     Clock::time_point t0 = Clock::now();
62     Clock::time_point t = t0 + Clock::duration(250);
63     while (test2 == 0 && cv.wait_until(lk, t) == std::cv_status::no_timeout)
64         ;
65     Clock::time_point t1 = Clock::now();
66     if (runs == 0)
67     {
68         assert(t1 - t0 < Clock::duration(250));
69         assert(test2 != 0);
70     }
71     else
72     {
73         assert(t1 - t0 - Clock::duration(250) < Clock::duration(50));
74         assert(test2 == 0);
75     }
76     ++runs;
77 }
78 
main()79 int main()
80 {
81     {
82         L1 lk(m0);
83         std::thread t(f);
84         assert(test1 == 0);
85         while (test1 == 0)
86             cv.wait(lk);
87         assert(test1 != 0);
88         test2 = 1;
89         lk.unlock();
90         cv.notify_one();
91         t.join();
92     }
93     test1 = 0;
94     test2 = 0;
95     {
96         L1 lk(m0);
97         std::thread t(f);
98         assert(test1 == 0);
99         while (test1 == 0)
100             cv.wait(lk);
101         assert(test1 != 0);
102         lk.unlock();
103         t.join();
104     }
105 }
106