Chris@16
|
1 #ifndef BOOST_THREAD_PTHREAD_TIMESPEC_HPP
|
Chris@16
|
2 #define BOOST_THREAD_PTHREAD_TIMESPEC_HPP
|
Chris@16
|
3 // (C) Copyright 2007-8 Anthony Williams
|
Chris@16
|
4 // (C) Copyright 2012 Vicente J. Botet Escriba
|
Chris@16
|
5 //
|
Chris@16
|
6 // Distributed under the Boost Software License, Version 1.0. (See
|
Chris@16
|
7 // accompanying file LICENSE_1_0.txt or copy at
|
Chris@16
|
8 // http://www.boost.org/LICENSE_1_0.txt)
|
Chris@16
|
9
|
Chris@16
|
10 #include <boost/thread/detail/config.hpp>
|
Chris@16
|
11 #include <boost/thread/thread_time.hpp>
|
Chris@16
|
12 #if defined BOOST_THREAD_USES_DATETIME
|
Chris@16
|
13 #include <boost/date_time/posix_time/conversion.hpp>
|
Chris@16
|
14 #endif
|
Chris@16
|
15 #include <pthread.h>
|
Chris@16
|
16 #ifndef _WIN32
|
Chris@16
|
17 #include <unistd.h>
|
Chris@16
|
18 #endif
|
Chris@16
|
19 #ifdef BOOST_THREAD_USES_CHRONO
|
Chris@16
|
20 #include <boost/chrono/duration.hpp>
|
Chris@16
|
21 #endif
|
Chris@16
|
22
|
Chris@16
|
23 #if defined(macintosh) || defined(__APPLE__) || defined(__APPLE_CC__)
|
Chris@16
|
24 # define BOOST_THREAD_TIMESPEC_MAC_API
|
Chris@16
|
25 #include <sys/time.h> //for gettimeofday and timeval
|
Chris@16
|
26 #else
|
Chris@16
|
27 #include <time.h> // for clock_gettime
|
Chris@16
|
28 #endif
|
Chris@16
|
29
|
Chris@16
|
30 #include <boost/config/abi_prefix.hpp>
|
Chris@16
|
31
|
Chris@16
|
32 namespace boost
|
Chris@16
|
33 {
|
Chris@16
|
34 namespace detail
|
Chris@16
|
35 {
|
Chris@16
|
36 #if defined BOOST_THREAD_USES_DATETIME
|
Chris@16
|
37 inline struct timespec to_timespec(boost::system_time const& abs_time)
|
Chris@16
|
38 {
|
Chris@16
|
39 struct timespec timeout = { 0,0};
|
Chris@16
|
40 boost::posix_time::time_duration const time_since_epoch=abs_time-boost::posix_time::from_time_t(0);
|
Chris@16
|
41
|
Chris@16
|
42 timeout.tv_sec=time_since_epoch.total_seconds();
|
Chris@16
|
43 timeout.tv_nsec=(long)(time_since_epoch.fractional_seconds()*(1000000000l/time_since_epoch.ticks_per_second()));
|
Chris@16
|
44 return timeout;
|
Chris@16
|
45 }
|
Chris@16
|
46 #endif
|
Chris@16
|
47 #if defined BOOST_THREAD_USES_CHRONO
|
Chris@16
|
48 inline timespec to_timespec(chrono::nanoseconds const& ns)
|
Chris@16
|
49 {
|
Chris@16
|
50 struct timespec ts;
|
Chris@16
|
51 ts.tv_sec = static_cast<long>(chrono::duration_cast<chrono::seconds>(ns).count());
|
Chris@16
|
52 ts.tv_nsec = static_cast<long>((ns - chrono::duration_cast<chrono::seconds>(ns)).count());
|
Chris@16
|
53 return ts;
|
Chris@16
|
54 }
|
Chris@16
|
55
|
Chris@16
|
56 #endif
|
Chris@16
|
57
|
Chris@16
|
58 inline timespec to_timespec(boost::intmax_t const& ns)
|
Chris@16
|
59 {
|
Chris@16
|
60 boost::intmax_t s = ns / 1000000000l;
|
Chris@16
|
61 struct timespec ts;
|
Chris@16
|
62 ts.tv_sec = static_cast<long> (s);
|
Chris@16
|
63 ts.tv_nsec = static_cast<long> (ns - s * 1000000000l);
|
Chris@16
|
64 return ts;
|
Chris@16
|
65 }
|
Chris@16
|
66 inline boost::intmax_t to_nanoseconds_int_max(timespec const& ts)
|
Chris@16
|
67 {
|
Chris@16
|
68 return static_cast<boost::intmax_t>(ts.tv_sec) * 1000000000l + ts.tv_nsec;
|
Chris@16
|
69 }
|
Chris@16
|
70 inline bool timespec_ge_zero(timespec const& ts)
|
Chris@16
|
71 {
|
Chris@16
|
72 return (ts.tv_sec >= 0) || (ts.tv_nsec >= 0);
|
Chris@16
|
73 }
|
Chris@16
|
74 inline timespec timespec_now()
|
Chris@16
|
75 {
|
Chris@16
|
76 timespec ts;
|
Chris@16
|
77
|
Chris@16
|
78 #if defined(BOOST_THREAD_TIMESPEC_MAC_API)
|
Chris@16
|
79 timeval tv;
|
Chris@16
|
80 ::gettimeofday(&tv, 0);
|
Chris@16
|
81 ts.tv_sec = tv.tv_sec;
|
Chris@16
|
82 ts.tv_nsec = tv.tv_usec * 1000;
|
Chris@16
|
83 #else
|
Chris@16
|
84 if ( ::clock_gettime( CLOCK_REALTIME, &ts ) )
|
Chris@16
|
85 {
|
Chris@16
|
86 BOOST_ASSERT(0 && "Boost::Thread - Internal Error");
|
Chris@16
|
87 }
|
Chris@16
|
88 #endif
|
Chris@16
|
89 return ts;
|
Chris@16
|
90 }
|
Chris@16
|
91 inline timespec timespec_zero()
|
Chris@16
|
92 {
|
Chris@16
|
93 timespec ts;
|
Chris@16
|
94 ts.tv_sec = 0;
|
Chris@16
|
95 ts.tv_nsec = 0;
|
Chris@16
|
96 return ts;
|
Chris@16
|
97 }
|
Chris@16
|
98 inline timespec timespec_plus(timespec const& lhs, timespec const& rhs)
|
Chris@16
|
99 {
|
Chris@16
|
100 return to_timespec(to_nanoseconds_int_max(lhs) + to_nanoseconds_int_max(rhs));
|
Chris@16
|
101 }
|
Chris@16
|
102 inline timespec timespec_minus(timespec const& lhs, timespec const& rhs)
|
Chris@16
|
103 {
|
Chris@16
|
104 return to_timespec(to_nanoseconds_int_max(lhs) - to_nanoseconds_int_max(rhs));
|
Chris@16
|
105 }
|
Chris@16
|
106 inline bool timespec_gt(timespec const& lhs, timespec const& rhs)
|
Chris@16
|
107 {
|
Chris@16
|
108 return to_nanoseconds_int_max(lhs) > to_nanoseconds_int_max(rhs);
|
Chris@16
|
109 }
|
Chris@16
|
110 inline bool timespec_ge(timespec const& lhs, timespec const& rhs)
|
Chris@16
|
111 {
|
Chris@16
|
112 return to_nanoseconds_int_max(lhs) >= to_nanoseconds_int_max(rhs);
|
Chris@16
|
113 }
|
Chris@16
|
114
|
Chris@16
|
115 }
|
Chris@16
|
116 }
|
Chris@16
|
117
|
Chris@16
|
118 #include <boost/config/abi_suffix.hpp>
|
Chris@16
|
119
|
Chris@16
|
120 #endif
|