duration_pass.cpp 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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/locks.hpp>
  14. // template <class Mutex> class unique_lock;
  15. // template <class Rep, class Period>
  16. // unique_lock(mutex_type& m, const chrono::duration<Rep, Period>& rel_time);
  17. #define BOOST_THREAD_PROVIDES_GENERIC_SHARED_MUTEX_ON_WIN
  18. #include <boost/thread/lock_types.hpp>
  19. #include <boost/thread/mutex.hpp>
  20. #include <boost/thread/thread.hpp>
  21. #include <boost/detail/lightweight_test.hpp>
  22. #include <boost/chrono/chrono_io.hpp>
  23. #include "../../../../../timming.hpp"
  24. #if defined BOOST_THREAD_USES_CHRONO
  25. boost::timed_mutex m;
  26. typedef boost::chrono::high_resolution_clock Clock;
  27. typedef Clock::time_point time_point;
  28. typedef Clock::duration duration;
  29. typedef boost::chrono::milliseconds ms;
  30. typedef boost::chrono::nanoseconds ns;
  31. time_point t0;
  32. time_point t1;
  33. const ms max_diff(BOOST_THREAD_TEST_TIME_MS);
  34. void f1()
  35. {
  36. t0 = Clock::now();
  37. boost::unique_lock<boost::timed_mutex> lk(m, ms(750));
  38. BOOST_TEST(lk.owns_lock() == true);
  39. t1 = Clock::now();
  40. }
  41. void f2()
  42. {
  43. t0 = Clock::now();
  44. boost::unique_lock<boost::timed_mutex> lk(m, ms(250));
  45. BOOST_TEST(lk.owns_lock() == false);
  46. t1 = Clock::now();
  47. ns d = t1 - t0 - ms(250);
  48. BOOST_THREAD_TEST_IT(d, ns(max_diff));
  49. }
  50. int main()
  51. {
  52. {
  53. m.lock();
  54. boost::thread t(f1);
  55. time_point t2 = Clock::now();
  56. boost::this_thread::sleep_for(ms(250));
  57. time_point t3 = Clock::now();
  58. m.unlock();
  59. t.join();
  60. ns sleep_time = t3 - t2;
  61. ns d_ns = t1 - t0 - sleep_time;
  62. ms d_ms = boost::chrono::duration_cast<boost::chrono::milliseconds>(d_ns);
  63. // BOOST_TEST_GE(d_ms.count(), 0);
  64. BOOST_THREAD_TEST_IT(d_ms, max_diff);
  65. BOOST_THREAD_TEST_IT(d_ns, ns(max_diff));
  66. }
  67. {
  68. m.lock();
  69. boost::thread t(f2);
  70. boost::this_thread::sleep_for(ms(750));
  71. m.unlock();
  72. t.join();
  73. }
  74. return boost::report_errors();
  75. }
  76. #else
  77. #error "Test not applicable: BOOST_THREAD_USES_CHRONO not defined for this platform as not supported"
  78. #endif