system_clocks.hpp 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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 <boost/ratio/config.hpp>
  54. #include <ctime>
  55. # if defined( BOOST_CHRONO_POSIX_API )
  56. # if ! defined(CLOCK_REALTIME) && ! defined (__hpux__)
  57. # error <time.h> does not supply CLOCK_REALTIME
  58. # endif
  59. # endif
  60. #ifdef BOOST_CHRONO_WINDOWS_API
  61. // The system_clock tick is 100 nanoseconds
  62. # define BOOST_SYSTEM_CLOCK_DURATION boost::chrono::duration<boost::int_least64_t, ratio<BOOST_RATIO_INTMAX_C(1), BOOST_RATIO_INTMAX_C(10000000)> >
  63. #else
  64. # define BOOST_SYSTEM_CLOCK_DURATION boost::chrono::nanoseconds
  65. #endif
  66. // this must occur after all of the includes and before any code appears:
  67. #ifndef BOOST_CHRONO_HEADER_ONLY
  68. #include <boost/config/abi_prefix.hpp> // must be the last #include
  69. #endif
  70. //----------------------------------------------------------------------------//
  71. // //
  72. // 20.9 Time utilities [time] //
  73. // synopsis //
  74. // //
  75. //----------------------------------------------------------------------------//
  76. namespace boost {
  77. namespace chrono {
  78. // Clocks
  79. class system_clock;
  80. #ifdef BOOST_CHRONO_HAS_CLOCK_STEADY
  81. class steady_clock;
  82. #endif
  83. #ifdef BOOST_CHRONO_HAS_CLOCK_STEADY
  84. typedef steady_clock high_resolution_clock; // as permitted by [time.clock.hires]
  85. #else
  86. typedef system_clock high_resolution_clock; // as permitted by [time.clock.hires]
  87. #endif
  88. //----------------------------------------------------------------------------//
  89. // //
  90. // 20.9.5 Clocks [time.clock] //
  91. // //
  92. //----------------------------------------------------------------------------//
  93. // If you're porting, clocks are the system-specific (non-portable) part.
  94. // You'll need to know how to get the current time and implement that under now().
  95. // You'll need to know what units (tick period) and representation makes the most
  96. // sense for your clock and set those accordingly.
  97. // If you know how to map this clock to time_t (perhaps your clock is std::time, which
  98. // makes that trivial), then you can fill out system_clock's to_time_t() and from_time_t().
  99. //----------------------------------------------------------------------------//
  100. // 20.9.5.1 Class system_clock [time.clock.system] //
  101. //----------------------------------------------------------------------------//
  102. class BOOST_CHRONO_DECL system_clock
  103. {
  104. public:
  105. typedef BOOST_SYSTEM_CLOCK_DURATION duration;
  106. typedef duration::rep rep;
  107. typedef duration::period period;
  108. typedef chrono::time_point<system_clock> time_point;
  109. BOOST_STATIC_CONSTEXPR bool is_steady = false;
  110. static BOOST_CHRONO_INLINE time_point now() BOOST_NOEXCEPT;
  111. #if !defined BOOST_CHRONO_DONT_PROVIDE_HYBRID_ERROR_HANDLING
  112. static BOOST_CHRONO_INLINE time_point now(system::error_code & ec);
  113. #endif
  114. static BOOST_CHRONO_INLINE std::time_t to_time_t(const time_point& t) BOOST_NOEXCEPT;
  115. static BOOST_CHRONO_INLINE time_point from_time_t(std::time_t t) BOOST_NOEXCEPT;
  116. };
  117. //----------------------------------------------------------------------------//
  118. // 20.9.5.2 Class steady_clock [time.clock.steady] //
  119. //----------------------------------------------------------------------------//
  120. // As permitted by [time.clock.steady]
  121. // The class steady_clock is conditionally supported.
  122. #ifdef BOOST_CHRONO_HAS_CLOCK_STEADY
  123. class BOOST_CHRONO_DECL steady_clock
  124. {
  125. public:
  126. typedef nanoseconds duration;
  127. typedef duration::rep rep;
  128. typedef duration::period period;
  129. typedef chrono::time_point<steady_clock> time_point;
  130. BOOST_STATIC_CONSTEXPR bool is_steady = true;
  131. static BOOST_CHRONO_INLINE time_point now() BOOST_NOEXCEPT;
  132. #if !defined BOOST_CHRONO_DONT_PROVIDE_HYBRID_ERROR_HANDLING
  133. static BOOST_CHRONO_INLINE time_point now(system::error_code & ec);
  134. #endif
  135. };
  136. #endif
  137. //----------------------------------------------------------------------------//
  138. // 20.9.5.3 Class high_resolution_clock [time.clock.hires] //
  139. //----------------------------------------------------------------------------//
  140. // As permitted, steady_clock or system_clock is a typedef for high_resolution_clock.
  141. // See synopsis.
  142. template<class CharT>
  143. struct clock_string<system_clock, CharT>
  144. {
  145. static std::basic_string<CharT> name()
  146. {
  147. static const CharT u[] =
  148. { 's', 'y', 's', 't', 'e', 'm', '_', 'c', 'l', 'o', 'c', 'k' };
  149. static const std::basic_string<CharT> str(u, u + sizeof(u)
  150. / sizeof(u[0]));
  151. return str;
  152. }
  153. static std::basic_string<CharT> since()
  154. {
  155. static const CharT
  156. u[] =
  157. { ' ', 's', 'i', 'n', 'c', 'e', ' ', 'J', 'a', 'n', ' ', '1', ',', ' ', '1', '9', '7', '0' };
  158. static const std::basic_string<CharT> str(u, u + sizeof(u)
  159. / sizeof(u[0]));
  160. return str;
  161. }
  162. };
  163. #ifdef BOOST_CHRONO_HAS_CLOCK_STEADY
  164. template<class CharT>
  165. struct clock_string<steady_clock, CharT>
  166. {
  167. static std::basic_string<CharT> name()
  168. {
  169. static const CharT
  170. u[] =
  171. { 's', 't', 'e', 'a', 'd', 'y', '_', 'c', 'l', 'o', 'c', 'k' };
  172. static const std::basic_string<CharT> str(u, u + sizeof(u)
  173. / sizeof(u[0]));
  174. return str;
  175. }
  176. static std::basic_string<CharT> since()
  177. {
  178. const CharT u[] =
  179. { ' ', 's', 'i', 'n', 'c', 'e', ' ', 'b', 'o', 'o', 't' };
  180. const std::basic_string<CharT> str(u, u + sizeof(u) / sizeof(u[0]));
  181. return str;
  182. }
  183. };
  184. #endif
  185. } // namespace chrono
  186. } // namespace boost
  187. #ifndef BOOST_CHRONO_HEADER_ONLY
  188. // the suffix header occurs after all of our code:
  189. #include <boost/config/abi_suffix.hpp> // pops abi_prefix.hpp pragmas
  190. #else
  191. #include <boost/chrono/detail/inlined/chrono.hpp>
  192. #endif
  193. #endif // BOOST_CHRONO_SYSTEM_CLOCKS_HPP