timed_utils.hpp 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. //////////////////////////////////////////////////////////////////////////////
  2. //
  3. // (C) Copyright Ion Gaztanaga 2021-2021.
  4. //
  5. // Distributed under the Boost Software License, Version 1.0.
  6. // (See accompanying file LICENSE_1_0.txt or copy at
  7. // http://www.boost.org/LICENSE_1_0.txt)
  8. //
  9. // See http://www.boost.org/libs/interprocess for documentation.
  10. //
  11. //////////////////////////////////////////////////////////////////////////////
  12. #ifndef BOOST_INTERPROCESS_DETAIL_TIMED_UTILS_HPP
  13. #define BOOST_INTERPROCESS_DETAIL_TIMED_UTILS_HPP
  14. #ifndef BOOST_CONFIG_HPP
  15. # include <boost/config.hpp>
  16. #endif
  17. #
  18. #if defined(BOOST_HAS_PRAGMA_ONCE)
  19. # pragma once
  20. #endif
  21. #include <boost/interprocess/detail/config_begin.hpp>
  22. #include <boost/interprocess/detail/workaround.hpp>
  23. #include <boost/interprocess/detail/mpl.hpp>
  24. #include <boost/interprocess/detail/type_traits.hpp>
  25. #include <boost/intrusive/detail/mpl.hpp>
  26. #include <ctime>
  27. #include <boost/cstdint.hpp>
  28. //The following is used to support high precision time clocks
  29. #ifdef BOOST_HAS_GETTIMEOFDAY
  30. #include <sys/time.h>
  31. #endif
  32. #ifdef BOOST_HAS_FTIME
  33. #include <time.h>
  34. #include <boost/winapi/time.hpp>
  35. #endif
  36. namespace boost {
  37. namespace interprocess {
  38. class ustime;
  39. class usduration;
  40. namespace ipcdetail {
  41. BOOST_INTRUSIVE_INSTANTIATE_DEFAULT_TYPE_TMPLT(time_duration_type)
  42. BOOST_INTRUSIVE_INSTANTIATE_DEFAULT_TYPE_TMPLT(clock)
  43. BOOST_INTRUSIVE_INSTANTIATE_DEFAULT_TYPE_TMPLT(rep_type)
  44. BOOST_INTRUSIVE_INSTANTIATE_DEFAULT_TYPE_TMPLT(rep)
  45. template<class T>
  46. struct enable_if_ptime
  47. : enable_if_c< BOOST_INTRUSIVE_HAS_TYPE(boost::interprocess::ipcdetail::, T, time_duration_type) >
  48. {};
  49. template<class T>
  50. struct disable_if_ptime
  51. : enable_if_c< ! BOOST_INTRUSIVE_HAS_TYPE(boost::interprocess::ipcdetail::, T, time_duration_type) >
  52. {};
  53. template<class T>
  54. struct enable_if_ptime_duration
  55. : enable_if_c< BOOST_INTRUSIVE_HAS_TYPE(boost::interprocess::ipcdetail::, T, rep_type) >
  56. {};
  57. template<class T>
  58. struct enable_if_time_point
  59. : enable_if_c< BOOST_INTRUSIVE_HAS_TYPE(boost::interprocess::ipcdetail::, T, clock) >
  60. {};
  61. template<class T>
  62. struct enable_if_duration
  63. : enable_if_c< BOOST_INTRUSIVE_HAS_TYPE(boost::interprocess::ipcdetail::, T, rep) >
  64. {};
  65. template<class T>
  66. struct enable_if_ustime
  67. : enable_if_c< is_same<T, ustime>::value >
  68. {};
  69. template<class T>
  70. struct enable_if_usduration
  71. : enable_if_c< is_same<T, usduration>::value >
  72. {};
  73. #if defined(BOOST_INTERPROCESS_HAS_REENTRANT_STD_FUNCTIONS)
  74. inline std::tm* interprocess_gmtime(const std::time_t* t, std::tm* result)
  75. {
  76. // gmtime_r() not in namespace std???
  77. #if defined(__VMS) && __INITIAL_POINTER_SIZE == 64
  78. std::tm tmp;
  79. if(!gmtime_r(t,&tmp))
  80. result = 0;
  81. else
  82. *result = tmp;
  83. #else
  84. result = gmtime_r(t, result);
  85. #endif
  86. return result;
  87. }
  88. #else // BOOST_DATE_TIME_HAS_REENTRANT_STD_FUNCTIONS
  89. #if defined(__clang__) // Clang has to be checked before MSVC
  90. # pragma clang diagnostic push
  91. # pragma clang diagnostic ignored "-Wdeprecated-declarations"
  92. #elif (defined(_MSC_VER) && (_MSC_VER >= 1400))
  93. # pragma warning(push) // preserve warning settings
  94. # pragma warning(disable : 4996) // disable depricated localtime/gmtime warning on vc8
  95. #endif
  96. inline std::tm* interprocess_gmtime(const std::time_t* t, std::tm* result)
  97. {
  98. result = std::gmtime(t);
  99. return result;
  100. }
  101. #if defined(__clang__) // Clang has to be checked before MSVC
  102. # pragma clang diagnostic pop
  103. #elif (defined(_MSC_VER) && (_MSC_VER >= 1400))
  104. # pragma warning(pop) // restore warnings to previous state
  105. #endif
  106. #endif // BOOST_DATE_TIME_HAS_REENTRANT_STD_FUNCTIONS
  107. #if defined(BOOST_HAS_FTIME)
  108. /*!
  109. * The function converts file_time into number of microseconds elapsed since 1970-Jan-01
  110. *
  111. * \note Only dates after 1970-Jan-01 are supported. Dates before will be wrapped.
  112. */
  113. inline boost::uint64_t file_time_to_microseconds(const boost::winapi::FILETIME_ & ft)
  114. {
  115. // shift is difference between 1970-Jan-01 & 1601-Jan-01
  116. // in 100-nanosecond units
  117. const boost::uint64_t shift = 116444736000000000ULL; // (27111902 << 32) + 3577643008
  118. // 100-nanos since 1601-Jan-01
  119. boost::uint64_t ft_as_integer = (static_cast< boost::uint64_t >(ft.dwHighDateTime) << 32) | static_cast< boost::uint64_t >(ft.dwLowDateTime);
  120. ft_as_integer -= shift; // filetime is now 100-nanos since 1970-Jan-01
  121. return (ft_as_integer / 10U); // truncate to microseconds
  122. }
  123. #endif
  124. inline boost::uint64_t universal_time_u64_us()
  125. {
  126. #ifdef BOOST_HAS_GETTIMEOFDAY
  127. timeval tv;
  128. gettimeofday(&tv, 0); //gettimeofday does not support TZ adjust on Linux.
  129. boost::uint64_t micros = boost::uint64_t(tv.tv_sec)*1000000u;
  130. micros += (boost::uint64_t)tv.tv_usec;
  131. #elif defined(BOOST_HAS_FTIME)
  132. boost::winapi::FILETIME_ ft;
  133. boost::winapi::GetSystemTimeAsFileTime(&ft);
  134. boost::uint64_t micros = file_time_to_microseconds(ft); // it will not wrap, since ft is the current time
  135. // and cannot be before 1970-Jan-01
  136. #else
  137. #error "Unsupported date-time error: neither gettimeofday nor FILETIME support is detected"
  138. #endif
  139. return micros;
  140. }
  141. template<class TimeType, class Enable = void>
  142. class microsec_clock;
  143. template<class TimeType>
  144. class microsec_clock<TimeType, typename enable_if_ptime<TimeType>::type>
  145. {
  146. private:
  147. typedef typename TimeType::date_type date_type;
  148. typedef typename TimeType::time_duration_type time_duration_type;
  149. typedef typename time_duration_type::rep_type resolution_traits_type;
  150. public:
  151. typedef TimeType time_point;
  152. static time_point universal_time()
  153. {
  154. #ifdef BOOST_HAS_GETTIMEOFDAY
  155. timeval tv;
  156. gettimeofday(&tv, 0); //gettimeofday does not support TZ adjust on Linux.
  157. std::time_t t = tv.tv_sec;
  158. boost::uint32_t sub_sec = static_cast<boost::uint32_t>(tv.tv_usec);
  159. #elif defined(BOOST_HAS_FTIME)
  160. boost::winapi::FILETIME_ ft;
  161. boost::winapi::GetSystemTimeAsFileTime(&ft);
  162. boost::uint64_t micros = file_time_to_microseconds(ft); // it will not wrap, since ft is the current time
  163. // and cannot be before 1970-Jan-01
  164. std::time_t t = static_cast<std::time_t>(micros / 1000000UL); // seconds since epoch
  165. // microseconds -- static casts suppress warnings
  166. boost::uint32_t sub_sec = static_cast<boost::uint32_t>(micros % 1000000UL);
  167. #else
  168. #error "Unsupported date-time error: neither gettimeofday nor FILETIME support is detected"
  169. #endif
  170. std::tm curr;
  171. std::tm* curr_ptr = interprocess_gmtime(&t, &curr);
  172. date_type d(static_cast< typename date_type::year_type::value_type >(curr_ptr->tm_year + 1900),
  173. static_cast< typename date_type::month_type::value_type >(curr_ptr->tm_mon + 1),
  174. static_cast< typename date_type::day_type::value_type >(curr_ptr->tm_mday));
  175. //The following line will adjust the fractional second tick in terms
  176. //of the current time system. For example, if the time system
  177. //doesn't support fractional seconds then res_adjust returns 0
  178. //and all the fractional seconds return 0.
  179. unsigned adjust = static_cast< unsigned >(resolution_traits_type::res_adjust() / 1000000);
  180. time_duration_type td(static_cast< typename time_duration_type::hour_type >(curr_ptr->tm_hour),
  181. static_cast< typename time_duration_type::min_type >(curr_ptr->tm_min),
  182. static_cast< typename time_duration_type::sec_type >(curr_ptr->tm_sec),
  183. static_cast< typename time_duration_type::fractional_seconds_type >(sub_sec * adjust)
  184. );
  185. return time_point(d,td);
  186. }
  187. };
  188. template<class TimePoint>
  189. class microsec_clock<TimePoint, typename enable_if_time_point<TimePoint>::type>
  190. {
  191. public:
  192. typedef typename TimePoint::clock::time_point time_point;
  193. static time_point universal_time()
  194. { return TimePoint::clock::now(); }
  195. };
  196. template<class TimePoint>
  197. inline bool is_pos_infinity(const TimePoint &abs_time, typename enable_if_ptime<TimePoint>::type* = 0)
  198. {
  199. return abs_time.is_pos_infinity();
  200. }
  201. template<class TimePoint>
  202. inline bool is_pos_infinity(const TimePoint &, typename disable_if_ptime<TimePoint>::type* = 0)
  203. {
  204. return false;
  205. }
  206. // duration_to_milliseconds
  207. template<class Duration>
  208. inline boost::uint64_t duration_to_milliseconds(const Duration &abs_time, typename enable_if_ptime_duration<Duration>::type* = 0)
  209. {
  210. return static_cast<boost::uint64_t>(abs_time.total_milliseconds());
  211. }
  212. template<class Duration>
  213. inline boost::uint64_t duration_to_milliseconds(const Duration &d, typename enable_if_duration<Duration>::type* = 0)
  214. {
  215. const double factor = double(Duration::period::num)*1000.0/double(Duration::period::den);
  216. return static_cast<boost::uint64_t>(double(d.count())*factor);
  217. }
  218. template<class Duration>
  219. inline boost::uint64_t duration_to_milliseconds(const Duration&d, typename enable_if_usduration<Duration>::type* = 0)
  220. {
  221. return d.get_microsecs()/1000u;
  222. }
  223. } //namespace ipcdetail {
  224. } //namespace interprocess {
  225. } //namespace boost {
  226. #include <boost/interprocess/detail/config_end.hpp>
  227. #endif //#ifndef BOOST_INTERPROCESS_DETAIL_TIMED_UTILS_HPP