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 // Copyright (C) 2011 Vicente J. Botet Escriba 10 // 11 // Distributed under the Boost Software License, Version 1.0. (See accompanying 12 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) 13 14 // <boost/thread/condition_variable_any> 15 16 // class condition_variable_any; 17 18 // condition_variable_any(const condition_variable_any&) = delete; 19 20 #include <boost/thread/condition_variable.hpp> 21 #include <boost/thread/mutex.hpp> 22 #include <boost/thread/thread.hpp> 23 #include <boost/detail/lightweight_test.hpp> 24 #include "../../../timming.hpp" 25 26 #if defined BOOST_THREAD_USES_CHRONO 27 28 typedef boost::chrono::milliseconds ms; 29 typedef boost::chrono::nanoseconds ns; 30 31 struct Clock 32 { 33 typedef boost::chrono::milliseconds duration; 34 typedef duration::rep rep; 35 typedef duration::period period; 36 typedef boost::chrono::time_point<Clock> time_point; 37 static const bool is_steady = true; 38 nowClock39 static time_point now() 40 { 41 using namespace boost::chrono; 42 return time_point(duration_cast<duration> (steady_clock::now().time_since_epoch())); 43 } 44 }; 45 46 class Pred 47 { 48 int& i_; 49 public: Pred(int & i)50 explicit Pred(int& i) : 51 i_(i) 52 { 53 } 54 operator ()()55 bool operator()() 56 { 57 return i_ != 0; 58 } 59 }; 60 61 boost::condition_variable_any cv; 62 63 typedef boost::timed_mutex L0; 64 typedef boost::unique_lock<L0> L1; 65 66 L0 m0; 67 68 int test1 = 0; 69 int test2 = 0; 70 71 int runs = 0; 72 73 const ms max_diff(BOOST_THREAD_TEST_TIME_MS); 74 f()75void f() 76 { 77 L1 lk(m0); 78 BOOST_TEST(test2 == 0); 79 test1 = 1; 80 cv.notify_one(); 81 Clock::time_point t0 = Clock::now(); 82 Clock::time_point t = t0 + Clock::duration(250); 83 bool r = cv.wait_until(lk, t, Pred(test2)); 84 Clock::time_point t1 = Clock::now(); 85 if (runs == 0) 86 { 87 ns d = t1 - t0; 88 BOOST_THREAD_TEST_IT(d, ns(max_diff)); 89 BOOST_TEST(test2 != 0); 90 BOOST_TEST(r); 91 } 92 else 93 { 94 ns d = t1 - t0 - ms(250); 95 BOOST_THREAD_TEST_IT(d, ns(max_diff)); 96 BOOST_TEST(test2 == 0); 97 BOOST_TEST(!r); 98 } 99 ++runs; 100 } 101 main()102int main() 103 { 104 { 105 L1 lk(m0); 106 boost::thread t(f); 107 BOOST_TEST(test1 == 0); 108 while (test1 == 0) 109 cv.wait(lk); 110 BOOST_TEST(test1 != 0); 111 test2 = 1; 112 lk.unlock(); 113 cv.notify_one(); 114 t.join(); 115 } 116 test1 = 0; 117 test2 = 0; 118 { 119 L1 lk(m0); 120 boost::thread t(f); 121 BOOST_TEST(test1 == 0); 122 while (test1 == 0) 123 cv.wait(lk); 124 BOOST_TEST(test1 != 0); 125 lk.unlock(); 126 t.join(); 127 } 128 129 return boost::report_errors(); 130 } 131 132 #else 133 #error "Test not applicable: BOOST_THREAD_USES_CHRONO not defined for this platform as not supported" 134 #endif 135