1#ifndef BOOST_THREAD_PTHREAD_TIMESPEC_HPP
2#define BOOST_THREAD_PTHREAD_TIMESPEC_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
10#include <boost/thread/detail/config.hpp>
11#include <boost/thread/thread_time.hpp>
12#if defined BOOST_THREAD_USES_DATETIME
13#include <boost/date_time/posix_time/conversion.hpp>
14#endif
15#include <pthread.h>
16#ifndef _WIN32
17#include <unistd.h>
18#endif
19#ifdef BOOST_THREAD_USES_CHRONO
20#include <boost/chrono/duration.hpp>
21#endif
22
23#if defined(macintosh) || defined(__APPLE__) || defined(__APPLE_CC__)
24# define BOOST_THREAD_TIMESPEC_MAC_API
25#include <sys/time.h> //for gettimeofday and timeval
26#else
27#include <time.h> // for clock_gettime
28#endif
29
30#include <boost/config/abi_prefix.hpp>
31
32namespace boost
33{
34 namespace detail
35 {
36#if defined BOOST_THREAD_USES_DATETIME
37 inline struct timespec to_timespec(boost::system_time const& abs_time)
38 {
39 struct timespec timeout = { 0,0};
40 boost::posix_time::time_duration const time_since_epoch=abs_time-boost::posix_time::from_time_t(0);
41
42 timeout.tv_sec=time_since_epoch.total_seconds();
43 timeout.tv_nsec=(long)(time_since_epoch.fractional_seconds()*(1000000000l/time_since_epoch.ticks_per_second()));
44 return timeout;
45 }
46#endif
47#if defined BOOST_THREAD_USES_CHRONO
48 inline timespec to_timespec(chrono::nanoseconds const& ns)
49 {
50 struct timespec ts;
51 ts.tv_sec = static_cast<long>(chrono::duration_cast<chrono::seconds>(ns).count());
52 ts.tv_nsec = static_cast<long>((ns - chrono::duration_cast<chrono::seconds>(ns)).count());
53 return ts;
54 }
55
56#endif
57
58 inline timespec to_timespec(boost::intmax_t const& ns)
59 {
60 boost::intmax_t s = ns / 1000000000l;
61 struct timespec ts;
62 ts.tv_sec = static_cast<long> (s);
63 ts.tv_nsec = static_cast<long> (ns - s * 1000000000l);
64 return ts;
65 }
66 inline boost::intmax_t to_nanoseconds_int_max(timespec const& ts)
67 {
68 return static_cast<boost::intmax_t>(ts.tv_sec) * 1000000000l + ts.tv_nsec;
69 }
70 inline bool timespec_ge_zero(timespec const& ts)
71 {
72 return (ts.tv_sec >= 0) || (ts.tv_nsec >= 0);
73 }
74 inline timespec timespec_now()
75 {
76 timespec ts;
77
78#if defined CLOCK_MONOTONIC && defined BOOST_THREAD_USEFIXES_TIMESPEC
79 if ( ::clock_gettime( CLOCK_MONOTONIC, &ts ) )
80 {
81 ts.tv_sec = 0;
82 ts.tv_nsec = 0;
83 BOOST_ASSERT(0 && "Boost::Thread - Internal Error");
84 }
85#elif defined(BOOST_THREAD_TIMESPEC_MAC_API)
86 timeval tv;
87 ::gettimeofday(&tv, 0);
88 ts.tv_sec = tv.tv_sec;
89 ts.tv_nsec = tv.tv_usec * 1000;
90#else
91 if ( ::clock_gettime( CLOCK_REALTIME, &ts ) )
92 {
93 ts.tv_sec = 0;
94 ts.tv_nsec = 0;
95 BOOST_ASSERT(0 && "Boost::Thread - Internal Error");
96 }
97#endif
98 return ts;
99 }
100
101 inline timespec timespec_now_realtime()
102 {
103 timespec ts;
104
105#if defined(BOOST_THREAD_TIMESPEC_MAC_API)
106 timeval tv;
107 ::gettimeofday(&tv, 0);
108 ts.tv_sec = tv.tv_sec;
109 ts.tv_nsec = tv.tv_usec * 1000;
110#else
111 if ( ::clock_gettime( CLOCK_REALTIME, &ts ) )
112 {
113 ts.tv_sec = 0;
114 ts.tv_nsec = 0;
115 BOOST_ASSERT(0 && "Boost::Thread - Internal Error");
116 }
117#endif
118 return ts;
119 }
120 inline timespec timespec_zero()
121 {
122 timespec ts;
123 ts.tv_sec = 0;
124 ts.tv_nsec = 0;
125 return ts;
126 }
127 inline timespec timespec_plus(timespec const& lhs, timespec const& rhs)
128 {
129 return to_timespec(to_nanoseconds_int_max(lhs) + to_nanoseconds_int_max(rhs));
130 }
131 inline timespec timespec_minus(timespec const& lhs, timespec const& rhs)
132 {
133 return to_timespec(to_nanoseconds_int_max(lhs) - to_nanoseconds_int_max(rhs));
134 }
135 inline bool timespec_gt(timespec const& lhs, timespec const& rhs)
136 {
137 return to_nanoseconds_int_max(lhs) > to_nanoseconds_int_max(rhs);
138 }
139 inline bool timespec_ge(timespec const& lhs, timespec const& rhs)
140 {
141 return to_nanoseconds_int_max(lhs) >= to_nanoseconds_int_max(rhs);
142 }
143
144 }
145}
146
147#include <boost/config/abi_suffix.hpp>
148
149#endif
150