1 //===----------------------------------------------------------------------===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is dual licensed under the MIT and the University of Illinois Open 6 // Source Licenses. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 // 10 // UNSUPPORTED: libcpp-has-no-threads 11 12 // <condition_variable> 13 14 // class condition_variable_any; 15 16 // template <class Lock, class Duration, class Predicate> 17 // bool 18 // wait_until(Lock& lock, 19 // const chrono::time_point<Clock, Duration>& abs_time, 20 // Predicate pred); 21 22 #include <condition_variable> 23 #include <mutex> 24 #include <thread> 25 #include <chrono> 26 #include <cassert> 27 28 struct Clock 29 { 30 typedef std::chrono::milliseconds duration; 31 typedef duration::rep rep; 32 typedef duration::period period; 33 typedef std::chrono::time_point<Clock> time_point; 34 static const bool is_steady = true; 35 nowClock36 static time_point now() 37 { 38 using namespace std::chrono; 39 return time_point(duration_cast<duration>( 40 steady_clock::now().time_since_epoch() 41 )); 42 } 43 }; 44 45 class Pred 46 { 47 int& i_; 48 public: Pred(int & i)49 explicit Pred(int& i) : i_(i) {} 50 operator ()()51 bool operator()() {return i_ != 0;} 52 }; 53 54 std::condition_variable_any cv; 55 56 typedef std::timed_mutex L0; 57 typedef std::unique_lock<L0> L1; 58 59 L0 m0; 60 61 int test1 = 0; 62 int test2 = 0; 63 64 int runs = 0; 65 f()66void f() 67 { 68 L1 lk(m0); 69 assert(test2 == 0); 70 test1 = 1; 71 cv.notify_one(); 72 Clock::time_point t0 = Clock::now(); 73 Clock::time_point t = t0 + Clock::duration(250); 74 bool r = cv.wait_until(lk, t, Pred(test2)); 75 Clock::time_point t1 = Clock::now(); 76 if (runs == 0) 77 { 78 assert(t1 - t0 < Clock::duration(250)); 79 assert(test2 != 0); 80 assert(r); 81 } 82 else 83 { 84 assert(t1 - t0 - Clock::duration(250) < Clock::duration(50)); 85 assert(test2 == 0); 86 assert(!r); 87 } 88 ++runs; 89 } 90 main()91int main() 92 { 93 { 94 L1 lk(m0); 95 std::thread t(f); 96 assert(test1 == 0); 97 while (test1 == 0) 98 cv.wait(lk); 99 assert(test1 != 0); 100 test2 = 1; 101 lk.unlock(); 102 cv.notify_one(); 103 t.join(); 104 } 105 test1 = 0; 106 test2 = 0; 107 { 108 L1 lk(m0); 109 std::thread t(f); 110 assert(test1 == 0); 111 while (test1 == 0) 112 cv.wait(lk); 113 assert(test1 != 0); 114 lk.unlock(); 115 t.join(); 116 } 117 } 118