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;
15 
16 // template <class Clock, class Duration, class Predicate>
17 //     bool
18 //     wait_until(unique_lock<mutex>& lock,
19 //                const chrono::time_point<Clock, Duration>& abs_time,
20 //                Predicate pred);
21 
22 #include <condition_variable>
23 #include <mutex>
24 #include <thread>
25 #include <chrono>
26 #include <cassert>
27 
28 struct Clock
29 {
30     typedef std::chrono::milliseconds duration;
31     typedef duration::rep             rep;
32     typedef duration::period          period;
33     typedef std::chrono::time_point<Clock> time_point;
34     static const bool is_steady =  true;
35 
nowClock36     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 class Pred
46 {
47     int& i_;
48 public:
Pred(int & i)49     explicit Pred(int& i) : i_(i) {}
50 
operator ()()51     bool operator()() {return i_ != 0;}
52 };
53 
54 std::condition_variable cv;
55 std::mutex mut;
56 
57 int test1 = 0;
58 int test2 = 0;
59 
60 int runs = 0;
61 
f()62 void f()
63 {
64     std::unique_lock<std::mutex> lk(mut);
65     assert(test2 == 0);
66     test1 = 1;
67     cv.notify_one();
68     Clock::time_point t0 = Clock::now();
69     Clock::time_point t = t0 + Clock::duration(250);
70     bool r = cv.wait_until(lk, t, Pred(test2));
71     Clock::time_point t1 = Clock::now();
72     if (runs == 0)
73     {
74         assert(t1 - t0 < Clock::duration(250));
75         assert(test2 != 0);
76         assert(r);
77     }
78     else
79     {
80         assert(t1 - t0 - Clock::duration(250) < Clock::duration(50));
81         assert(test2 == 0);
82         assert(!r);
83     }
84     ++runs;
85 }
86 
main()87 int main()
88 {
89     {
90         std::unique_lock<std::mutex>lk(mut);
91         std::thread t(f);
92         assert(test1 == 0);
93         while (test1 == 0)
94             cv.wait(lk);
95         assert(test1 != 0);
96         test2 = 1;
97         lk.unlock();
98         cv.notify_one();
99         t.join();
100     }
101     test1 = 0;
102     test2 = 0;
103     {
104         std::unique_lock<std::mutex>lk(mut);
105         std::thread t(f);
106         assert(test1 == 0);
107         while (test1 == 0)
108             cv.wait(lk);
109         assert(test1 != 0);
110         lk.unlock();
111         t.join();
112     }
113 }
114