[RISCV] Fix mgather -> riscv.masked.strided.load combine not extending indices (...
[llvm-project.git] / libcxx / test / std / thread / thread.condition / thread.condition.condvarany / wait_for_pred.pass.cpp
blob0b560022bc67c944f8c25a9f055f7c6cf5ffc35b
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, class Predicate>
17 // bool
18 // wait_for(Lock& lock, const chrono::duration<Rep, Period>& rel_time,
19 // Predicate pred);
21 #include <condition_variable>
22 #include <mutex>
23 #include <thread>
24 #include <chrono>
25 #include <cassert>
27 #include "make_test_thread.h"
28 #include "test_macros.h"
30 class Pred
32 int& i_;
33 public:
34 explicit Pred(int& i) : i_(i) {}
36 bool operator()() {return i_ != 0;}
39 std::condition_variable_any cv;
41 typedef std::timed_mutex L0;
42 typedef std::unique_lock<L0> L1;
44 L0 m0;
46 int test1 = 0;
47 int test2 = 0;
49 int runs = 0;
50 bool expect_result = false;
52 void f()
54 typedef std::chrono::system_clock Clock;
55 typedef std::chrono::milliseconds milliseconds;
56 L1 lk(m0);
57 assert(test2 == 0);
58 test1 = 1;
59 cv.notify_one();
60 Clock::time_point t0 = Clock::now();
61 bool result = cv.wait_for(lk, milliseconds(250), Pred(test2));
62 assert(result == expect_result);
63 Clock::time_point t1 = Clock::now();
64 if (runs == 0)
66 assert(t1 - t0 < milliseconds(250));
67 assert(test2 != 0);
69 else
71 assert(t1 - t0 - milliseconds(250) < milliseconds(50));
72 assert(test2 == 0);
74 ++runs;
77 int main(int, char**)
80 expect_result = true;
81 L1 lk(m0);
82 std::thread t = support::make_test_thread(f);
83 assert(test1 == 0);
84 while (test1 == 0)
85 cv.wait(lk);
86 assert(test1 != 0);
87 test2 = 1;
88 lk.unlock();
89 cv.notify_one();
90 t.join();
92 test1 = 0;
93 test2 = 0;
95 expect_result = false;
96 L1 lk(m0);
97 std::thread t = support::make_test_thread(f);
98 assert(test1 == 0);
99 while (test1 == 0)
100 cv.wait(lk);
101 assert(test1 != 0);
102 lk.unlock();
103 t.join();
106 return 0;