system_clocks.hpp 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. // boost/chrono/system_clocks.hpp --------------------------------------------------------------//
  2. // Copyright 2008 Howard Hinnant
  3. // Copyright 2008 Beman Dawes
  4. // Copyright 2009-2011 Vicente J. Botet Escriba
  5. // Distributed under the Boost Software License, Version 1.0.
  6. // See http://www.boost.org/LICENSE_1_0.txt
  7. /*
  8. This code was derived by Beman Dawes from Howard Hinnant's time2_demo prototype.
  9. Many thanks to Howard for making his code available under the Boost license.
  10. The original code was modified to conform to Boost conventions and to section
  11. 20.9 Time utilities [time] of the C++ committee's working paper N2798.
  12. See http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2008/n2798.pdf.
  13. time2_demo contained this comment:
  14. Much thanks to Andrei Alexandrescu,
  15. Walter Brown,
  16. Peter Dimov,
  17. Jeff Garland,
  18. Terry Golubiewski,
  19. Daniel Krugler,
  20. Anthony Williams.
  21. */
  22. /*
  23. TODO:
  24. * Fully implement error handling, with test cases.
  25. * Consider issues raised by Michael Marcin:
  26. > In the past I've seen QueryPerformanceCounter give incorrect results,
  27. > especially with SpeedStep processors on laptops. This was many years ago and
  28. > might have been fixed by service packs and drivers.
  29. >
  30. > Typically you check the results of QPC against GetTickCount to see if the
  31. > results are reasonable.
  32. > http://support.microsoft.com/kb/274323
  33. >
  34. > I've also heard of problems with QueryPerformanceCounter in multi-processor
  35. > systems.
  36. >
  37. > I know some people SetThreadAffinityMask to 1 for the current thread call
  38. > their QueryPerformance* functions then restore SetThreadAffinityMask. This
  39. > seems horrible to me because it forces your program to jump to another
  40. > physical processor if it isn't already on cpu0 but they claim it worked well
  41. > in practice because they called the timing functions infrequently.
  42. >
  43. > In the past I have chosen to use timeGetTime with timeBeginPeriod(1) for
  44. > high resolution timers to avoid these issues.
  45. */
  46. #ifndef BOOST_CHRONO_SYSTEM_CLOCKS_HPP
  47. #define BOOST_CHRONO_SYSTEM_CLOCKS_HPP
  48. #include <boost/chrono/config.hpp>
  49. #include <boost/chrono/duration.hpp>
  50. #include <boost/chrono/time_point.hpp>
  51. #include <boost/chrono/detail/system.hpp>
  52. #include <boost/chrono/clock_string.hpp>
  53. #include <ctime>
  54. # if defined( BOOST_CHRONO_POSIX_API )
  55. # if ! defined(CLOCK_REALTIME) && ! defined (__hpux__)
  56. # error <time.h> does not supply CLOCK_REALTIME
  57. # endif
  58. # endif
  59. #ifdef BOOST_CHRONO_WINDOWS_API
  60. // The system_clock tick is 100 nanoseconds
  61. # define BOOST_SYSTEM_CLOCK_DURATION boost::chrono::duration<boost::int_least64_t, ratio<BOOST_RATIO_INTMAX_C(1), BOOST_RATIO_INTMAX_C(10000000)> >
  62. #else
  63. # define BOOST_SYSTEM_CLOCK_DURATION boost::chrono::nanoseconds
  64. #endif
  65. // this must occur after all of the includes and before any code appears:
  66. #ifndef BOOST_CHRONO_HEADER_ONLY
  67. #include <boost/config/abi_prefix.hpp> // must be the last #include
  68. #endif
  69. //----------------------------------------------------------------------------//
  70. // //
  71. // 20.9 Time utilities [time] //
  72. // synopsis //
  73. // //
  74. //----------------------------------------------------------------------------//
  75. namespace boost {
  76. namespace chrono {
  77. // Clocks
  78. class system_clock;
  79. #ifdef BOOST_CHRONO_HAS_CLOCK_STEADY
  80. class steady_clock;
  81. #endif
  82. #ifdef BOOST_CHRONO_HAS_CLOCK_STEADY
  83. typedef steady_clock high_resolution_clock; // as permitted by [time.clock.hires]
  84. #else
  85. typedef system_clock high_resolution_clock; // as permitted by [time.clock.hires]
  86. #endif
  87. //----------------------------------------------------------------------------//
  88. // //
  89. // 20.9.5 Clocks [time.clock] //
  90. // //
  91. //----------------------------------------------------------------------------//
  92. // If you're porting, clocks are the system-specific (non-portable) part.
  93. // You'll need to know how to get the current time and implement that under now().
  94. // You'll need to know what units (tick period) and representation makes the most
  95. // sense for your clock and set those accordingly.
  96. // If you know how to map this clock to time_t (perhaps your clock is std::time, which
  97. // makes that trivial), then you can fill out system_clock's to_time_t() and from_time_t().
  98. //----------------------------------------------------------------------------//
  99. // 20.9.5.1 Class system_clock [time.clock.system] //
  100. //----------------------------------------------------------------------------//
  101. class BOOST_CHRONO_DECL system_clock
  102. {
  103. public:
  104. typedef BOOST_SYSTEM_CLOCK_DURATION duration;
  105. typedef duration::rep rep;
  106. typedef duration::period period;
  107. typedef chrono::time_point<system_clock> time_point;
  108. BOOST_STATIC_CONSTEXPR bool is_steady = false;
  109. static BOOST_CHRONO_INLINE time_point now() BOOST_NOEXCEPT;
  110. #if !defined BOOST_CHRONO_DONT_PROVIDE_HYBRID_ERROR_HANDLING
  111. static BOOST_CHRONO_INLINE time_point now(system::error_code & ec);
  112. #endif
  113. static BOOST_CHRONO_INLINE std::time_t to_time_t(const time_point& t) BOOST_NOEXCEPT;
  114. static BOOST_CHRONO_INLINE time_point from_time_t(std::time_t t) BOOST_NOEXCEPT;
  115. };
  116. //----------------------------------------------------------------------------//
  117. // 20.9.5.2 Class steady_clock [time.clock.steady] //
  118. //----------------------------------------------------------------------------//
  119. // As permitted by [time.clock.steady]
  120. // The class steady_clock is conditionally supported.
  121. #ifdef BOOST_CHRONO_HAS_CLOCK_STEADY
  122. class BOOST_CHRONO_DECL steady_clock
  123. {
  124. public:
  125. typedef nanoseconds duration;
  126. typedef duration::rep rep;
  127. typedef duration::period period;
  128. typedef chrono::time_point<steady_clock> time_point;
  129. BOOST_STATIC_CONSTEXPR bool is_steady = true;
  130. static BOOST_CHRONO_INLINE time_point now() BOOST_NOEXCEPT;
  131. #if !defined BOOST_CHRONO_DONT_PROVIDE_HYBRID_ERROR_HANDLING
  132. static BOOST_CHRONO_INLINE time_point now(system::error_code & ec);
  133. #endif
  134. };
  135. #endif
  136. //----------------------------------------------------------------------------//
  137. // 20.9.5.3 Class high_resolution_clock [time.clock.hires] //
  138. //----------------------------------------------------------------------------//
  139. // As permitted, steady_clock or system_clock is a typedef for high_resolution_clock.
  140. // See synopsis.
  141. template<class CharT>
  142. struct clock_string<system_clock, CharT>
  143. {
  144. static std::basic_string<CharT> name()
  145. {
  146. static const CharT u[] =
  147. { 's', 'y', 's', 't', 'e', 'm', '_', 'c', 'l', 'o', 'c', 'k' };
  148. static const std::basic_string<CharT> str(u, u + sizeof(u)
  149. / sizeof(u[0]));
  150. return str;
  151. }
  152. static std::basic_string<CharT> since()
  153. {
  154. static const CharT
  155. u[] =
  156. { ' ', 's', 'i', 'n', 'c', 'e', ' ', 'J', 'a', 'n', ' ', '1', ',', ' ', '1', '9', '7', '0' };
  157. static const std::basic_string<CharT> str(u, u + sizeof(u)
  158. / sizeof(u[0]));
  159. return str;
  160. }
  161. };
  162. #ifdef BOOST_CHRONO_HAS_CLOCK_STEADY
  163. template<class CharT>
  164. struct clock_string<steady_clock, CharT>
  165. {
  166. static std::basic_string<CharT> name()
  167. {
  168. static const CharT
  169. u[] =
  170. { 's', 't', 'e', 'a', 'd', 'y', '_', 'c', 'l', 'o', 'c', 'k' };
  171. static const std::basic_string<CharT> str(u, u + sizeof(u)
  172. / sizeof(u[0]));
  173. return str;
  174. }
  175. static std::basic_string<CharT> since()
  176. {
  177. const CharT u[] =
  178. { ' ', 's', 'i', 'n', 'c', 'e', ' ', 'b', 'o', 'o', 't' };
  179. const std::basic_string<CharT> str(u, u + sizeof(u) / sizeof(u[0]));
  180. return str;
  181. }
  182. };
  183. #endif
  184. } // namespace chrono
  185. } // namespace boost
  186. #ifndef BOOST_CHRONO_HEADER_ONLY
  187. // the suffix header occurs after all of our code:
  188. #include <boost/config/abi_suffix.hpp> // pops abi_prefix.hpp pragmas
  189. #else
  190. #include <boost/chrono/detail/inlined/chrono.hpp>
  191. #endif
  192. #endif // BOOST_CHRONO_SYSTEM_CLOCKS_HPP