Run DCE after a LoopFlatten test to reduce spurious output [nfc]
[llvm-project.git] / libcxx / test / std / thread / thread.condition / thread.condition.condvarany / wait_until.pass.cpp
blob0f2334393d83313b3ab34a14d7f3a3b1fa782b3e
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
12 // <condition_variable>
14 // class condition_variable_any;
16 // template <class Lock, class Clock, class Duration>
17 // cv_status
18 // wait_until(Lock& lock, const chrono::time_point<Clock, Duration>& abs_time);
20 #include <condition_variable>
21 #include <mutex>
22 #include <thread>
23 #include <chrono>
24 #include <cassert>
26 #include "make_test_thread.h"
27 #include "test_macros.h"
29 struct Clock
31 typedef std::chrono::milliseconds duration;
32 typedef duration::rep rep;
33 typedef duration::period period;
34 typedef std::chrono::time_point<Clock> time_point;
35 static const bool is_steady = true;
37 static time_point now()
39 using namespace std::chrono;
40 return time_point(duration_cast<duration>(
41 steady_clock::now().time_since_epoch()
42 ));
46 std::condition_variable_any cv;
48 typedef std::timed_mutex L0;
49 typedef std::unique_lock<L0> L1;
51 L0 m0;
53 int test1 = 0;
54 int test2 = 0;
56 int runs = 0;
58 void f()
60 L1 lk(m0);
61 assert(test2 == 0);
62 test1 = 1;
63 cv.notify_one();
64 Clock::time_point t0 = Clock::now();
65 Clock::time_point t = t0 + Clock::duration(250);
66 while (test2 == 0 && cv.wait_until(lk, t) == std::cv_status::no_timeout)
68 Clock::time_point t1 = Clock::now();
69 if (runs == 0)
71 assert(t1 - t0 < Clock::duration(250));
72 assert(test2 != 0);
74 else
76 assert(t1 - t0 - Clock::duration(250) < Clock::duration(50));
77 assert(test2 == 0);
79 ++runs;
82 int main(int, char**)
85 L1 lk(m0);
86 std::thread t = support::make_test_thread(f);
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();
96 test1 = 0;
97 test2 = 0;
99 L1 lk(m0);
100 std::thread t = support::make_test_thread(f);
101 assert(test1 == 0);
102 while (test1 == 0)
103 cv.wait(lk);
104 assert(test1 != 0);
105 lk.unlock();
106 t.join();
109 return 0;