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 class Pred 29 { 30 int& i_; 31 public: Pred(int & i)32 explicit Pred(int& i) : 33 i_(i) 34 { 35 } 36 operator ()()37 bool operator()() 38 { 39 return i_ != 0; 40 } 41 }; 42 43 boost::condition_variable_any cv; 44 45 typedef boost::timed_mutex L0; 46 typedef boost::unique_lock<L0> L1; 47 48 L0 m0; 49 50 int test1 = 0; 51 int test2 = 0; 52 53 int runs = 0; 54 55 typedef boost::chrono::system_clock Clock; 56 typedef boost::chrono::milliseconds milliseconds; 57 typedef boost::chrono::milliseconds ms; 58 typedef boost::chrono::nanoseconds ns; 59 60 const ms max_diff(BOOST_THREAD_TEST_TIME_MS); 61 f()62void f() 63 { 64 L1 lk(m0); 65 BOOST_TEST(test2 == 0); 66 test1 = 1; 67 cv.notify_one(); 68 Clock::time_point t0 = Clock::now(); 69 cv.wait_for(lk, milliseconds(250), Pred(test2)); 70 Clock::time_point t1 = Clock::now(); 71 if (runs == 0) 72 { 73 ns d = t1 - t0 ; 74 BOOST_THREAD_TEST_IT(d, ns(max_diff)); 75 BOOST_TEST(test2 != 0); 76 } 77 else 78 { 79 ns d = t1 - t0 - ms(250); 80 BOOST_THREAD_TEST_IT(d, ns(max_diff)); 81 BOOST_TEST(test2 == 0); 82 } 83 ++runs; 84 } 85 main()86int main() 87 { 88 { 89 L1 lk(m0); 90 boost::thread t(f); 91 BOOST_TEST(test1 == 0); 92 while (test1 == 0) 93 cv.wait(lk); 94 BOOST_TEST(test1 != 0); 95 test2 = 1; 96 lk.unlock(); 97 cv.notify_one(); 98 t.join(); 99 } 100 test1 = 0; 101 test2 = 0; 102 { 103 L1 lk(m0); 104 boost::thread t(f); 105 BOOST_TEST(test1 == 0); 106 while (test1 == 0) 107 cv.wait(lk); 108 BOOST_TEST(test1 != 0); 109 lk.unlock(); 110 t.join(); 111 } 112 113 return boost::report_errors(); 114 } 115 116 #else 117 #error "Test not applicable: BOOST_THREAD_USES_CHRONO not defined for this platform as not supported" 118 #endif 119