]> git.sesse.net Git - casparcg/blob - dependencies/boost/boost/chrono/detail/inlined/mac/process_cpu_clocks.hpp
Manually merged pull request #222
[casparcg] / dependencies / boost / boost / chrono / detail / inlined / mac / process_cpu_clocks.hpp
1 //  boost process_cpu_clocks.cpp  -----------------------------------------------------------//
2
3 //  Copyright Beman Dawes 1994, 2006, 2008
4 //  Copyright Vicente J. Botet Escriba 2009
5
6 //  Distributed under the Boost Software License, Version 1.0.
7 //  See http://www.boost.org/LICENSE_1_0.txt
8
9 //  See http://www.boost.org/libs/chrono for documentation.
10
11 //--------------------------------------------------------------------------------------//
12
13 #include <boost/chrono/config.hpp>
14 #include <boost/chrono/process_cpu_clocks.hpp>
15 #include <boost/assert.hpp>
16
17 #include <sys/time.h> //for gettimeofday and timeval
18 #include <sys/times.h> //for times
19 # include <unistd.h>
20
21 namespace boost
22 {
23   namespace chrono
24   {
25     namespace chrono_detail
26     {
27
28       inline long tick_factor() // multiplier to convert ticks
29       //  to nanoseconds; -1 if unknown
30       {
31         static long factor = 0;
32         if (!factor)
33         {
34           if ((factor = ::sysconf(_SC_CLK_TCK)) <= 0)
35             factor = -1;
36           else
37           {
38             BOOST_ASSERT(factor <= 1000000000l); // doesn't handle large ticks
39             factor = 1000000000l / factor; // compute factor
40             if (!factor)
41               factor = -1;
42           }
43         }
44         return factor;
45       }
46     }
47
48
49     process_real_cpu_clock::time_point process_real_cpu_clock::now() BOOST_CHRONO_NOEXCEPT
50     {
51 #if 0
52       tms tm;
53       clock_t c = ::times(&tm);
54       if (c == clock_t(-1)) // error
55       {
56         BOOST_ASSERT(0 && "Boost::Chrono - Internal Error");
57       } else
58       {
59         long factor = chrono_detail::tick_factor();
60         if (factor != -1)
61         {
62           return time_point(nanoseconds(c * factor));
63         } else
64         {
65           BOOST_ASSERT(0 && "Boost::Chrono - Internal Error");
66         }
67       }
68       return time_point();
69 #else
70       clock_t c = ::clock();
71       if (c == clock_t(-1)) // error
72       {
73         BOOST_ASSERT(0 && "Boost::Chrono - Internal Error");
74       }
75       return time_point(
76           duration(c*(1000000000l/CLOCKS_PER_SEC))
77       );
78 #endif
79     }
80
81 #if !defined BOOST_CHRONO_DONT_PROVIDE_HYBRID_ERROR_HANDLING
82     process_real_cpu_clock::time_point process_real_cpu_clock::now(system::error_code & ec)
83     {
84
85 #if 0
86       tms tm;
87       clock_t c = ::times(&tm);
88       if (c == clock_t(-1)) // error
89       {
90         if (BOOST_CHRONO_IS_THROWS(ec))
91         {
92           boost::throw_exception(system::system_error(errno, BOOST_CHRONO_SYSTEM_CATEGORY, "chrono::process_real_cpu_clock"));
93         } else
94         {
95           ec.assign(errno, BOOST_CHRONO_SYSTEM_CATEGORY);
96           return time_point();
97         }
98       } else
99       {
100         long factor = chrono_detail::tick_factor();
101         if (factor != -1)
102         {
103           if (!BOOST_CHRONO_IS_THROWS(ec))
104           {
105             ec.clear();
106           }
107           return time_point(nanoseconds(c * factor));
108         } else
109         {
110           if (BOOST_CHRONO_IS_THROWS(ec))
111           {
112             boost::throw_exception(system::system_error(errno, BOOST_CHRONO_SYSTEM_CATEGORY, "chrono::process_real_cpu_clock"));
113           } else
114           {
115             ec.assign(errno, BOOST_CHRONO_SYSTEM_CATEGORY);
116             return time_point();
117           }
118         }
119       }
120 #else
121       clock_t c = ::clock();
122       if (c == clock_t(-1)) // error
123       {
124         if (BOOST_CHRONO_IS_THROWS(ec))
125         {
126           boost::throw_exception(system::system_error(errno, BOOST_CHRONO_SYSTEM_CATEGORY, "chrono::process_real_cpu_clock"));
127         } else
128         {
129           ec.assign(errno, BOOST_CHRONO_SYSTEM_CATEGORY);
130           return time_point();
131         }
132       }
133       return time_point(
134           duration(c*(1000000000l/CLOCKS_PER_SEC))
135       );
136
137 #endif
138
139     }
140 #endif
141
142 #if !defined BOOST_CHRONO_DONT_PROVIDE_HYBRID_ERROR_HANDLING
143     process_user_cpu_clock::time_point process_user_cpu_clock::now(system::error_code & ec)
144     {
145       tms tm;
146       clock_t c = ::times(&tm);
147       if (c == clock_t(-1)) // error
148       {
149         if (BOOST_CHRONO_IS_THROWS(ec))
150         {
151           boost::throw_exception(system::system_error(errno, BOOST_CHRONO_SYSTEM_CATEGORY, "chrono::process_user_cpu_clock"));
152         } else
153         {
154           ec.assign(errno, BOOST_CHRONO_SYSTEM_CATEGORY);
155           return time_point();
156         }
157       } else
158       {
159         long factor = chrono_detail::tick_factor();
160         if (factor != -1)
161         {
162           if (!BOOST_CHRONO_IS_THROWS(ec))
163           {
164             ec.clear();
165           }
166           return time_point(nanoseconds((tm.tms_utime + tm.tms_cutime) * factor));
167         } else
168         {
169           if (BOOST_CHRONO_IS_THROWS(ec))
170           {
171             boost::throw_exception(system::system_error(errno, BOOST_CHRONO_SYSTEM_CATEGORY, "chrono::process_user_cpu_clock"));
172           } else
173           {
174             ec.assign(errno, BOOST_CHRONO_SYSTEM_CATEGORY);
175             return time_point();
176           }
177         }
178       }
179     }
180 #endif
181
182     process_user_cpu_clock::time_point process_user_cpu_clock::now() BOOST_CHRONO_NOEXCEPT
183     {
184       tms tm;
185       clock_t c = ::times(&tm);
186       if (c == clock_t(-1)) // error
187       {
188         BOOST_ASSERT(0 && "Boost::Chrono - Internal Error");
189       } else
190       {
191         long factor = chrono_detail::tick_factor();
192         if (factor != -1)
193         {
194           return time_point(nanoseconds((tm.tms_utime + tm.tms_cutime)
195               * factor));
196         } else
197         {
198           BOOST_ASSERT(0 && "Boost::Chrono - Internal Error");
199         }
200       }
201       return time_point();
202     }
203     process_system_cpu_clock::time_point process_system_cpu_clock::now() BOOST_CHRONO_NOEXCEPT
204     {
205       tms tm;
206       clock_t c = ::times(&tm);
207       if (c == clock_t(-1)) // error
208       {
209         BOOST_ASSERT(0 && "Boost::Chrono - Internal Error");
210       } else
211       {
212         long factor = chrono_detail::tick_factor();
213         if (factor != -1)
214         {
215           return time_point(nanoseconds((tm.tms_stime + tm.tms_cstime)
216               * factor));
217         } else
218         {
219           BOOST_ASSERT(0 && "Boost::Chrono - Internal Error");
220         }
221       }
222       return time_point();
223     }
224
225 #if !defined BOOST_CHRONO_DONT_PROVIDE_HYBRID_ERROR_HANDLING
226     process_system_cpu_clock::time_point process_system_cpu_clock::now(system::error_code & ec)
227     {
228       tms tm;
229       clock_t c = ::times(&tm);
230       if (c == clock_t(-1)) // error
231       {
232         if (BOOST_CHRONO_IS_THROWS(ec))
233         {
234           boost::throw_exception(system::system_error(errno, BOOST_CHRONO_SYSTEM_CATEGORY, "chrono::process_system_cpu_clock"));
235         } else
236         {
237           ec.assign(errno, BOOST_CHRONO_SYSTEM_CATEGORY);
238           return time_point();
239         }
240       } else
241       {
242         long factor = chrono_detail::tick_factor();
243         if (factor != -1)
244         {
245           if (!BOOST_CHRONO_IS_THROWS(ec))
246           {
247             ec.clear();
248           }
249           return time_point(nanoseconds((tm.tms_stime + tm.tms_cstime) * factor));
250         } else
251         {
252           if (BOOST_CHRONO_IS_THROWS(ec))
253           {
254             boost::throw_exception(system::system_error(errno, BOOST_CHRONO_SYSTEM_CATEGORY, "chrono::process_system_cpu_clock"));
255           } else
256           {
257             ec.assign(errno, BOOST_CHRONO_SYSTEM_CATEGORY);
258             return time_point();
259           }
260         }
261       }
262     }
263 #endif
264
265     process_cpu_clock::time_point process_cpu_clock::now() BOOST_CHRONO_NOEXCEPT
266     {
267       tms tm;
268       clock_t c = ::times(&tm);
269       if (c == clock_t(-1)) // error
270       {
271         BOOST_ASSERT(0 && "Boost::Chrono - Internal Error");
272       } else
273       {
274         long factor = chrono_detail::tick_factor();
275         if (factor != -1)
276         {
277           time_point::rep
278               r(c * factor, (tm.tms_utime + tm.tms_cutime) * factor, (tm.tms_stime
279                   + tm.tms_cstime) * factor);
280           return time_point(duration(r));
281         } else
282         {
283           BOOST_ASSERT(0 && "Boost::Chrono - Internal Error");
284         }
285       }
286       return time_point();
287     }
288
289 #if !defined BOOST_CHRONO_DONT_PROVIDE_HYBRID_ERROR_HANDLING
290     process_cpu_clock::time_point process_cpu_clock::now(system::error_code & ec)
291     {
292
293       tms tm;
294       clock_t c = ::times(&tm);
295       if (c == clock_t(-1)) // error
296       {
297         if (BOOST_CHRONO_IS_THROWS(ec))
298         {
299           boost::throw_exception(system::system_error(errno, BOOST_CHRONO_SYSTEM_CATEGORY, "chrono::process_clock"));
300         } else
301         {
302           ec.assign(errno, BOOST_CHRONO_SYSTEM_CATEGORY);
303           return time_point();
304         }
305       } else
306       {
307         long factor = chrono_detail::tick_factor();
308         if (factor != -1)
309         {
310           time_point::rep
311               r(c * factor, (tm.tms_utime + tm.tms_cutime) * factor, (tm.tms_stime
312                   + tm.tms_cstime) * factor);
313           return time_point(duration(r));
314         } else
315         {
316           if (BOOST_CHRONO_IS_THROWS(ec))
317           {
318             boost::throw_exception(system::system_error(errno, BOOST_CHRONO_SYSTEM_CATEGORY, "chrono::process_clock"));
319           } else
320           {
321             ec.assign(errno, BOOST_CHRONO_SYSTEM_CATEGORY);
322             return time_point();
323           }
324         }
325       }
326
327     }
328 #endif
329
330   }
331 }