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: libcpp-has-no-threads 10 // ALLOW_RETRIES: 2 11 12 // TODO(ldionne): This test fails on Ubuntu Focal on our CI nodes (and only there), in 32 bit mode. 13 // UNSUPPORTED: linux && 32bits-on-64bits 14 15 // <condition_variable> 16 17 // class condition_variable; 18 19 // template <class Rep, class Period, class Predicate> 20 // bool 21 // wait_for(unique_lock<mutex>& lock, 22 // const chrono::duration<Rep, Period>& rel_time, 23 // Predicate pred); 24 25 #include <condition_variable> 26 #include <mutex> 27 #include <thread> 28 #include <chrono> 29 #include <cassert> 30 31 #include "make_test_thread.h" 32 #include "test_macros.h" 33 34 class Pred 35 { 36 int& i_; 37 public: 38 explicit Pred(int& i) : i_(i) {} 39 40 bool operator()() {return i_ != 0;} 41 }; 42 43 std::condition_variable cv; 44 std::mutex mut; 45 46 int test1 = 0; 47 int test2 = 0; 48 49 int runs = 0; 50 51 void f() 52 { 53 typedef std::chrono::system_clock Clock; 54 typedef std::chrono::milliseconds milliseconds; 55 std::unique_lock<std::mutex> lk(mut); 56 assert(test2 == 0); 57 test1 = 1; 58 cv.notify_one(); 59 Clock::time_point t0 = Clock::now(); 60 bool r = cv.wait_for(lk, milliseconds(250), Pred(test2)); 61 ((void)r); // Prevent unused warning 62 Clock::time_point t1 = Clock::now(); 63 if (runs == 0) 64 { 65 assert(t1 - t0 < milliseconds(250)); 66 assert(test2 != 0); 67 } 68 else 69 { 70 assert(t1 - t0 - milliseconds(250) < milliseconds(50)); 71 assert(test2 == 0); 72 } 73 ++runs; 74 } 75 76 int main(int, char**) 77 { 78 { 79 std::unique_lock<std::mutex>lk(mut); 80 std::thread t = support::make_test_thread(f); 81 assert(test1 == 0); 82 while (test1 == 0) 83 cv.wait(lk); 84 assert(test1 != 0); 85 test2 = 1; 86 lk.unlock(); 87 cv.notify_one(); 88 t.join(); 89 } 90 test1 = 0; 91 test2 = 0; 92 { 93 std::unique_lock<std::mutex>lk(mut); 94 std::thread t = support::make_test_thread(f); 95 assert(test1 == 0); 96 while (test1 == 0) 97 cv.wait(lk); 98 assert(test1 != 0); 99 lk.unlock(); 100 t.join(); 101 } 102 103 return 0; 104 } 105