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