Run DCE after a LoopFlatten test to reduce spurious output [nfc]
[llvm-project.git] / libcxx / test / std / thread / thread.condition / thread.condition.condvarany / wait_for.pass.cpp
blob95acef90470ec39b3135591c28408fc69ef1a66f
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 Rep, class Period>
17 // cv_status
18 // wait_for(Lock& lock, const chrono::duration<Rep, Period>& rel_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 std::condition_variable_any cv;
31 typedef std::timed_mutex L0;
32 typedef std::unique_lock<L0> L1;
34 L0 m0;
36 int test1 = 0;
37 int test2 = 0;
39 bool expect_timeout = false;
41 void f()
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)
59 assert(t1 - t0 < milliseconds(250));
60 assert(test2 != 0);
62 else
64 assert(t1 - t0 - milliseconds(250) < milliseconds(50));
65 assert(test2 == 0);
69 int main(int, char**)
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();
83 test1 = 0;
84 test2 = 0;
85 expect_timeout = true;
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();
97 return 0;