blob: c3714208b5ca42d3d8789d549f0044327848c186 [file] [log] [blame]
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001//===------------------------- chrono.cpp ---------------------------------===//
2//
Howard Hinnantf5256e12010-05-11 21:36:01 +00003// The LLVM Compiler Infrastructure
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004//
Howard Hinnantb64f8b02010-11-16 22:09:02 +00005// This file is dual licensed under the MIT and the University of Illinois Open
6// Source Licenses. See LICENSE.TXT for details.
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00007//
8//===----------------------------------------------------------------------===//
9
10#include "chrono"
11#include <sys/time.h> //for gettimeofday and timeval
Howard Hinnantadff4892010-05-24 17:49:41 +000012#if __APPLE__
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000013#include <mach/mach_time.h> // mach_absolute_time, mach_timebase_info_data_t
Howard Hinnantadff4892010-05-24 17:49:41 +000014#else /* !__APPLE__ */
15#include <cerrno> // errno
16#include <system_error> // __throw_system_error
17#include <time.h> // clock_gettime, CLOCK_MONOTONIC
Howard Hinnant16e6e1d2010-08-22 00:03:27 +000018#endif // __APPLE__
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000019
20_LIBCPP_BEGIN_NAMESPACE_STD
21
22namespace chrono
23{
24
25// system_clock
26
27system_clock::time_point
28system_clock::now()
29{
30 timeval tv;
31 gettimeofday(&tv, 0);
32 return time_point(seconds(tv.tv_sec) + microseconds(tv.tv_usec));
33}
34
35time_t
36system_clock::to_time_t(const time_point& t)
37{
38 return time_t(duration_cast<seconds>(t.time_since_epoch()).count());
39}
40
41system_clock::time_point
42system_clock::from_time_t(time_t t)
43{
44 return system_clock::time_point(seconds(t));
45}
46
Howard Hinnantf8f85212010-11-20 19:16:30 +000047// steady_clock
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000048
Howard Hinnantadff4892010-05-24 17:49:41 +000049#if __APPLE__
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000050// mach_absolute_time() * MachInfo.numer / MachInfo.denom is the number of
51// nanoseconds since the computer booted up. MachInfo.numer and MachInfo.denom
52// are run time constants supplied by the OS. This clock has no relationship
53// to the Gregorian calendar. It's main use is as a high resolution timer.
54
55// MachInfo.numer / MachInfo.denom is often 1 on the latest equipment. Specialize
56// for that case as an optimization.
57
58#pragma GCC visibility push(hidden)
59
60static
Howard Hinnantf8f85212010-11-20 19:16:30 +000061steady_clock::rep
62steady_simplified()
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000063{
64 return mach_absolute_time();
65}
66
67static
68double
Howard Hinnantf8f85212010-11-20 19:16:30 +000069compute_steady_factor()
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000070{
71 mach_timebase_info_data_t MachInfo;
72 mach_timebase_info(&MachInfo);
73 return static_cast<double>(MachInfo.numer) / MachInfo.denom;
74}
75
76static
Howard Hinnantf8f85212010-11-20 19:16:30 +000077steady_clock::rep
78steady_full()
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000079{
Howard Hinnantf8f85212010-11-20 19:16:30 +000080 static const double factor = compute_steady_factor();
81 return static_cast<steady_clock::rep>(mach_absolute_time() * factor);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000082}
83
Howard Hinnantf8f85212010-11-20 19:16:30 +000084typedef steady_clock::rep (*FP)();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000085
86static
87FP
Howard Hinnantf8f85212010-11-20 19:16:30 +000088init_steady_clock()
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000089{
90 mach_timebase_info_data_t MachInfo;
91 mach_timebase_info(&MachInfo);
92 if (MachInfo.numer == MachInfo.denom)
Howard Hinnantf8f85212010-11-20 19:16:30 +000093 return &steady_simplified;
94 return &steady_full;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000095}
96
Daniel Dunbar04acaca2010-09-04 03:15:51 +000097#pragma GCC visibility pop
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000098
Howard Hinnantf8f85212010-11-20 19:16:30 +000099steady_clock::time_point
100steady_clock::now()
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000101{
Howard Hinnantf8f85212010-11-20 19:16:30 +0000102 static FP fp = init_steady_clock();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000103 return time_point(duration(fp()));
104}
105
Howard Hinnant16e6e1d2010-08-22 00:03:27 +0000106#else // __APPLE__
Howard Hinnantadff4892010-05-24 17:49:41 +0000107// FIXME: We assume that clock_gettime(CLOCK_MONOTONIC) works on
108// non-apple systems. Instead, we should check _POSIX_TIMERS and
109// _POSIX_MONOTONIC_CLOCK and fall back to something else if those
110// don't exist.
111
Howard Hinnantf8f85212010-11-20 19:16:30 +0000112// Warning: If this is not truly steady, then it is non-conforming. It is
Howard Hinnantadff4892010-05-24 17:49:41 +0000113// better for it to not exist and have the rest of libc++ use system_clock
114// instead.
115
Howard Hinnantf8f85212010-11-20 19:16:30 +0000116steady_clock::time_point
117steady_clock::now()
Howard Hinnantadff4892010-05-24 17:49:41 +0000118{
119 struct timespec tp;
120 if (0 != clock_gettime(CLOCK_MONOTONIC, &tp))
121 __throw_system_error(errno, "clock_gettime(CLOCK_MONOTONIC) failed");
122 return time_point(seconds(tp.tv_sec) + nanoseconds(tp.tv_nsec));
123}
Howard Hinnant16e6e1d2010-08-22 00:03:27 +0000124#endif // __APPLE__
Howard Hinnantadff4892010-05-24 17:49:41 +0000125
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000126}
127
128_LIBCPP_END_NAMESPACE_STD