[RISCV] Fix mgather -> riscv.masked.strided.load combine not extending indices (...
[llvm-project.git] / libcxx / test / std / thread / thread.condition / thread.condition.condvar / wait_for_pred.pass.cpp
blob872bcb6d8a57dbd88c097554949f9e4099f71d64
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;
16 // template <class Rep, class Period, class Predicate>
17 // bool
18 // wait_for(unique_lock<mutex>& lock,
19 // const chrono::duration<Rep, Period>& rel_time,
20 // Predicate pred);
22 #include <condition_variable>
23 #include <mutex>
24 #include <thread>
25 #include <chrono>
26 #include <cassert>
28 #include "make_test_thread.h"
29 #include "test_macros.h"
31 class Pred
33 int& i_;
34 public:
35 explicit Pred(int& i) : i_(i) {}
37 bool operator()() {return i_ != 0;}
40 std::condition_variable cv;
41 std::mutex mut;
43 int test1 = 0;
44 int test2 = 0;
46 int runs = 0;
48 void f()
50 typedef std::chrono::system_clock Clock;
51 typedef std::chrono::milliseconds milliseconds;
52 std::unique_lock<std::mutex> lk(mut);
53 assert(test2 == 0);
54 test1 = 1;
55 cv.notify_one();
56 Clock::time_point t0 = Clock::now();
57 bool r = cv.wait_for(lk, milliseconds(250), Pred(test2));
58 ((void)r); // Prevent unused warning
59 Clock::time_point t1 = Clock::now();
60 if (runs == 0)
62 assert(t1 - t0 < milliseconds(250));
63 assert(test2 != 0);
65 else
67 assert(t1 - t0 - milliseconds(250) < milliseconds(50));
68 assert(test2 == 0);
70 ++runs;
73 int main(int, char**)
76 std::unique_lock<std::mutex>lk(mut);
77 std::thread t = support::make_test_thread(f);
78 assert(test1 == 0);
79 while (test1 == 0)
80 cv.wait(lk);
81 assert(test1 != 0);
82 test2 = 1;
83 lk.unlock();
84 cv.notify_one();
85 t.join();
87 test1 = 0;
88 test2 = 0;
90 std::unique_lock<std::mutex>lk(mut);
91 std::thread t = support::make_test_thread(f);
92 assert(test1 == 0);
93 while (test1 == 0)
94 cv.wait(lk);
95 assert(test1 != 0);
96 lk.unlock();
97 t.join();
100 return 0;