platform_time.hpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478
  1. #ifndef BOOST_THREAD_DETAIL_PLATFORM_TIME_HPP
  2. #define BOOST_THREAD_DETAIL_PLATFORM_TIME_HPP
  3. // (C) Copyright 2007-8 Anthony Williams
  4. // (C) Copyright 2012 Vicente J. Botet Escriba
  5. //
  6. // Distributed under the Boost Software License, Version 1.0. (See
  7. // accompanying file LICENSE_1_0.txt or copy at
  8. // http://www.boost.org/LICENSE_1_0.txt)
  9. #include <boost/thread/detail/config.hpp>
  10. #include <boost/thread/thread_time.hpp>
  11. #if defined BOOST_THREAD_USES_DATETIME
  12. #include <boost/date_time/posix_time/conversion.hpp>
  13. #endif
  14. #ifndef _WIN32
  15. #include <unistd.h>
  16. #endif
  17. #ifdef BOOST_THREAD_USES_CHRONO
  18. #include <boost/chrono/duration.hpp>
  19. #include <boost/chrono/system_clocks.hpp>
  20. #include <boost/chrono/ceil.hpp>
  21. #endif
  22. #if defined(BOOST_THREAD_CHRONO_WINDOWS_API)
  23. #include <boost/winapi/time.hpp>
  24. #include <boost/winapi/timers.hpp>
  25. #include <boost/thread/win32/thread_primitives.hpp>
  26. #elif defined(BOOST_THREAD_CHRONO_MAC_API)
  27. #include <sys/time.h> //for gettimeofday and timeval
  28. #include <mach/mach_time.h> // mach_absolute_time, mach_timebase_info_data_t
  29. #else
  30. #include <time.h> // for clock_gettime
  31. #endif
  32. #include <limits>
  33. #include <boost/config/abi_prefix.hpp>
  34. namespace boost
  35. {
  36. //typedef boost::int_least64_t time_max_t;
  37. typedef boost::intmax_t time_max_t;
  38. #if defined BOOST_THREAD_CHRONO_MAC_API
  39. namespace threads
  40. {
  41. namespace chrono_details
  42. {
  43. // steady_clock
  44. // Note, in this implementation steady_clock and high_resolution_clock
  45. // are the same clock. They are both based on mach_absolute_time().
  46. // mach_absolute_time() * MachInfo.numer / MachInfo.denom is the number of
  47. // nanoseconds since the computer booted up. MachInfo.numer and MachInfo.denom
  48. // are run time constants supplied by the OS. This clock has no relationship
  49. // to the Gregorian calendar. It's main use is as a high resolution timer.
  50. // MachInfo.numer / MachInfo.denom is often 1 on the latest equipment. Specialize
  51. // for that case as an optimization.
  52. inline time_max_t
  53. steady_simplified()
  54. {
  55. return mach_absolute_time();
  56. }
  57. inline double compute_steady_factor(kern_return_t& err)
  58. {
  59. mach_timebase_info_data_t MachInfo;
  60. err = mach_timebase_info(&MachInfo);
  61. if ( err != 0 ) {
  62. return 0;
  63. }
  64. return static_cast<double>(MachInfo.numer) / MachInfo.denom;
  65. }
  66. inline time_max_t steady_full()
  67. {
  68. kern_return_t err;
  69. const double factor = chrono_details::compute_steady_factor(err);
  70. if (err != 0)
  71. {
  72. BOOST_ASSERT(0 && "Boost::Chrono - Internal Error");
  73. }
  74. return static_cast<time_max_t>(mach_absolute_time() * factor);
  75. }
  76. typedef time_max_t (*FP)();
  77. inline FP init_steady_clock(kern_return_t & err)
  78. {
  79. mach_timebase_info_data_t MachInfo;
  80. err = mach_timebase_info(&MachInfo);
  81. if ( err != 0 )
  82. {
  83. return 0;
  84. }
  85. if (MachInfo.numer == MachInfo.denom)
  86. {
  87. return &chrono_details::steady_simplified;
  88. }
  89. return &chrono_details::steady_full;
  90. }
  91. }
  92. }
  93. #endif
  94. namespace detail
  95. {
  96. #if defined BOOST_THREAD_CHRONO_POSIX_API || defined BOOST_THREAD_CHRONO_MAC_API
  97. inline timespec ns_to_timespec(boost::time_max_t const& ns)
  98. {
  99. boost::time_max_t s = ns / 1000000000l;
  100. timespec ts;
  101. ts.tv_sec = static_cast<long> (s);
  102. ts.tv_nsec = static_cast<long> (ns - s * 1000000000l);
  103. return ts;
  104. }
  105. inline boost::time_max_t timespec_to_ns(timespec const& ts)
  106. {
  107. return static_cast<boost::time_max_t>(ts.tv_sec) * 1000000000l + ts.tv_nsec;
  108. }
  109. #endif
  110. struct platform_duration
  111. {
  112. #if defined BOOST_THREAD_CHRONO_POSIX_API || defined BOOST_THREAD_CHRONO_MAC_API
  113. explicit platform_duration(timespec const& v) : ts_val(v) {}
  114. timespec const& getTs() const { return ts_val; }
  115. explicit platform_duration(boost::time_max_t const& ns = 0) : ts_val(ns_to_timespec(ns)) {}
  116. boost::time_max_t getNs() const { return timespec_to_ns(ts_val); }
  117. #else
  118. explicit platform_duration(boost::time_max_t const& ns = 0) : ns_val(ns) {}
  119. boost::time_max_t getNs() const { return ns_val; }
  120. #endif
  121. #if defined BOOST_THREAD_USES_DATETIME
  122. platform_duration(boost::posix_time::time_duration const& rel_time)
  123. {
  124. #if defined BOOST_THREAD_CHRONO_POSIX_API || defined BOOST_THREAD_CHRONO_MAC_API
  125. ts_val.tv_sec = rel_time.total_seconds();
  126. ts_val.tv_nsec = static_cast<long>(rel_time.fractional_seconds() * (1000000000l / rel_time.ticks_per_second()));
  127. #else
  128. ns_val = static_cast<boost::time_max_t>(rel_time.total_seconds()) * 1000000000l;
  129. ns_val += rel_time.fractional_seconds() * (1000000000l / rel_time.ticks_per_second());
  130. #endif
  131. }
  132. #endif
  133. #if defined BOOST_THREAD_USES_CHRONO
  134. template <class Rep, class Period>
  135. platform_duration(chrono::duration<Rep, Period> const& d)
  136. {
  137. #if defined BOOST_THREAD_CHRONO_POSIX_API || defined BOOST_THREAD_CHRONO_MAC_API
  138. ts_val = ns_to_timespec(chrono::ceil<chrono::nanoseconds>(d).count());
  139. #else
  140. ns_val = chrono::ceil<chrono::nanoseconds>(d).count();
  141. #endif
  142. }
  143. #endif
  144. boost::time_max_t getMs() const
  145. {
  146. const boost::time_max_t ns = getNs();
  147. // ceil/floor away from zero
  148. if (ns >= 0)
  149. {
  150. // return ceiling of positive numbers
  151. return (ns + 999999) / 1000000;
  152. }
  153. else
  154. {
  155. // return floor of negative numbers
  156. return (ns - 999999) / 1000000;
  157. }
  158. }
  159. static platform_duration zero()
  160. {
  161. return platform_duration(0);
  162. }
  163. private:
  164. #if defined BOOST_THREAD_CHRONO_POSIX_API || defined BOOST_THREAD_CHRONO_MAC_API
  165. timespec ts_val;
  166. #else
  167. boost::time_max_t ns_val;
  168. #endif
  169. };
  170. inline bool operator==(platform_duration const& lhs, platform_duration const& rhs)
  171. {
  172. return lhs.getNs() == rhs.getNs();
  173. }
  174. inline bool operator!=(platform_duration const& lhs, platform_duration const& rhs)
  175. {
  176. return lhs.getNs() != rhs.getNs();
  177. }
  178. inline bool operator<(platform_duration const& lhs, platform_duration const& rhs)
  179. {
  180. return lhs.getNs() < rhs.getNs();
  181. }
  182. inline bool operator<=(platform_duration const& lhs, platform_duration const& rhs)
  183. {
  184. return lhs.getNs() <= rhs.getNs();
  185. }
  186. inline bool operator>(platform_duration const& lhs, platform_duration const& rhs)
  187. {
  188. return lhs.getNs() > rhs.getNs();
  189. }
  190. inline bool operator>=(platform_duration const& lhs, platform_duration const& rhs)
  191. {
  192. return lhs.getNs() >= rhs.getNs();
  193. }
  194. static inline platform_duration platform_milliseconds(long const& ms)
  195. {
  196. return platform_duration(ms * 1000000l);
  197. }
  198. struct real_platform_timepoint
  199. {
  200. #if defined BOOST_THREAD_CHRONO_POSIX_API || defined BOOST_THREAD_CHRONO_MAC_API
  201. explicit real_platform_timepoint(timespec const& v) : dur(v) {}
  202. timespec const& getTs() const { return dur.getTs(); }
  203. #endif
  204. explicit real_platform_timepoint(boost::time_max_t const& ns) : dur(ns) {}
  205. boost::time_max_t getNs() const { return dur.getNs(); }
  206. #if defined BOOST_THREAD_USES_DATETIME
  207. real_platform_timepoint(boost::system_time const& abs_time)
  208. : dur(abs_time - boost::posix_time::from_time_t(0)) {}
  209. #endif
  210. #if defined BOOST_THREAD_USES_CHRONO
  211. template <class Duration>
  212. real_platform_timepoint(chrono::time_point<chrono::system_clock, Duration> const& abs_time)
  213. : dur(abs_time.time_since_epoch()) {}
  214. #endif
  215. private:
  216. platform_duration dur;
  217. };
  218. inline bool operator==(real_platform_timepoint const& lhs, real_platform_timepoint const& rhs)
  219. {
  220. return lhs.getNs() == rhs.getNs();
  221. }
  222. inline bool operator!=(real_platform_timepoint const& lhs, real_platform_timepoint const& rhs)
  223. {
  224. return lhs.getNs() != rhs.getNs();
  225. }
  226. inline bool operator<(real_platform_timepoint const& lhs, real_platform_timepoint const& rhs)
  227. {
  228. return lhs.getNs() < rhs.getNs();
  229. }
  230. inline bool operator<=(real_platform_timepoint const& lhs, real_platform_timepoint const& rhs)
  231. {
  232. return lhs.getNs() <= rhs.getNs();
  233. }
  234. inline bool operator>(real_platform_timepoint const& lhs, real_platform_timepoint const& rhs)
  235. {
  236. return lhs.getNs() > rhs.getNs();
  237. }
  238. inline bool operator>=(real_platform_timepoint const& lhs, real_platform_timepoint const& rhs)
  239. {
  240. return lhs.getNs() >= rhs.getNs();
  241. }
  242. inline real_platform_timepoint operator+(real_platform_timepoint const& lhs, platform_duration const& rhs)
  243. {
  244. return real_platform_timepoint(lhs.getNs() + rhs.getNs());
  245. }
  246. inline real_platform_timepoint operator+(platform_duration const& lhs, real_platform_timepoint const& rhs)
  247. {
  248. return real_platform_timepoint(lhs.getNs() + rhs.getNs());
  249. }
  250. inline platform_duration operator-(real_platform_timepoint const& lhs, real_platform_timepoint const& rhs)
  251. {
  252. return platform_duration(lhs.getNs() - rhs.getNs());
  253. }
  254. struct real_platform_clock
  255. {
  256. static real_platform_timepoint now()
  257. {
  258. #if defined(BOOST_THREAD_CHRONO_WINDOWS_API)
  259. boost::winapi::FILETIME_ ft;
  260. boost::winapi::GetSystemTimeAsFileTime(&ft); // never fails
  261. boost::time_max_t ns = ((((static_cast<boost::time_max_t>(ft.dwHighDateTime) << 32) | ft.dwLowDateTime) - 116444736000000000LL) * 100LL);
  262. return real_platform_timepoint(ns);
  263. #elif defined(BOOST_THREAD_CHRONO_MAC_API)
  264. timeval tv;
  265. ::gettimeofday(&tv, 0);
  266. timespec ts;
  267. ts.tv_sec = tv.tv_sec;
  268. ts.tv_nsec = tv.tv_usec * 1000;
  269. return real_platform_timepoint(ts);
  270. #else
  271. timespec ts;
  272. if ( ::clock_gettime( CLOCK_REALTIME, &ts ) )
  273. {
  274. BOOST_ASSERT(0 && "Boost::Thread - clock_gettime(CLOCK_REALTIME) Internal Error");
  275. return real_platform_timepoint(0);
  276. }
  277. return real_platform_timepoint(ts);
  278. #endif
  279. }
  280. };
  281. #if defined(BOOST_THREAD_HAS_MONO_CLOCK)
  282. struct mono_platform_timepoint
  283. {
  284. #if defined BOOST_THREAD_CHRONO_POSIX_API || defined BOOST_THREAD_CHRONO_MAC_API
  285. explicit mono_platform_timepoint(timespec const& v) : dur(v) {}
  286. timespec const& getTs() const { return dur.getTs(); }
  287. #endif
  288. explicit mono_platform_timepoint(boost::time_max_t const& ns) : dur(ns) {}
  289. boost::time_max_t getNs() const { return dur.getNs(); }
  290. #if defined BOOST_THREAD_USES_CHRONO
  291. // This conversion assumes that chrono::steady_clock::time_point and mono_platform_timepoint share the same epoch.
  292. template <class Duration>
  293. mono_platform_timepoint(chrono::time_point<chrono::steady_clock, Duration> const& abs_time)
  294. : dur(abs_time.time_since_epoch()) {}
  295. #endif
  296. // can't name this max() since that is a macro on some Windows systems
  297. static mono_platform_timepoint getMax()
  298. {
  299. #if defined BOOST_THREAD_CHRONO_POSIX_API || defined BOOST_THREAD_CHRONO_MAC_API
  300. timespec ts;
  301. ts.tv_sec = (std::numeric_limits<time_t>::max)();
  302. ts.tv_nsec = 999999999;
  303. return mono_platform_timepoint(ts);
  304. #else
  305. boost::time_max_t ns = (std::numeric_limits<boost::time_max_t>::max)();
  306. return mono_platform_timepoint(ns);
  307. #endif
  308. }
  309. private:
  310. platform_duration dur;
  311. };
  312. inline bool operator==(mono_platform_timepoint const& lhs, mono_platform_timepoint const& rhs)
  313. {
  314. return lhs.getNs() == rhs.getNs();
  315. }
  316. inline bool operator!=(mono_platform_timepoint const& lhs, mono_platform_timepoint const& rhs)
  317. {
  318. return lhs.getNs() != rhs.getNs();
  319. }
  320. inline bool operator<(mono_platform_timepoint const& lhs, mono_platform_timepoint const& rhs)
  321. {
  322. return lhs.getNs() < rhs.getNs();
  323. }
  324. inline bool operator<=(mono_platform_timepoint const& lhs, mono_platform_timepoint const& rhs)
  325. {
  326. return lhs.getNs() <= rhs.getNs();
  327. }
  328. inline bool operator>(mono_platform_timepoint const& lhs, mono_platform_timepoint const& rhs)
  329. {
  330. return lhs.getNs() > rhs.getNs();
  331. }
  332. inline bool operator>=(mono_platform_timepoint const& lhs, mono_platform_timepoint const& rhs)
  333. {
  334. return lhs.getNs() >= rhs.getNs();
  335. }
  336. inline mono_platform_timepoint operator+(mono_platform_timepoint const& lhs, platform_duration const& rhs)
  337. {
  338. return mono_platform_timepoint(lhs.getNs() + rhs.getNs());
  339. }
  340. inline mono_platform_timepoint operator+(platform_duration const& lhs, mono_platform_timepoint const& rhs)
  341. {
  342. return mono_platform_timepoint(lhs.getNs() + rhs.getNs());
  343. }
  344. inline platform_duration operator-(mono_platform_timepoint const& lhs, mono_platform_timepoint const& rhs)
  345. {
  346. return platform_duration(lhs.getNs() - rhs.getNs());
  347. }
  348. struct mono_platform_clock
  349. {
  350. static mono_platform_timepoint now()
  351. {
  352. #if defined(BOOST_THREAD_CHRONO_WINDOWS_API)
  353. #if defined(BOOST_THREAD_USES_CHRONO)
  354. // Use QueryPerformanceCounter() to match the implementation in Boost
  355. // Chrono so that chrono::steady_clock::now() and this function share the
  356. // same epoch and so can be converted between each other.
  357. boost::winapi::LARGE_INTEGER_ freq;
  358. if ( !boost::winapi::QueryPerformanceFrequency( &freq ) )
  359. {
  360. BOOST_ASSERT(0 && "Boost::Thread - QueryPerformanceFrequency Internal Error");
  361. return mono_platform_timepoint(0);
  362. }
  363. if ( freq.QuadPart <= 0 )
  364. {
  365. BOOST_ASSERT(0 && "Boost::Thread - QueryPerformanceFrequency Internal Error");
  366. return mono_platform_timepoint(0);
  367. }
  368. boost::winapi::LARGE_INTEGER_ pcount;
  369. unsigned times=0;
  370. while ( ! boost::winapi::QueryPerformanceCounter( &pcount ) )
  371. {
  372. if ( ++times > 3 )
  373. {
  374. BOOST_ASSERT(0 && "Boost::Thread - QueryPerformanceCounter Internal Error");
  375. return mono_platform_timepoint(0);
  376. }
  377. }
  378. long double ns = 1000000000.0L * pcount.QuadPart / freq.QuadPart;
  379. return mono_platform_timepoint(static_cast<boost::time_max_t>(ns));
  380. #else
  381. // Use GetTickCount64() because it's more reliable on older
  382. // systems like Windows XP and Windows Server 2003.
  383. win32::ticks_type msec = win32::gettickcount64();
  384. return mono_platform_timepoint(msec * 1000000);
  385. #endif
  386. #elif defined(BOOST_THREAD_CHRONO_MAC_API)
  387. kern_return_t err;
  388. threads::chrono_details::FP fp = threads::chrono_details::init_steady_clock(err);
  389. if ( err != 0 )
  390. {
  391. BOOST_ASSERT(0 && "Boost::Chrono - Internal Error");
  392. }
  393. return mono_platform_timepoint(fp());
  394. #else
  395. timespec ts;
  396. if ( ::clock_gettime( CLOCK_MONOTONIC, &ts ) )
  397. {
  398. BOOST_ASSERT(0 && "Boost::Thread - clock_gettime(CLOCK_MONOTONIC) Internal Error");
  399. return mono_platform_timepoint(0);
  400. }
  401. return mono_platform_timepoint(ts);
  402. #endif
  403. }
  404. };
  405. #endif
  406. #if defined(BOOST_THREAD_INTERNAL_CLOCK_IS_MONO)
  407. typedef mono_platform_clock internal_platform_clock;
  408. typedef mono_platform_timepoint internal_platform_timepoint;
  409. #else
  410. typedef real_platform_clock internal_platform_clock;
  411. typedef real_platform_timepoint internal_platform_timepoint;
  412. #endif
  413. #ifdef BOOST_THREAD_USES_CHRONO
  414. #ifdef BOOST_THREAD_INTERNAL_CLOCK_IS_MONO
  415. typedef chrono::steady_clock internal_chrono_clock;
  416. #else
  417. typedef chrono::system_clock internal_chrono_clock;
  418. #endif
  419. #endif
  420. }
  421. }
  422. #include <boost/config/abi_suffix.hpp>
  423. #endif