1//===------------------------- chrono.cpp ---------------------------------===//
2//
3//                     The LLVM Compiler Infrastructure
4//
5// This file is dual licensed under the MIT and the University of Illinois Open
6// Source Licenses. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#include "chrono"
11#include <sys/time.h>        //for gettimeofday and timeval
12#ifdef __APPLE__
13#include <mach/mach_time.h>  // mach_absolute_time, mach_timebase_info_data_t
14#else  /* !__APPLE__ */
15#include <cerrno>  // errno
16#include <system_error>  // __throw_system_error
17#include <time.h>  // clock_gettime, CLOCK_MONOTONIC
18#endif  // __APPLE__
19
20_LIBCPP_BEGIN_NAMESPACE_STD
21
22namespace chrono
23{
24
25// system_clock
26
27const bool system_clock::is_steady;
28
29system_clock::time_point
30system_clock::now() _NOEXCEPT
31{
32    timeval tv;
33    gettimeofday(&tv, 0);
34    return time_point(seconds(tv.tv_sec) + microseconds(tv.tv_usec));
35}
36
37time_t
38system_clock::to_time_t(const time_point& t) _NOEXCEPT
39{
40    return time_t(duration_cast<seconds>(t.time_since_epoch()).count());
41}
42
43system_clock::time_point
44system_clock::from_time_t(time_t t) _NOEXCEPT
45{
46    return system_clock::time_point(seconds(t));
47}
48
49#ifndef _LIBCPP_HAS_NO_MONOTONIC_CLOCK
50// steady_clock
51
52const bool steady_clock::is_steady;
53
54#ifdef __APPLE__
55//   mach_absolute_time() * MachInfo.numer / MachInfo.denom is the number of
56//   nanoseconds since the computer booted up.  MachInfo.numer and MachInfo.denom
57//   are run time constants supplied by the OS.  This clock has no relationship
58//   to the Gregorian calendar.  It's main use is as a high resolution timer.
59
60// MachInfo.numer / MachInfo.denom is often 1 on the latest equipment.  Specialize
61//   for that case as an optimization.
62
63#pragma GCC visibility push(hidden)
64
65static
66steady_clock::rep
67steady_simplified()
68{
69    return static_cast<steady_clock::rep>(mach_absolute_time());
70}
71
72static
73double
74compute_steady_factor()
75{
76    mach_timebase_info_data_t MachInfo;
77    mach_timebase_info(&MachInfo);
78    return static_cast<double>(MachInfo.numer) / MachInfo.denom;
79}
80
81static
82steady_clock::rep
83steady_full()
84{
85    static const double factor = compute_steady_factor();
86    return static_cast<steady_clock::rep>(mach_absolute_time() * factor);
87}
88
89typedef steady_clock::rep (*FP)();
90
91static
92FP
93init_steady_clock()
94{
95    mach_timebase_info_data_t MachInfo;
96    mach_timebase_info(&MachInfo);
97    if (MachInfo.numer == MachInfo.denom)
98        return &steady_simplified;
99    return &steady_full;
100}
101
102#pragma GCC visibility pop
103
104steady_clock::time_point
105steady_clock::now() _NOEXCEPT
106{
107    static FP fp = init_steady_clock();
108    return time_point(duration(fp()));
109}
110
111#else  // __APPLE__
112// FIXME: if _LIBCPP_HAS_NO_MONOTONIC_CLOCK, then clock_gettime isn't going to
113// work. It may be possible to fall back on something else, depending on the system.
114
115// Warning:  If this is not truly steady, then it is non-conforming.  It is
116//  better for it to not exist and have the rest of libc++ use system_clock
117//  instead.
118
119steady_clock::time_point
120steady_clock::now() _NOEXCEPT
121{
122    struct timespec tp;
123    if (0 != clock_gettime(CLOCK_MONOTONIC, &tp))
124        __throw_system_error(errno, "clock_gettime(CLOCK_MONOTONIC) failed");
125    return time_point(seconds(tp.tv_sec) + nanoseconds(tp.tv_nsec));
126}
127#endif  // __APPLE__
128
129#endif // !_LIBCPP_HAS_NO_MONOTONIC_CLOCK
130
131}
132
133_LIBCPP_END_NAMESPACE_STD
134