thread_clock.hpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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/detail/winapi/GetLastError.hpp>
  13. #include <boost/detail/winapi/GetCurrentThread.hpp>
  14. #include <boost/detail/winapi/GetThreadTimes.hpp>
  15. namespace boost
  16. {
  17. namespace chrono
  18. {
  19. #if !defined BOOST_CHRONO_DONT_PROVIDE_HYBRID_ERROR_HANDLING
  20. thread_clock::time_point thread_clock::now( system::error_code & ec )
  21. {
  22. // note that Windows uses 100 nanosecond ticks for FILETIME
  23. boost::detail::winapi::FILETIME_ creation, exit, user_time, system_time;
  24. if ( boost::detail::winapi::GetThreadTimes(
  25. boost::detail::winapi::GetCurrentThread (), &creation, &exit,
  26. &system_time, &user_time ) )
  27. {
  28. duration user = duration(
  29. ((static_cast<duration::rep>(user_time.dwHighDateTime) << 32)
  30. | user_time.dwLowDateTime) * 100 );
  31. duration system = duration(
  32. ((static_cast<duration::rep>(system_time.dwHighDateTime) << 32)
  33. | system_time.dwLowDateTime) * 100 );
  34. if (!BOOST_CHRONO_IS_THROWS(ec))
  35. {
  36. ec.clear();
  37. }
  38. return time_point(system+user);
  39. }
  40. else
  41. {
  42. if (BOOST_CHRONO_IS_THROWS(ec))
  43. {
  44. boost::throw_exception(
  45. system::system_error(
  46. boost::detail::winapi::GetLastError(),
  47. BOOST_CHRONO_SYSTEM_CATEGORY,
  48. "chrono::thread_clock" ));
  49. }
  50. else
  51. {
  52. ec.assign( boost::detail::winapi::GetLastError(), BOOST_CHRONO_SYSTEM_CATEGORY );
  53. return thread_clock::time_point(duration(0));
  54. }
  55. }
  56. }
  57. #endif
  58. thread_clock::time_point thread_clock::now() BOOST_NOEXCEPT
  59. {
  60. // note that Windows uses 100 nanosecond ticks for FILETIME
  61. boost::detail::winapi::FILETIME_ creation, exit, user_time, system_time;
  62. if ( boost::detail::winapi::GetThreadTimes(
  63. boost::detail::winapi::GetCurrentThread (), &creation, &exit,
  64. &system_time, &user_time ) )
  65. {
  66. duration user = duration(
  67. ((static_cast<duration::rep>(user_time.dwHighDateTime) << 32)
  68. | user_time.dwLowDateTime) * 100 );
  69. duration system = duration(
  70. ((static_cast<duration::rep>(system_time.dwHighDateTime) << 32)
  71. | system_time.dwLowDateTime) * 100 );
  72. return time_point(system+user);
  73. }
  74. else
  75. {
  76. BOOST_ASSERT(0 && "Boost::Chrono - Internal Error");
  77. return time_point();
  78. }
  79. }
  80. } // namespace chrono
  81. } // namespace boost
  82. #endif