time_point_pass.cpp 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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 upgrade_lock;
  15. // template <class Clock, class Duration>
  16. // upgrade_lock(mutex_type& m, const chrono::time_point<Clock, Duration>& abs_time);
  17. #define BOOST_THREAD_PROVIDES_GENERIC_SHARED_MUTEX_ON_WIN
  18. #include <boost/thread/lock_types.hpp>
  19. #include <boost/thread/shared_mutex.hpp>
  20. #include <boost/thread/thread.hpp>
  21. #include <boost/detail/lightweight_test.hpp>
  22. #include "../../../../../timming.hpp"
  23. boost::shared_mutex m;
  24. typedef boost::chrono::high_resolution_clock Clock;
  25. typedef Clock::time_point time_point;
  26. typedef Clock::duration duration;
  27. typedef boost::chrono::milliseconds ms;
  28. typedef boost::chrono::nanoseconds ns;
  29. time_point t0;
  30. time_point t1;
  31. const ms max_diff(BOOST_THREAD_TEST_TIME_MS);
  32. void f1()
  33. {
  34. t0 = Clock::now();
  35. boost::upgrade_lock<boost::shared_mutex> lk(m, Clock::now() + ms(750));
  36. BOOST_TEST(lk.owns_lock() == true);
  37. t1 = Clock::now();
  38. }
  39. void f2()
  40. {
  41. t0 = Clock::now();
  42. boost::upgrade_lock<boost::shared_mutex> lk(m, Clock::now() + ms(250));
  43. BOOST_TEST(lk.owns_lock() == false);
  44. t1 = Clock::now();
  45. ns d = t1 - t0 - ms(250);
  46. BOOST_THREAD_TEST_IT(d, ns(max_diff));
  47. }
  48. int main()
  49. {
  50. {
  51. m.lock();
  52. boost::thread t(f1);
  53. time_point t2 = Clock::now();
  54. boost::this_thread::sleep_for(ms(250));
  55. time_point t3 = Clock::now();
  56. m.unlock();
  57. t.join();
  58. ns sleep_time = t3 - t2;
  59. ns d_ns = t1 - t0 - sleep_time;
  60. ms d_ms = boost::chrono::duration_cast<boost::chrono::milliseconds>(d_ns);
  61. // BOOST_TEST_GE(d_ms.count(), 0);
  62. BOOST_THREAD_TEST_IT(d_ms, max_diff);
  63. BOOST_THREAD_TEST_IT(d_ns, ns(max_diff));
  64. }
  65. {
  66. m.lock();
  67. boost::thread t(f2);
  68. boost::this_thread::sleep_for(ms(750));
  69. m.unlock();
  70. t.join();
  71. }
  72. return boost::report_errors();
  73. }