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 11 // <condition_variable> 12 13 // class condition_variable_any; 14 15 // template <class Lock, class Clock, class Duration> 16 // cv_status 17 // wait_until(Lock& lock, const chrono::time_point<Clock, Duration>& abs_time); 18 19 #include <condition_variable> 20 #include <mutex> 21 #include <thread> 22 #include <chrono> 23 #include <cassert> 24 25 struct Clock 26 { 27 typedef std::chrono::milliseconds duration; 28 typedef duration::rep rep; 29 typedef duration::period period; 30 typedef std::chrono::time_point<Clock> time_point; 31 static const bool is_steady = true; 32 33 static time_point now() 34 { 35 using namespace std::chrono; 36 return time_point(duration_cast<duration>( 37 steady_clock::now().time_since_epoch() 38 )); 39 } 40 }; 41 42 std::condition_variable_any cv; 43 44 typedef std::timed_mutex L0; 45 typedef std::unique_lock<L0> L1; 46 47 L0 m0; 48 49 int test1 = 0; 50 int test2 = 0; 51 52 int runs = 0; 53 54 void f() 55 { 56 L1 lk(m0); 57 assert(test2 == 0); 58 test1 = 1; 59 cv.notify_one(); 60 Clock::time_point t0 = Clock::now(); 61 Clock::time_point t = t0 + Clock::duration(250); 62 while (test2 == 0 && cv.wait_until(lk, t) == std::cv_status::no_timeout) 63 ; 64 Clock::time_point t1 = Clock::now(); 65 if (runs == 0) 66 { 67 assert(t1 - t0 < Clock::duration(250)); 68 assert(test2 != 0); 69 } 70 else 71 { 72 assert(t1 - t0 - Clock::duration(250) < Clock::duration(50)); 73 assert(test2 == 0); 74 } 75 ++runs; 76 } 77 78 int main(int, char**) 79 { 80 { 81 L1 lk(m0); 82 std::thread t(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(); 91 } 92 test1 = 0; 93 test2 = 0; 94 { 95 L1 lk(m0); 96 std::thread t(f); 97 assert(test1 == 0); 98 while (test1 == 0) 99 cv.wait(lk); 100 assert(test1 != 0); 101 lk.unlock(); 102 t.join(); 103 } 104 105 return 0; 106 } 107