duration_pass.cpp 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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 Rep, class Period>
  16. // upgrade_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/shared_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. boost::shared_mutex m;
  25. typedef boost::chrono::high_resolution_clock Clock;
  26. typedef Clock::time_point time_point;
  27. typedef Clock::duration duration;
  28. typedef boost::chrono::milliseconds ms;
  29. typedef boost::chrono::nanoseconds ns;
  30. time_point t0;
  31. time_point t1;
  32. const ms max_diff(BOOST_THREAD_TEST_TIME_MS);
  33. void f1()
  34. {
  35. t0 = Clock::now();
  36. boost::upgrade_lock<boost::shared_mutex> lk(m, ms(750));
  37. BOOST_TEST(lk.owns_lock() == true);
  38. t1 = Clock::now();
  39. }
  40. void f2()
  41. {
  42. t0 = Clock::now();
  43. boost::upgrade_lock<boost::shared_mutex> lk(m, ms(250));
  44. BOOST_TEST(lk.owns_lock() == false);
  45. t1 = Clock::now();
  46. ns d = t1 - t0 - ms(250);
  47. BOOST_THREAD_TEST_IT(d, ns(max_diff));
  48. }
  49. int main()
  50. {
  51. {
  52. m.lock();
  53. boost::thread t(f1);
  54. time_point t2 = Clock::now();
  55. boost::this_thread::sleep_for(ms(250));
  56. time_point t3 = Clock::now();
  57. m.unlock();
  58. t.join();
  59. ns sleep_time = t3 - t2;
  60. ns d_ns = t1 - t0 - sleep_time;
  61. ms d_ms = boost::chrono::duration_cast<boost::chrono::milliseconds>(d_ns);
  62. // BOOST_TEST_GE(d_ms.count(), 0);
  63. BOOST_THREAD_TEST_IT(d_ms, max_diff);
  64. BOOST_THREAD_TEST_IT(d_ns, ns(max_diff));
  65. }
  66. {
  67. m.lock();
  68. boost::thread t(f2);
  69. boost::this_thread::sleep_for(ms(750));
  70. m.unlock();
  71. t.join();
  72. }
  73. return boost::report_errors();
  74. }