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