OLD | NEW |
1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "base/time.h" | 5 #include "base/time.h" |
6 | 6 |
| 7 #ifdef OS_MACOSX |
| 8 #include <mach/mach_time.h> |
| 9 #endif |
7 #include <sys/time.h> | 10 #include <sys/time.h> |
8 #include <time.h> | 11 #include <time.h> |
9 | 12 |
10 #include "base/basictypes.h" | 13 #include "base/basictypes.h" |
11 #include "base/logging.h" | 14 #include "base/logging.h" |
12 | 15 |
13 namespace base { | 16 namespace base { |
14 | 17 |
15 // The Time routines in this file use standard POSIX routines, or almost- | 18 // The Time routines in this file use standard POSIX routines, or almost- |
16 // standard routines in the case of timegm. We need to use a Mach-specific | 19 // standard routines in the case of timegm. We need to use a Mach-specific |
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
82 exploded->second = timestruct.tm_sec; | 85 exploded->second = timestruct.tm_sec; |
83 exploded->millisecond = milliseconds % kMillisecondsPerSecond; | 86 exploded->millisecond = milliseconds % kMillisecondsPerSecond; |
84 } | 87 } |
85 | 88 |
86 // TimeTicks ------------------------------------------------------------------ | 89 // TimeTicks ------------------------------------------------------------------ |
87 | 90 |
88 // static | 91 // static |
89 TimeTicks TimeTicks::Now() { | 92 TimeTicks TimeTicks::Now() { |
90 uint64_t absolute_micro; | 93 uint64_t absolute_micro; |
91 | 94 |
92 #if defined(OS_POSIX) && \ | 95 #if defined(OS_MACOSX) |
| 96 static mach_timebase_info_data_t timebase_info;» |
| 97 if (timebase_info.denom == 0) {» |
| 98 // Zero-initialization of statics guarantees that denom will be 0 before» |
| 99 // calling mach_timebase_info. mach_timebase_info will never set denom to» |
| 100 // 0 as that would be invalid, so the zero-check can be used to determine» |
| 101 // whether mach_timebase_info has already been called. This is» |
| 102 // recommended by Apple's QA1398.» |
| 103 kern_return_t kr = mach_timebase_info(&timebase_info);» |
| 104 DCHECK(kr == KERN_SUCCESS);» |
| 105 }» |
| 106 |
| 107 // mach_absolute_time is it when it comes to ticks on the Mac. Other calls» |
| 108 // with less precision (such as TickCount) just call through to» |
| 109 // mach_absolute_time.» |
| 110 » |
| 111 // timebase_info converts absolute time tick units into nanoseconds. Convert» |
| 112 // to microseconds up front to stave off overflows.» |
| 113 absolute_micro = mach_absolute_time() / Time::kNanosecondsPerMicrosecond *» |
| 114 timebase_info.numer / timebase_info.denom;» |
| 115 » |
| 116 // Don't bother with the rollover handling that the Windows version does.» |
| 117 // With numer and denom = 1 (the expected case), the 64-bit absolute time» |
| 118 // reported in nanoseconds is enough to last nearly 585 years.» |
| 119 » |
| 120 #elif defined(OS_POSIX) && \ |
93 defined(_POSIX_MONOTONIC_CLOCK) && _POSIX_MONOTONIC_CLOCK >= 0 | 121 defined(_POSIX_MONOTONIC_CLOCK) && _POSIX_MONOTONIC_CLOCK >= 0 |
94 | 122 |
95 struct timespec ts; | 123 struct timespec ts; |
96 if (clock_gettime(CLOCK_MONOTONIC, &ts) != 0) { | 124 if (clock_gettime(CLOCK_MONOTONIC, &ts) != 0) { |
97 NOTREACHED() << "clock_gettime(CLOCK_MONOTONIC) failed."; | 125 NOTREACHED() << "clock_gettime(CLOCK_MONOTONIC) failed."; |
98 return TimeTicks(); | 126 return TimeTicks(); |
99 } | 127 } |
100 | 128 |
101 absolute_micro = | 129 absolute_micro = |
102 (static_cast<int64>(ts.tv_sec) * Time::kMicrosecondsPerSecond) + | 130 (static_cast<int64>(ts.tv_sec) * Time::kMicrosecondsPerSecond) + |
103 (static_cast<int64>(ts.tv_nsec) / Time::kNanosecondsPerMicrosecond); | 131 (static_cast<int64>(ts.tv_nsec) / Time::kNanosecondsPerMicrosecond); |
104 | 132 |
105 #else // _POSIX_MONOTONIC_CLOCK | 133 #else // _POSIX_MONOTONIC_CLOCK |
106 #error No usable tick clock function on this platform. | 134 #error No usable tick clock function on this platform. |
107 #endif // _POSIX_MONOTONIC_CLOCK | 135 #endif // _POSIX_MONOTONIC_CLOCK |
108 | 136 |
109 return TimeTicks(absolute_micro); | 137 return TimeTicks(absolute_micro); |
110 } | 138 } |
111 | 139 |
112 // static | 140 // static |
113 TimeTicks TimeTicks::HighResNow() { | 141 TimeTicks TimeTicks::HighResNow() { |
114 return Now(); | 142 return Now(); |
115 } | 143 } |
116 | 144 |
117 } // namespace base | 145 } // namespace base |
OLD | NEW |