chrono.hpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. // win/chrono.cpp --------------------------------------------------------------//
  2. // Copyright Beman Dawes 2008
  3. // Copyright 2009-2010 Vicente J. Botet Escriba
  4. // Distributed under the Boost Software License, Version 1.0.
  5. // See http://www.boost.org/LICENSE_1_0.txt
  6. //----------------------------------------------------------------------------//
  7. // Windows //
  8. //----------------------------------------------------------------------------//
  9. #ifndef BOOST_CHRONO_DETAIL_INLINED_WIN_CHRONO_HPP
  10. #define BOOST_CHRONO_DETAIL_INLINED_WIN_CHRONO_HPP
  11. #include <boost/winapi/time.hpp>
  12. #include <boost/winapi/timers.hpp>
  13. #include <boost/winapi/get_last_error.hpp>
  14. #include <boost/winapi/error_codes.hpp>
  15. #include <boost/assert.hpp>
  16. namespace boost
  17. {
  18. namespace chrono
  19. {
  20. namespace chrono_detail
  21. {
  22. BOOST_CHRONO_INLINE double get_nanosecs_per_tic() BOOST_NOEXCEPT
  23. {
  24. boost::winapi::LARGE_INTEGER_ freq;
  25. if ( !boost::winapi::QueryPerformanceFrequency( &freq ) )
  26. return 0.0L;
  27. return double(1000000000.0L / freq.QuadPart);
  28. }
  29. }
  30. steady_clock::time_point steady_clock::now() BOOST_NOEXCEPT
  31. {
  32. double nanosecs_per_tic = chrono_detail::get_nanosecs_per_tic();
  33. boost::winapi::LARGE_INTEGER_ pcount;
  34. if ( nanosecs_per_tic <= 0.0L )
  35. {
  36. BOOST_ASSERT(0 && "Boost::Chrono - get_nanosecs_per_tic Internal Error");
  37. return steady_clock::time_point();
  38. }
  39. unsigned times=0;
  40. while ( ! boost::winapi::QueryPerformanceCounter( &pcount ) )
  41. {
  42. if ( ++times > 3 )
  43. {
  44. BOOST_ASSERT(0 && "Boost::Chrono - QueryPerformanceCounter Internal Error");
  45. return steady_clock::time_point();
  46. }
  47. }
  48. return steady_clock::time_point(steady_clock::duration(
  49. static_cast<steady_clock::rep>((nanosecs_per_tic) * pcount.QuadPart)));
  50. }
  51. #if !defined BOOST_CHRONO_DONT_PROVIDE_HYBRID_ERROR_HANDLING
  52. steady_clock::time_point steady_clock::now( system::error_code & ec )
  53. {
  54. double nanosecs_per_tic = chrono_detail::get_nanosecs_per_tic();
  55. boost::winapi::LARGE_INTEGER_ pcount;
  56. if ( (nanosecs_per_tic <= 0.0L)
  57. || (!boost::winapi::QueryPerformanceCounter( &pcount )) )
  58. {
  59. boost::winapi::DWORD_ cause =
  60. ((nanosecs_per_tic <= 0.0L)
  61. ? boost::winapi::ERROR_NOT_SUPPORTED_
  62. : boost::winapi::GetLastError());
  63. if (::boost::chrono::is_throws(ec)) {
  64. boost::throw_exception(
  65. system::system_error(
  66. cause,
  67. ::boost::system::system_category(),
  68. "chrono::steady_clock" ));
  69. }
  70. else
  71. {
  72. ec.assign( cause, ::boost::system::system_category() );
  73. return steady_clock::time_point(duration(0));
  74. }
  75. }
  76. if (!::boost::chrono::is_throws(ec))
  77. {
  78. ec.clear();
  79. }
  80. return time_point(duration(
  81. static_cast<steady_clock::rep>(nanosecs_per_tic * pcount.QuadPart)));
  82. }
  83. #endif
  84. BOOST_CHRONO_INLINE
  85. system_clock::time_point system_clock::now() BOOST_NOEXCEPT
  86. {
  87. boost::winapi::FILETIME_ ft;
  88. boost::winapi::GetSystemTimeAsFileTime( &ft ); // never fails
  89. return system_clock::time_point(
  90. system_clock::duration(
  91. ((static_cast<__int64>( ft.dwHighDateTime ) << 32) | ft.dwLowDateTime)
  92. - 116444736000000000LL
  93. //- (134775LL*864000000000LL)
  94. )
  95. );
  96. }
  97. #if !defined BOOST_CHRONO_DONT_PROVIDE_HYBRID_ERROR_HANDLING
  98. BOOST_CHRONO_INLINE
  99. system_clock::time_point system_clock::now( system::error_code & ec )
  100. {
  101. boost::winapi::FILETIME_ ft;
  102. boost::winapi::GetSystemTimeAsFileTime( &ft ); // never fails
  103. if (!::boost::chrono::is_throws(ec))
  104. {
  105. ec.clear();
  106. }
  107. return system_clock::time_point(
  108. system_clock::duration(
  109. ((static_cast<__int64>( ft.dwHighDateTime ) << 32) | ft.dwLowDateTime)
  110. - 116444736000000000LL
  111. //- (134775LL*864000000000LL)
  112. ));
  113. }
  114. #endif
  115. BOOST_CHRONO_INLINE
  116. std::time_t system_clock::to_time_t(const system_clock::time_point& t) BOOST_NOEXCEPT
  117. {
  118. __int64 temp = t.time_since_epoch().count();
  119. temp /= 10000000;
  120. return static_cast<std::time_t>( temp );
  121. }
  122. BOOST_CHRONO_INLINE
  123. system_clock::time_point system_clock::from_time_t(std::time_t t) BOOST_NOEXCEPT
  124. {
  125. __int64 temp = t;
  126. temp *= 10000000;
  127. return time_point(duration(temp));
  128. }
  129. } // namespace chrono
  130. } // namespace boost
  131. #endif