]> git.proxmox.com Git - ceph.git/blame - ceph/src/boost/libs/chrono/include/boost/chrono/detail/inlined/mac/chrono.hpp
add subtree-ish sources for 12.0.3
[ceph.git] / ceph / src / boost / libs / chrono / include / boost / chrono / detail / inlined / mac / chrono.hpp
CommitLineData
7c673cae
FG
1// mac/chrono.cpp --------------------------------------------------------------//
2
3// Copyright Beman Dawes 2008
4// Copyright 2009-2010 Vicente J. Botet Escriba
5
6// Distributed under the Boost Software License, Version 1.0.
7// See http://www.boost.org/LICENSE_1_0.txt
8
9
10//----------------------------------------------------------------------------//
11// Mac //
12//----------------------------------------------------------------------------//
13
14#include <sys/time.h> //for gettimeofday and timeval
15#include <mach/mach_time.h> // mach_absolute_time, mach_timebase_info_data_t
16#include <boost/assert.hpp>
17
18namespace boost
19{
20namespace chrono
21{
22
23// system_clock
24
25// gettimeofday is the most precise "system time" available on this platform.
26// It returns the number of microseconds since New Years 1970 in a struct called timeval
27// which has a field for seconds and a field for microseconds.
28// Fill in the timeval and then convert that to the time_point
29system_clock::time_point
30system_clock::now() BOOST_NOEXCEPT
31{
32 timeval tv;
33 gettimeofday(&tv, 0);
34 return time_point(seconds(tv.tv_sec) + microseconds(tv.tv_usec));
35}
36
37#if !defined BOOST_CHRONO_DONT_PROVIDE_HYBRID_ERROR_HANDLING
38system_clock::time_point
39system_clock::now(system::error_code & ec)
40{
41 timeval tv;
42 gettimeofday(&tv, 0);
43 if (!BOOST_CHRONO_IS_THROWS(ec))
44 {
45 ec.clear();
46 }
47 return time_point(seconds(tv.tv_sec) + microseconds(tv.tv_usec));
48}
49#endif
50// Take advantage of the fact that on this platform time_t is nothing but
51// an integral count of seconds since New Years 1970 (same epoch as timeval).
52// Just get the duration out of the time_point and truncate it to seconds.
53time_t
54system_clock::to_time_t(const time_point& t) BOOST_NOEXCEPT
55{
56 return time_t(duration_cast<seconds>(t.time_since_epoch()).count());
57}
58
59// Just turn the time_t into a count of seconds and construct a time_point with it.
60system_clock::time_point
61system_clock::from_time_t(time_t t) BOOST_NOEXCEPT
62{
63 return system_clock::time_point(seconds(t));
64}
65
66namespace chrono_detail
67{
68
69// steady_clock
70
71// Note, in this implementation steady_clock and high_resolution_clock
72// are the same clock. They are both based on mach_absolute_time().
73// mach_absolute_time() * MachInfo.numer / MachInfo.denom is the number of
74// nanoseconds since the computer booted up. MachInfo.numer and MachInfo.denom
75// are run time constants supplied by the OS. This clock has no relationship
76// to the Gregorian calendar. It's main use is as a high resolution timer.
77
78// MachInfo.numer / MachInfo.denom is often 1 on the latest equipment. Specialize
79// for that case as an optimization.
80BOOST_CHRONO_STATIC
81steady_clock::rep
82steady_simplified()
83{
84 return mach_absolute_time();
85}
86
87#if !defined BOOST_CHRONO_DONT_PROVIDE_HYBRID_ERROR_HANDLING
88BOOST_CHRONO_STATIC
89steady_clock::rep
90steady_simplified_ec(system::error_code & ec)
91{
92 if (!BOOST_CHRONO_IS_THROWS(ec))
93 {
94 ec.clear();
95 }
96 return mach_absolute_time();
97}
98#endif
99
100BOOST_CHRONO_STATIC
101double
102compute_steady_factor(kern_return_t& err)
103{
104 mach_timebase_info_data_t MachInfo;
105 err = mach_timebase_info(&MachInfo);
106 if ( err != 0 ) {
107 return 0;
108 }
109 return static_cast<double>(MachInfo.numer) / MachInfo.denom;
110}
111
112BOOST_CHRONO_STATIC
113steady_clock::rep
114steady_full()
115{
116 kern_return_t err;
117 const double factor = chrono_detail::compute_steady_factor(err);
118 if (err != 0)
119 {
120 BOOST_ASSERT(0 && "Boost::Chrono - Internal Error");
121 }
122 return static_cast<steady_clock::rep>(mach_absolute_time() * factor);
123}
124
125#if !defined BOOST_CHRONO_DONT_PROVIDE_HYBRID_ERROR_HANDLING
126BOOST_CHRONO_STATIC
127steady_clock::rep
128steady_full_ec(system::error_code & ec)
129{
130 kern_return_t err;
131 const double factor = chrono_detail::compute_steady_factor(err);
132 if (err != 0)
133 {
134 if (BOOST_CHRONO_IS_THROWS(ec))
135 {
136 boost::throw_exception(
137 system::system_error(
138 err,
139 BOOST_CHRONO_SYSTEM_CATEGORY,
140 "chrono::steady_clock" ));
141 }
142 else
143 {
144 ec.assign( errno, BOOST_CHRONO_SYSTEM_CATEGORY );
145 return steady_clock::rep();
146 }
147 }
148 if (!BOOST_CHRONO_IS_THROWS(ec))
149 {
150 ec.clear();
151 }
152 return static_cast<steady_clock::rep>(mach_absolute_time() * factor);
153}
154#endif
155
156typedef steady_clock::rep (*FP)();
157#if !defined BOOST_CHRONO_DONT_PROVIDE_HYBRID_ERROR_HANDLING
158typedef steady_clock::rep (*FP_ec)(system::error_code &);
159#endif
160
161BOOST_CHRONO_STATIC
162FP
163init_steady_clock(kern_return_t & err)
164{
165 mach_timebase_info_data_t MachInfo;
166 err = mach_timebase_info(&MachInfo);
167 if ( err != 0 )
168 {
169 return 0;
170 }
171
172 if (MachInfo.numer == MachInfo.denom)
173 {
174 return &chrono_detail::steady_simplified;
175 }
176 return &chrono_detail::steady_full;
177}
178
179#if !defined BOOST_CHRONO_DONT_PROVIDE_HYBRID_ERROR_HANDLING
180BOOST_CHRONO_STATIC
181FP_ec
182init_steady_clock_ec(kern_return_t & err)
183{
184 mach_timebase_info_data_t MachInfo;
185 err = mach_timebase_info(&MachInfo);
186 if ( err != 0 )
187 {
188 return 0;
189 }
190
191 if (MachInfo.numer == MachInfo.denom)
192 {
193 return &chrono_detail::steady_simplified_ec;
194 }
195 return &chrono_detail::steady_full_ec;
196}
197#endif
198}
199
200steady_clock::time_point
201steady_clock::now() BOOST_NOEXCEPT
202{
203 kern_return_t err;
204 chrono_detail::FP fp = chrono_detail::init_steady_clock(err);
205 if ( err != 0 )
206 {
207 BOOST_ASSERT(0 && "Boost::Chrono - Internal Error");
208 }
209 return time_point(duration(fp()));
210}
211
212#if !defined BOOST_CHRONO_DONT_PROVIDE_HYBRID_ERROR_HANDLING
213steady_clock::time_point
214steady_clock::now(system::error_code & ec)
215{
216 kern_return_t err;
217 chrono_detail::FP_ec fp = chrono_detail::init_steady_clock_ec(err);
218 if ( err != 0 )
219 {
220 if (BOOST_CHRONO_IS_THROWS(ec))
221 {
222 boost::throw_exception(
223 system::system_error(
224 err,
225 BOOST_CHRONO_SYSTEM_CATEGORY,
226 "chrono::steady_clock" ));
227 }
228 else
229 {
230 ec.assign( err, BOOST_CHRONO_SYSTEM_CATEGORY );
231 return time_point();
232 }
233 }
234 if (!BOOST_CHRONO_IS_THROWS(ec))
235 {
236 ec.clear();
237 }
238 return time_point(duration(fp(ec)));
239}
240#endif
241} // namespace chrono
242} // namespace boost