wait_for_pass.cpp 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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. boost::condition_variable_any cv;
  23. typedef boost::timed_mutex L0;
  24. typedef boost::unique_lock<L0> L1;
  25. L0 m0;
  26. int test1 = 0;
  27. int test2 = 0;
  28. int runs = 0;
  29. typedef boost::chrono::system_clock Clock;
  30. typedef boost::chrono::milliseconds milliseconds;
  31. typedef boost::chrono::milliseconds ms;
  32. typedef boost::chrono::nanoseconds ns;
  33. const ms max_diff(BOOST_THREAD_TEST_TIME_MS);
  34. void f()
  35. {
  36. L1 lk(m0);
  37. BOOST_TEST(test2 == 0);
  38. test1 = 1;
  39. cv.notify_one();
  40. Clock::time_point t0 = Clock::now();
  41. Clock::time_point t = t0 + milliseconds(250);
  42. while (test2 == 0 && cv.wait_for(lk, t - Clock::now()) == boost::cv_status::no_timeout) {}
  43. Clock::time_point t1 = Clock::now();
  44. if (runs == 0)
  45. {
  46. ns d = t1 - t0;
  47. BOOST_THREAD_TEST_IT(d, ns(max_diff));
  48. BOOST_TEST(test2 != 0);
  49. }
  50. else
  51. {
  52. ns d = t1 - t0 - ms(250);
  53. BOOST_THREAD_TEST_IT(d, ns(max_diff));
  54. BOOST_TEST(test2 == 0);
  55. }
  56. ++runs;
  57. }
  58. int main()
  59. {
  60. {
  61. L1 lk(m0);
  62. boost::thread t(f);
  63. BOOST_TEST(test1 == 0);
  64. while (test1 == 0)
  65. cv.wait(lk);
  66. BOOST_TEST(test1 != 0);
  67. test2 = 1;
  68. lk.unlock();
  69. cv.notify_one();
  70. t.join();
  71. }
  72. test1 = 0;
  73. test2 = 0;
  74. {
  75. L1 lk(m0);
  76. boost::thread t(f);
  77. BOOST_TEST(test1 == 0);
  78. while (test1 == 0)
  79. cv.wait(lk);
  80. BOOST_TEST(test1 != 0);
  81. lk.unlock();
  82. t.join();
  83. }
  84. return boost::report_errors();
  85. }
  86. #else
  87. #error "Test not applicable: BOOST_THREAD_USES_CHRONO not defined for this platform as not supported"
  88. #endif