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 
32 namespace boost
33 {
34   namespace detail
35   {
36 #if defined BOOST_THREAD_USES_DATETIME
to_timespec(boost::system_time const & abs_time)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
to_timespec(chrono::nanoseconds const & ns)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 
to_timespec(boost::intmax_t const & ns)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     }
to_nanoseconds_int_max(timespec const & ts)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     }
timespec_ge_zero(timespec const & ts)70     inline bool timespec_ge_zero(timespec const& ts)
71     {
72       return (ts.tv_sec >= 0) || (ts.tv_nsec >= 0);
73     }
timespec_now()74     inline timespec timespec_now()
75     {
76       timespec ts;
77 
78 #if defined(BOOST_THREAD_TIMESPEC_MAC_API)
79       timeval tv;
80       ::gettimeofday(&tv, 0);
81       ts.tv_sec = tv.tv_sec;
82       ts.tv_nsec = tv.tv_usec * 1000;
83 #else
84       if ( ::clock_gettime( CLOCK_REALTIME, &ts ) )
85       {
86         BOOST_ASSERT(0 && "Boost::Thread - Internal Error");
87       }
88 #endif
89       return ts;
90     }
timespec_zero()91     inline timespec timespec_zero()
92     {
93       timespec ts;
94       ts.tv_sec = 0;
95       ts.tv_nsec = 0;
96       return ts;
97     }
timespec_plus(timespec const & lhs,timespec const & rhs)98     inline timespec timespec_plus(timespec const& lhs, timespec const& rhs)
99     {
100       return to_timespec(to_nanoseconds_int_max(lhs) + to_nanoseconds_int_max(rhs));
101     }
timespec_minus(timespec const & lhs,timespec const & rhs)102     inline timespec timespec_minus(timespec const& lhs, timespec const& rhs)
103     {
104       return to_timespec(to_nanoseconds_int_max(lhs) - to_nanoseconds_int_max(rhs));
105     }
timespec_gt(timespec const & lhs,timespec const & rhs)106     inline bool timespec_gt(timespec const& lhs, timespec const& rhs)
107     {
108       return to_nanoseconds_int_max(lhs) > to_nanoseconds_int_max(rhs);
109     }
timespec_ge(timespec const & lhs,timespec const & rhs)110     inline bool timespec_ge(timespec const& lhs, timespec const& rhs)
111     {
112       return to_nanoseconds_int_max(lhs) >= to_nanoseconds_int_max(rhs);
113     }
114 
115   }
116 }
117 
118 #include <boost/config/abi_suffix.hpp>
119 
120 #endif
121