timed_utils.hpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  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. namespace ipcdetail {
  39. BOOST_INTRUSIVE_INSTANTIATE_DEFAULT_TYPE_TMPLT(time_duration_type)
  40. BOOST_INTRUSIVE_INSTANTIATE_DEFAULT_TYPE_TMPLT(clock)
  41. BOOST_INTRUSIVE_INSTANTIATE_DEFAULT_TYPE_TMPLT(rep_type)
  42. BOOST_INTRUSIVE_INSTANTIATE_DEFAULT_TYPE_TMPLT(rep)
  43. template<class T>
  44. struct enable_if_ptime
  45. : enable_if_c< BOOST_INTRUSIVE_HAS_TYPE(boost::interprocess::ipcdetail::, T, time_duration_type) >
  46. {};
  47. template<class T>
  48. struct disable_if_ptime
  49. : enable_if_c< ! BOOST_INTRUSIVE_HAS_TYPE(boost::interprocess::ipcdetail::, T, time_duration_type) >
  50. {};
  51. template<class T>
  52. struct enable_if_ptime_duration
  53. : enable_if_c< BOOST_INTRUSIVE_HAS_TYPE(boost::interprocess::ipcdetail::, T, rep_type) >
  54. {};
  55. template<class T>
  56. struct enable_if_time_point
  57. : enable_if_c< BOOST_INTRUSIVE_HAS_TYPE(boost::interprocess::ipcdetail::, T, clock) >
  58. {};
  59. template<class T>
  60. struct enable_if_duration
  61. : enable_if_c< BOOST_INTRUSIVE_HAS_TYPE(boost::interprocess::ipcdetail::, T, rep) >
  62. {};
  63. #if defined(BOOST_INTERPROCESS_HAS_REENTRANT_STD_FUNCTIONS)
  64. inline std::tm* interprocess_gmtime(const std::time_t* t, std::tm* result)
  65. {
  66. // gmtime_r() not in namespace std???
  67. #if defined(__VMS) && __INITIAL_POINTER_SIZE == 64
  68. std::tm tmp;
  69. if(!gmtime_r(t,&tmp))
  70. result = 0;
  71. else
  72. *result = tmp;
  73. #else
  74. result = gmtime_r(t, result);
  75. #endif
  76. return result;
  77. }
  78. #else // BOOST_DATE_TIME_HAS_REENTRANT_STD_FUNCTIONS
  79. #if defined(__clang__) // Clang has to be checked before MSVC
  80. # pragma clang diagnostic push
  81. # pragma clang diagnostic ignored "-Wdeprecated-declarations"
  82. #elif (defined(_MSC_VER) && (_MSC_VER >= 1400))
  83. # pragma warning(push) // preserve warning settings
  84. # pragma warning(disable : 4996) // disable depricated localtime/gmtime warning on vc8
  85. #endif
  86. inline std::tm* interprocess_gmtime(const std::time_t* t, std::tm* result)
  87. {
  88. result = std::gmtime(t);
  89. return result;
  90. }
  91. #if defined(__clang__) // Clang has to be checked before MSVC
  92. # pragma clang diagnostic pop
  93. #elif (defined(_MSC_VER) && (_MSC_VER >= 1400))
  94. # pragma warning(pop) // restore warnings to previous state
  95. #endif
  96. #endif // BOOST_DATE_TIME_HAS_REENTRANT_STD_FUNCTIONS
  97. #if defined(BOOST_HAS_FTIME)
  98. /*!
  99. * The function converts file_time into number of microseconds elapsed since 1970-Jan-01
  100. *
  101. * \note Only dates after 1970-Jan-01 are supported. Dates before will be wrapped.
  102. */
  103. inline boost::uint64_t file_time_to_microseconds(const boost::winapi::FILETIME_ & ft)
  104. {
  105. // shift is difference between 1970-Jan-01 & 1601-Jan-01
  106. // in 100-nanosecond units
  107. const boost::uint64_t shift = 116444736000000000ULL; // (27111902 << 32) + 3577643008
  108. // 100-nanos since 1601-Jan-01
  109. boost::uint64_t ft_as_integer = (static_cast< boost::uint64_t >(ft.dwHighDateTime) << 32) | static_cast< boost::uint64_t >(ft.dwLowDateTime);
  110. ft_as_integer -= shift; // filetime is now 100-nanos since 1970-Jan-01
  111. return (ft_as_integer / 10U); // truncate to microseconds
  112. }
  113. #endif
  114. class ustime;
  115. class usduration
  116. {
  117. public:
  118. friend class ustime;
  119. explicit usduration(boost::uint64_t microsecs)
  120. : m_microsecs(microsecs)
  121. {}
  122. boost::uint64_t get_microsecs() const
  123. { return m_microsecs; }
  124. bool operator < (const usduration &other) const
  125. { return m_microsecs < other.m_microsecs; }
  126. bool operator > (const usduration &other) const
  127. { return m_microsecs > other.m_microsecs; }
  128. bool operator <= (const usduration &other) const
  129. { return m_microsecs <= other.m_microsecs; }
  130. bool operator >= (const usduration &other) const
  131. { return m_microsecs >= other.m_microsecs; }
  132. private:
  133. boost::uint64_t m_microsecs;
  134. };
  135. class ustime
  136. {
  137. public:
  138. explicit ustime(boost::uint64_t microsecs)
  139. : m_microsecs(microsecs)
  140. {}
  141. ustime &operator += (const usduration &other)
  142. { m_microsecs += other.m_microsecs; return *this; }
  143. ustime operator + (const usduration &other)
  144. { ustime r(*this); r += other; return r; }
  145. ustime &operator -= (const usduration &other)
  146. { m_microsecs -= other.m_microsecs; return *this; }
  147. ustime operator - (const usduration &other)
  148. { ustime r(*this); r -= other; return r; }
  149. friend usduration operator - (const ustime &l, const ustime &r)
  150. { return usduration(l.m_microsecs - r.m_microsecs); }
  151. bool operator < (const ustime &other) const
  152. { return m_microsecs < other.m_microsecs; }
  153. bool operator > (const ustime &other) const
  154. { return m_microsecs > other.m_microsecs; }
  155. bool operator <= (const ustime &other) const
  156. { return m_microsecs <= other.m_microsecs; }
  157. bool operator >= (const ustime &other) const
  158. { return m_microsecs >= other.m_microsecs; }
  159. boost::uint64_t get_microsecs() const
  160. { return m_microsecs; }
  161. private:
  162. boost::uint64_t m_microsecs;
  163. };
  164. inline usduration usduration_milliseconds(boost::uint64_t millisec)
  165. { return usduration(millisec*1000u); }
  166. inline usduration usduration_seconds(boost::uint64_t sec)
  167. { return usduration(sec*uint64_t(1000000u)); }
  168. template<class TimeType, class Enable = void>
  169. class microsec_clock;
  170. template<class TimeType>
  171. class microsec_clock<TimeType, typename enable_if_ptime<TimeType>::type>
  172. {
  173. private:
  174. typedef typename TimeType::date_type date_type;
  175. typedef typename TimeType::time_duration_type time_duration_type;
  176. typedef typename time_duration_type::rep_type resolution_traits_type;
  177. public:
  178. static TimeType universal_time()
  179. {
  180. #ifdef BOOST_HAS_GETTIMEOFDAY
  181. timeval tv;
  182. gettimeofday(&tv, 0); //gettimeofday does not support TZ adjust on Linux.
  183. std::time_t t = tv.tv_sec;
  184. boost::uint32_t sub_sec = static_cast<boost::uint32_t>(tv.tv_usec);
  185. #elif defined(BOOST_HAS_FTIME)
  186. boost::winapi::FILETIME_ ft;
  187. boost::winapi::GetSystemTimeAsFileTime(&ft);
  188. boost::uint64_t micros = file_time_to_microseconds(ft); // it will not wrap, since ft is the current time
  189. // and cannot be before 1970-Jan-01
  190. std::time_t t = static_cast<std::time_t>(micros / 1000000UL); // seconds since epoch
  191. // microseconds -- static casts suppress warnings
  192. boost::uint32_t sub_sec = static_cast<boost::uint32_t>(micros % 1000000UL);
  193. #else
  194. #error "Unsupported date-time error: neither gettimeofday nor FILETIME support is detected"
  195. #endif
  196. std::tm curr;
  197. std::tm* curr_ptr = interprocess_gmtime(&t, &curr);
  198. date_type d(static_cast< typename date_type::year_type::value_type >(curr_ptr->tm_year + 1900),
  199. static_cast< typename date_type::month_type::value_type >(curr_ptr->tm_mon + 1),
  200. static_cast< typename date_type::day_type::value_type >(curr_ptr->tm_mday));
  201. //The following line will adjust the fractional second tick in terms
  202. //of the current time system. For example, if the time system
  203. //doesn't support fractional seconds then res_adjust returns 0
  204. //and all the fractional seconds return 0.
  205. unsigned adjust = static_cast< unsigned >(resolution_traits_type::res_adjust() / 1000000);
  206. time_duration_type td(static_cast< typename time_duration_type::hour_type >(curr_ptr->tm_hour),
  207. static_cast< typename time_duration_type::min_type >(curr_ptr->tm_min),
  208. static_cast< typename time_duration_type::sec_type >(curr_ptr->tm_sec),
  209. static_cast< typename time_duration_type::fractional_seconds_type >(sub_sec * adjust)
  210. );
  211. return TimeType(d,td);
  212. }
  213. };
  214. template<>
  215. class microsec_clock<ustime>
  216. {
  217. public:
  218. static ustime universal_time()
  219. {
  220. #ifdef BOOST_HAS_GETTIMEOFDAY
  221. timeval tv;
  222. gettimeofday(&tv, 0); //gettimeofday does not support TZ adjust on Linux.
  223. boost::uint64_t micros = boost::uint64_t(tv.tv_sec)*1000000;
  224. micros += (boost::uint64_t)tv.tv_usec;
  225. #elif defined(BOOST_HAS_FTIME)
  226. boost::winapi::FILETIME_ ft;
  227. boost::winapi::GetSystemTimeAsFileTime(&ft);
  228. boost::uint64_t micros = file_time_to_microseconds(ft); // it will not wrap, since ft is the current time
  229. // and cannot be before 1970-Jan-01
  230. #else
  231. #error "Unsupported date-time error: neither gettimeofday nor FILETIME support is detected"
  232. #endif
  233. return ustime(micros);
  234. }
  235. };
  236. template<class TimePoint>
  237. class microsec_clock<TimePoint, typename enable_if_time_point<TimePoint>::type>
  238. {
  239. public:
  240. static TimePoint universal_time()
  241. { return TimePoint::clock::now(); }
  242. };
  243. template<class TimePoint>
  244. inline TimePoint delay_ms(unsigned msecs, typename enable_if_ptime<TimePoint>::type* = 0)
  245. {
  246. typedef typename TimePoint::time_duration_type time_duration_type;
  247. typedef typename time_duration_type::rep_type resolution_traits_type;
  248. time_duration_type td(msecs*1000*resolution_traits_type::res_adjust());
  249. TimePoint tp(microsec_clock<TimePoint>::universal_time());
  250. return (tp += td);
  251. }
  252. template<class TimePoint>
  253. inline bool is_pos_infinity(const TimePoint &abs_time, typename enable_if_ptime<TimePoint>::type* = 0)
  254. {
  255. return abs_time.is_pos_infinity();
  256. }
  257. template<class TimePoint>
  258. inline bool is_pos_infinity(const TimePoint &, typename disable_if_ptime<TimePoint>::type* = 0)
  259. {
  260. return false;
  261. }
  262. /*
  263. template<class Duration>
  264. inline ustime duration_to_timepoint(const Duration &dur, typename enable_if_ptime<Duration>::type* = 0)
  265. {
  266. return dur.is_pos_infinity();
  267. }
  268. template<class Duration>
  269. inline bool duration_to_timepoint(const Duration &, typename disable_if_ptime<Duration>::type* = 0)
  270. {
  271. return false;
  272. }
  273. */
  274. // duration_to_milliseconds
  275. template<class Duration>
  276. inline boost::uint64_t duration_to_milliseconds(const Duration &abs_time, typename enable_if_ptime_duration<Duration>::type* = 0)
  277. {
  278. return static_cast<boost::uint64_t>(abs_time.total_milliseconds());
  279. }
  280. template<class Duration>
  281. inline boost::uint64_t duration_to_milliseconds(const Duration &d, typename enable_if_duration<Duration>::type* = 0)
  282. {
  283. const double factor = double(Duration::period::num)*1000.0/double(Duration::period::den);
  284. return static_cast<boost::uint64_t>(double(d.count())*factor);
  285. }
  286. inline boost::uint64_t duration_to_milliseconds(const usduration &d)
  287. {
  288. return d.get_microsecs()/1000;
  289. }
  290. // duration_to_usduration
  291. template<class Duration>
  292. inline usduration duration_to_usduration(const Duration &d, typename enable_if_ptime_duration<Duration>::type* = 0)
  293. {
  294. return usduration(static_cast<boost::uint64_t>(d.total_microseconds()));
  295. }
  296. template<class Duration>
  297. inline usduration duration_to_usduration(const Duration &d, typename enable_if_duration<Duration>::type* = 0)
  298. {
  299. const double factor = double(Duration::period::num)*1000000.0/double(Duration::period::den);
  300. return usduration(static_cast<boost::uint64_t>(double(d.count())*factor));
  301. }
  302. // duration_to_ustime
  303. template<class Duration>
  304. inline ustime duration_to_ustime(const Duration &d)
  305. {
  306. return microsec_clock<ustime>::universal_time() + (duration_to_usduration)(d);
  307. }
  308. } //namespace ipcdetail {
  309. } //namespace interprocess {
  310. } //namespace boost {
  311. #include <boost/interprocess/detail/config_end.hpp>
  312. #endif //#ifndef BOOST_INTERPROCESS_DETAIL_TIMED_UTILS_HPP