wait_until_pass.cpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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. // <boost/thread/condition_variable_any>
  14. // class condition_variable_any;
  15. // condition_variable_any(const condition_variable_any&) = delete;
  16. #include <boost/thread/condition_variable.hpp>
  17. #include <boost/thread/mutex.hpp>
  18. #include <boost/thread/thread.hpp>
  19. #include <boost/detail/lightweight_test.hpp>
  20. #include "../../../timming.hpp"
  21. #if defined BOOST_THREAD_USES_CHRONO
  22. typedef boost::chrono::milliseconds ms;
  23. typedef boost::chrono::nanoseconds ns;
  24. struct Clock
  25. {
  26. typedef boost::chrono::milliseconds duration;
  27. typedef duration::rep rep;
  28. typedef duration::period period;
  29. typedef boost::chrono::time_point<Clock> time_point;
  30. static const bool is_steady = true;
  31. static time_point now()
  32. {
  33. using namespace boost::chrono;
  34. return time_point(duration_cast<duration> (steady_clock::now().time_since_epoch()));
  35. }
  36. };
  37. boost::condition_variable_any cv;
  38. typedef boost::timed_mutex L0;
  39. typedef boost::unique_lock<L0> L1;
  40. L0 m0;
  41. int test1 = 0;
  42. int test2 = 0;
  43. int runs = 0;
  44. const ms max_diff(BOOST_THREAD_TEST_TIME_MS);
  45. void f()
  46. {
  47. L1 lk(m0);
  48. BOOST_TEST(test2 == 0);
  49. test1 = 1;
  50. cv.notify_one();
  51. Clock::time_point t0 = Clock::now();
  52. Clock::time_point t = t0 + Clock::duration(250);
  53. while (test2 == 0 && cv.wait_until(lk, t) == boost::cv_status::no_timeout) {}
  54. Clock::time_point t1 = Clock::now();
  55. if (runs == 0)
  56. {
  57. ns d = t1 - t0;
  58. BOOST_THREAD_TEST_IT(d, ns(max_diff));
  59. BOOST_TEST(test2 != 0);
  60. }
  61. else
  62. {
  63. ns d = t1 - t0 - ms(250);
  64. BOOST_THREAD_TEST_IT(d, ns(max_diff));
  65. BOOST_TEST(test2 == 0);
  66. }
  67. ++runs;
  68. }
  69. int main()
  70. {
  71. {
  72. L1 lk(m0);
  73. boost::thread t(f);
  74. BOOST_TEST(test1 == 0);
  75. while (test1 == 0)
  76. cv.wait(lk);
  77. BOOST_TEST(test1 != 0);
  78. test2 = 1;
  79. lk.unlock();
  80. cv.notify_one();
  81. t.join();
  82. }
  83. test1 = 0;
  84. test2 = 0;
  85. {
  86. L1 lk(m0);
  87. boost::thread t(f);
  88. BOOST_TEST(test1 == 0);
  89. while (test1 == 0)
  90. cv.wait(lk);
  91. BOOST_TEST(test1 != 0);
  92. lk.unlock();
  93. t.join();
  94. }
  95. return boost::report_errors();
  96. }
  97. #else
  98. #error "Test not applicable: BOOST_THREAD_USES_CHRONO not defined for this platform as not supported"
  99. #endif