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: no-threads
10 // ALLOW_RETRIES: 2
11 
12 // <condition_variable>
13 
14 // class condition_variable_any;
15 
16 // template <class Lock, class Rep, class Period>
17 //   cv_status
18 //   wait_for(Lock& lock, const chrono::duration<Rep, Period>& rel_time);
19 
20 #include <condition_variable>
21 #include <mutex>
22 #include <thread>
23 #include <chrono>
24 #include <cassert>
25 
26 #include "make_test_thread.h"
27 #include "test_macros.h"
28 
29 std::condition_variable_any cv;
30 
31 typedef std::timed_mutex L0;
32 typedef std::unique_lock<L0> L1;
33 
34 L0 m0;
35 
36 int test1 = 0;
37 int test2 = 0;
38 
39 bool expect_timeout = false;
40 
f()41 void f()
42 {
43     typedef std::chrono::system_clock Clock;
44     typedef std::chrono::milliseconds milliseconds;
45     L1 lk(m0);
46     assert(test2 == 0);
47     test1 = 1;
48     cv.notify_one();
49     Clock::time_point t0 = Clock::now();
50     Clock::time_point wait_end = t0 + milliseconds(250);
51     Clock::duration d;
52     do {
53         d = wait_end - Clock::now();
54         if (d <= milliseconds(0)) break;
55     } while (test2 == 0 && cv.wait_for(lk, d) == std::cv_status::no_timeout);
56     Clock::time_point t1 = Clock::now();
57     if (!expect_timeout)
58     {
59         assert(t1 - t0 < milliseconds(250));
60         assert(test2 != 0);
61     }
62     else
63     {
64         assert(t1 - t0 - milliseconds(250) < milliseconds(50));
65         assert(test2 == 0);
66     }
67 }
68 
main(int,char **)69 int main(int, char**)
70 {
71     {
72         L1 lk(m0);
73         std::thread t = support::make_test_thread(f);
74         assert(test1 == 0);
75         while (test1 == 0)
76             cv.wait(lk);
77         assert(test1 != 0);
78         test2 = 1;
79         lk.unlock();
80         cv.notify_one();
81         t.join();
82     }
83     test1 = 0;
84     test2 = 0;
85     expect_timeout = true;
86     {
87         L1 lk(m0);
88         std::thread t = support::make_test_thread(f);
89         assert(test1 == 0);
90         while (test1 == 0)
91             cv.wait(lk);
92         assert(test1 != 0);
93         lk.unlock();
94         t.join();
95     }
96 
97   return 0;
98 }
99