BVB Source Codes

mars Show chrono.hpp Source code

Return Download mars: download chrono.hpp Source code - Download mars Source code - Type:.hpp
  1. //  mac/chrono.cpp  --------------------------------------------------------------//
  2.  
  3. //  Copyright Beman Dawes 2008
  4. //  Copyright 2009-2010 Vicente J. Botet Escriba
  5.  
  6. //  Distributed under the Boost Software License, Version 1.0.
  7. //  See http://www.boost.org/LICENSE_1_0.txt
  8.  
  9.  
  10. //----------------------------------------------------------------------------//
  11. //                                 Mac                                        //
  12. //----------------------------------------------------------------------------//
  13.  
  14. #include <sys/time.h> //for gettimeofday and timeval
  15. #include <mach/mach_time.h>  // mach_absolute_time, mach_timebase_info_data_t
  16.  
  17. namespace mars_boost {} namespace boost = mars_boost; namespace mars_boost
  18. {
  19. namespace chrono
  20. {
  21.  
  22. // system_clock
  23.  
  24. // gettimeofday is the most precise "system time" available on this platform.
  25. // It returns the number of microseconds since New Years 1970 in a struct called timeval
  26. // which has a field for seconds and a field for microseconds.
  27. //    Fill in the timeval and then convert that to the time_point
  28. system_clock::time_point
  29. system_clock::now() BOOST_NOEXCEPT
  30. {
  31.     timeval tv;
  32.     gettimeofday(&tv, 0);
  33.     return time_point(seconds(tv.tv_sec) + microseconds(tv.tv_usec));
  34. }
  35.  
  36. #if !defined BOOST_CHRONO_DONT_PROVIDE_HYBRID_ERROR_HANDLING
  37. system_clock::time_point
  38. system_clock::now(system::error_code & ec)
  39. {
  40.     timeval tv;
  41.     gettimeofday(&tv, 0);
  42.     if (!BOOST_CHRONO_IS_THROWS(ec))
  43.     {
  44.         ec.clear();
  45.     }
  46.     return time_point(seconds(tv.tv_sec) + microseconds(tv.tv_usec));
  47. }
  48. #endif
  49. // Take advantage of the fact that on this platform time_t is nothing but
  50. //    an integral count of seconds since New Years 1970 (same epoch as timeval).
  51. //    Just get the duration out of the time_point and truncate it to seconds.
  52. time_t
  53. system_clock::to_time_t(const time_point& t) BOOST_NOEXCEPT
  54. {
  55.     return time_t(duration_cast<seconds>(t.time_since_epoch()).count());
  56. }
  57.  
  58. // Just turn the time_t into a count of seconds and construct a time_point with it.
  59. system_clock::time_point
  60. system_clock::from_time_t(time_t t) BOOST_NOEXCEPT
  61. {
  62.     return system_clock::time_point(seconds(t));
  63. }
  64.  
  65. namespace chrono_detail
  66. {
  67.  
  68. // steady_clock
  69.  
  70. // Note, in this implementation steady_clock and high_resolution_clock
  71. //   are the same clock.  They are both based on mach_absolute_time().
  72. //   mach_absolute_time() * MachInfo.numer / MachInfo.denom is the number of
  73. //   nanoseconds since the computer booted up.  MachInfo.numer and MachInfo.denom
  74. //   are run time constants supplied by the OS.  This clock has no relationship
  75. //   to the Gregorian calendar.  It's main use is as a high resolution timer.
  76.  
  77. // MachInfo.numer / MachInfo.denom is often 1 on the latest equipment.  Specialize
  78. //   for that case as an optimization.
  79. BOOST_CHRONO_STATIC
  80. steady_clock::rep
  81. steady_simplified()
  82. {
  83.     return mach_absolute_time();
  84. }
  85.  
  86. #if !defined BOOST_CHRONO_DONT_PROVIDE_HYBRID_ERROR_HANDLING
  87. BOOST_CHRONO_STATIC
  88. steady_clock::rep
  89. steady_simplified_ec(system::error_code & ec)
  90. {
  91.     if (!BOOST_CHRONO_IS_THROWS(ec))
  92.     {
  93.         ec.clear();
  94.     }
  95.     return mach_absolute_time();
  96. }
  97. #endif
  98.  
  99. BOOST_CHRONO_STATIC
  100. double
  101. compute_steady_factor(kern_return_t& err)
  102. {
  103.     mach_timebase_info_data_t MachInfo;
  104.     err = mach_timebase_info(&MachInfo);
  105.     if ( err != 0  ) {
  106.         return 0;
  107.     }
  108.     return static_cast<double>(MachInfo.numer) / MachInfo.denom;
  109. }
  110.  
  111. BOOST_CHRONO_STATIC
  112. steady_clock::rep
  113. steady_full()
  114. {
  115.     kern_return_t err;
  116.     const double factor = chrono_detail::compute_steady_factor(err);
  117.     if (err != 0)
  118.     {
  119.       BOOST_ASSERT(0 && "Boost::Chrono - Internal Error");
  120.     }
  121.     return static_cast<steady_clock::rep>(mach_absolute_time() * factor);
  122. }
  123.  
  124. #if !defined BOOST_CHRONO_DONT_PROVIDE_HYBRID_ERROR_HANDLING
  125. BOOST_CHRONO_STATIC
  126. steady_clock::rep
  127. steady_full_ec(system::error_code & ec)
  128. {
  129.     kern_return_t err;
  130.     const double factor = chrono_detail::compute_steady_factor(err);
  131.     if (err != 0)
  132.     {
  133.         if (BOOST_CHRONO_IS_THROWS(ec))
  134.         {
  135.             mars_boost::throw_exception(
  136.                     system::system_error(
  137.                             err,
  138.                             BOOST_CHRONO_SYSTEM_CATEGORY,
  139.                             "chrono::steady_clock" ));
  140.         }
  141.         else
  142.         {
  143.             ec.assign( errno, BOOST_CHRONO_SYSTEM_CATEGORY );
  144.             return steady_clock::rep();
  145.         }
  146.     }
  147.     if (!BOOST_CHRONO_IS_THROWS(ec))
  148.     {
  149.         ec.clear();
  150.     }
  151.     return static_cast<steady_clock::rep>(mach_absolute_time() * factor);
  152. }
  153. #endif
  154.  
  155. typedef steady_clock::rep (*FP)();
  156. #if !defined BOOST_CHRONO_DONT_PROVIDE_HYBRID_ERROR_HANDLING
  157. typedef steady_clock::rep (*FP_ec)(system::error_code &);
  158. #endif
  159.  
  160. BOOST_CHRONO_STATIC
  161. FP
  162. init_steady_clock(kern_return_t & err)
  163. {
  164.     mach_timebase_info_data_t MachInfo;
  165.     err = mach_timebase_info(&MachInfo);
  166.     if ( err != 0  )
  167.     {
  168.         return 0;
  169.     }
  170.  
  171.     if (MachInfo.numer == MachInfo.denom)
  172.     {
  173.         return &chrono_detail::steady_simplified;
  174.     }
  175.     return &chrono_detail::steady_full;
  176. }
  177.  
  178. #if !defined BOOST_CHRONO_DONT_PROVIDE_HYBRID_ERROR_HANDLING
  179. BOOST_CHRONO_STATIC
  180. FP_ec
  181. init_steady_clock_ec(kern_return_t & err)
  182. {
  183.     mach_timebase_info_data_t MachInfo;
  184.     err = mach_timebase_info(&MachInfo);
  185.     if ( err != 0  )
  186.     {
  187.         return 0;
  188.     }
  189.  
  190.     if (MachInfo.numer == MachInfo.denom)
  191.     {
  192.         return &chrono_detail::steady_simplified_ec;
  193.     }
  194.     return &chrono_detail::steady_full_ec;
  195. }
  196. #endif
  197. }
  198.  
  199. steady_clock::time_point
  200. steady_clock::now() BOOST_NOEXCEPT
  201. {
  202.     kern_return_t err;
  203.     chrono_detail::FP fp = chrono_detail::init_steady_clock(err);
  204.     if ( err != 0  )
  205.     {
  206.       BOOST_ASSERT(0 && "Boost::Chrono - Internal Error");
  207.     }
  208.     return time_point(duration(fp()));
  209. }
  210.  
  211. #if !defined BOOST_CHRONO_DONT_PROVIDE_HYBRID_ERROR_HANDLING
  212. steady_clock::time_point
  213. steady_clock::now(system::error_code & ec)
  214. {
  215.     kern_return_t err;
  216.     chrono_detail::FP_ec fp = chrono_detail::init_steady_clock_ec(err);
  217.     if ( err != 0  )
  218.     {
  219.         if (BOOST_CHRONO_IS_THROWS(ec))
  220.         {
  221.             mars_boost::throw_exception(
  222.                     system::system_error(
  223.                             err,
  224.                             BOOST_CHRONO_SYSTEM_CATEGORY,
  225.                             "chrono::steady_clock" ));
  226.         }
  227.         else
  228.         {
  229.             ec.assign( err, BOOST_CHRONO_SYSTEM_CATEGORY );
  230.             return time_point();
  231.         }
  232.     }
  233.     if (!BOOST_CHRONO_IS_THROWS(ec))
  234.     {
  235.         ec.clear();
  236.     }
  237.     return time_point(duration(fp(ec)));
  238. }
  239. #endif
  240. }  // namespace chrono
  241. }  // namespace mars_boost
  242.  
downloadchrono.hpp Source code - Download mars Source code
Related Source Codes/Software:
Hero - Elegant transition library for iOS & tvOS 2017-06-09
deep-photo-styletransfer - Code and data for paper "Deep Photo Style Transfer... 2017-06-09
mastodon - A GNU Social-compatible microblogging server ... 2017-06-09
plyr - A simple HTML5, YouTube and Vimeo player ... 2017-06-08
prepack - Prepack is a partial evaluator for JavaScript. Pre... 2017-06-08
Public-APIs - 2017-06-09
lottie-ios - An iOS library to natively render After Effects ve... 2017-06-09
Awesome-Hacking - A collection of various awesome lists for hackers,... 2017-06-09
algorithms - Minimal examples of data structures and algorithms... 2017-06-10
lectures - Oxford Deep NLP 2017 course 2017-06-10
CRYENGINE - CRYENGINE is a powerful real-time game development... 2017-06-11
postal - 2017-06-11
reactide - Reactide is the first dedicated IDE for React web ... 2017-06-11
rkt - rkt is a pod-native container engine for Linux. It... 2017-06-11
uWebSockets - Tiny WebSockets https://for... 2017-06-11
realworld - TodoMVC for the RealWorld - Exemplary fullstack Me... 2017-06-11
goreplay - GoReplay is an open-source tool for capturing and ... 2017-06-10
pyenv - Simple Python version management 2017-06-10
redux-saga - An alternative side effect model for Redux apps ... 2017-06-10
angular-starter - 2017-06-10

 Back to top