]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/boost/thread/pthread/timespec.hpp
update sources to v12.2.3
[ceph.git] / ceph / src / boost / boost / thread / pthread / timespec.hpp
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
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 #if defined BOOST_THREAD_HAS_CONDATTR_SET_CLOCK_MONOTONIC
75
76 inline timespec timespec_now_monotonic()
77 {
78 timespec ts;
79
80 if ( ::clock_gettime( CLOCK_MONOTONIC, &ts ) )
81 {
82 ts.tv_sec = 0;
83 ts.tv_nsec = 0;
84 BOOST_ASSERT(0 && "Boost::Thread - Internal Error");
85 }
86 return ts;
87 }
88 #endif
89
90 inline timespec timespec_now_realtime()
91 {
92 timespec ts;
93
94 #if defined(BOOST_THREAD_TIMESPEC_MAC_API)
95 timeval tv;
96 ::gettimeofday(&tv, 0);
97 ts.tv_sec = tv.tv_sec;
98 ts.tv_nsec = tv.tv_usec * 1000;
99 #else
100 if ( ::clock_gettime( CLOCK_REALTIME, &ts ) )
101 {
102 ts.tv_sec = 0;
103 ts.tv_nsec = 0;
104 BOOST_ASSERT(0 && "Boost::Thread - Internal Error");
105 }
106 #endif
107 return ts;
108 }
109 inline timespec timespec_zero()
110 {
111 timespec ts;
112 ts.tv_sec = 0;
113 ts.tv_nsec = 0;
114 return ts;
115 }
116 inline timespec timespec_plus(timespec const& lhs, timespec const& rhs)
117 {
118 return to_timespec(to_nanoseconds_int_max(lhs) + to_nanoseconds_int_max(rhs));
119 }
120 inline timespec timespec_minus(timespec const& lhs, timespec const& rhs)
121 {
122 return to_timespec(to_nanoseconds_int_max(lhs) - to_nanoseconds_int_max(rhs));
123 }
124 inline bool timespec_gt(timespec const& lhs, timespec const& rhs)
125 {
126 return to_nanoseconds_int_max(lhs) > to_nanoseconds_int_max(rhs);
127 }
128 inline bool timespec_ge(timespec const& lhs, timespec const& rhs)
129 {
130 return to_nanoseconds_int_max(lhs) >= to_nanoseconds_int_max(rhs);
131 }
132
133 }
134 }
135
136 #include <boost/config/abi_suffix.hpp>
137
138 #endif