thread_clock.hpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. // boost thread_clock.cpp -----------------------------------------------------------//
  2. // Copyright 2010 Vicente J. Botet Escriba
  3. // Distributed under the Boost Software License, Version 1.0.
  4. // See http://www.boost.org/LICENSE_1_0.txt
  5. // See http://www.boost.org/libs/chrono for documentation.
  6. //--------------------------------------------------------------------------------------//
  7. #ifndef BOOST_CHRONO_DETAIL_INLINED_WIN_THREAD_CLOCK_HPP
  8. #define BOOST_CHRONO_DETAIL_INLINED_WIN_THREAD_CLOCK_HPP
  9. #include <boost/chrono/config.hpp>
  10. #include <boost/chrono/thread_clock.hpp>
  11. #include <cassert>
  12. #include <boost/assert.hpp>
  13. #include <boost/detail/winapi/get_last_error.hpp>
  14. #include <boost/detail/winapi/get_current_thread.hpp>
  15. #include <boost/detail/winapi/get_thread_times.hpp>
  16. namespace boost
  17. {
  18. namespace chrono
  19. {
  20. #if !defined BOOST_CHRONO_DONT_PROVIDE_HYBRID_ERROR_HANDLING
  21. thread_clock::time_point thread_clock::now( system::error_code & ec )
  22. {
  23. // note that Windows uses 100 nanosecond ticks for FILETIME
  24. boost::detail::winapi::FILETIME_ creation, exit, user_time, system_time;
  25. if ( boost::detail::winapi::GetThreadTimes(
  26. boost::detail::winapi::GetCurrentThread (), &creation, &exit,
  27. &system_time, &user_time ) )
  28. {
  29. duration user = duration(
  30. ((static_cast<duration::rep>(user_time.dwHighDateTime) << 32)
  31. | user_time.dwLowDateTime) * 100 );
  32. duration system = duration(
  33. ((static_cast<duration::rep>(system_time.dwHighDateTime) << 32)
  34. | system_time.dwLowDateTime) * 100 );
  35. if (!::boost::chrono::is_throws(ec))
  36. {
  37. ec.clear();
  38. }
  39. return time_point(system+user);
  40. }
  41. else
  42. {
  43. if (::boost::chrono::is_throws(ec))
  44. {
  45. boost::throw_exception(
  46. system::system_error(
  47. boost::detail::winapi::GetLastError(),
  48. ::boost::system::system_category(),
  49. "chrono::thread_clock" ));
  50. }
  51. else
  52. {
  53. ec.assign( boost::detail::winapi::GetLastError(), ::boost::system::system_category() );
  54. return thread_clock::time_point(duration(0));
  55. }
  56. }
  57. }
  58. #endif
  59. thread_clock::time_point thread_clock::now() BOOST_NOEXCEPT
  60. {
  61. // note that Windows uses 100 nanosecond ticks for FILETIME
  62. boost::detail::winapi::FILETIME_ creation, exit, user_time, system_time;
  63. if ( boost::detail::winapi::GetThreadTimes(
  64. boost::detail::winapi::GetCurrentThread (), &creation, &exit,
  65. &system_time, &user_time ) )
  66. {
  67. duration user = duration(
  68. ((static_cast<duration::rep>(user_time.dwHighDateTime) << 32)
  69. | user_time.dwLowDateTime) * 100 );
  70. duration system = duration(
  71. ((static_cast<duration::rep>(system_time.dwHighDateTime) << 32)
  72. | system_time.dwLowDateTime) * 100 );
  73. return time_point(system+user);
  74. }
  75. else
  76. {
  77. BOOST_ASSERT(0 && "Boost::Chrono - Internal Error");
  78. return time_point();
  79. }
  80. }
  81. } // namespace chrono
  82. } // namespace boost
  83. #endif