Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(55)

Side by Side Diff: base/time/time_now_posix.cc

Issue 2891583002: Fuchsia port of base/time, with some refactoring of POSIX time modules. (Closed)
Patch Set: Created 3 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "base/time/time.h"
6
7 #include <stdint.h>
8 #include <sys/time.h>
9 #include <time.h>
10 #if defined(OS_ANDROID) && !defined(__LP64__)
11 #include <time64.h>
12 #endif
13 #include <unistd.h>
14
15 #include "base/logging.h"
16 #include "base/numerics/safe_math.h"
17 #include "build/build_config.h"
18
19 // Ensure the Fuchsia and Mac builds do not include this module. Instead,
20 // non-POSIX implementation is used for sampling the system clocks.
21 #if defined(OS_FUCHSIA) || defined(OS_MACOSX)
22 #error "This implementation is for POSIX platforms other than Fuchsia or Mac."
23 #endif
24
25 namespace {
26
27 int64_t ConvertTimespecToMicros(const struct timespec& ts) {
28 // On 32-bit systems, the calculation cannot overflow int64_t.
29 // 2**32 * 1000000 + 2**64 / 1000 < 2**63
30 if (sizeof(ts.tv_sec) <= 4 && sizeof(ts.tv_nsec) <= 8) {
31 int64_t result = ts.tv_sec;
32 result *= base::Time::kMicrosecondsPerSecond;
33 result += (ts.tv_nsec / base::Time::kNanosecondsPerMicrosecond);
34 return result;
35 } else {
36 base::CheckedNumeric<int64_t> result(ts.tv_sec);
37 result *= base::Time::kMicrosecondsPerSecond;
38 result += (ts.tv_nsec / base::Time::kNanosecondsPerMicrosecond);
39 return result.ValueOrDie();
40 }
41 }
42
43 // Helper function to get results from clock_gettime() and convert to a
44 // microsecond timebase. Minimum requirement is MONOTONIC_CLOCK to be supported
45 // on the system. FreeBSD 6 has CLOCK_MONOTONIC but defines
46 // _POSIX_MONOTONIC_CLOCK to -1.
47 #if (defined(OS_POSIX) && defined(_POSIX_MONOTONIC_CLOCK) && \
48 _POSIX_MONOTONIC_CLOCK >= 0) || \
49 defined(OS_BSD) || defined(OS_ANDROID)
50 int64_t ClockNow(clockid_t clk_id) {
51 struct timespec ts;
52 if (clock_gettime(clk_id, &ts) != 0) {
53 NOTREACHED() << "clock_gettime(" << clk_id << ") failed.";
54 return 0;
55 }
56 return ConvertTimespecToMicros(ts);
57 }
58 #else // _POSIX_MONOTONIC_CLOCK
59 #error No usable tick clock function on this platform.
60 #endif // _POSIX_MONOTONIC_CLOCK
61
62 } // namespace
63
64 namespace base {
65
66 // Time -----------------------------------------------------------------------
67
68 // static
69 Time Time::Now() {
70 struct timeval tv;
71 struct timezone tz = {0, 0}; // UTC
72 if (gettimeofday(&tv, &tz) != 0) {
73 DCHECK(0) << "Could not determine time of day";
74 PLOG(ERROR) << "Call to gettimeofday failed.";
75 // Return null instead of uninitialized |tv| value, which contains random
76 // garbage data. This may result in the crash seen in crbug.com/147570.
77 return Time();
78 }
79 // Combine seconds and microseconds in a 64-bit field containing microseconds
80 // since the epoch. That's enough for nearly 600 centuries. Adjust from
81 // Unix (1970) to Windows (1601) epoch.
82 return Time((tv.tv_sec * kMicrosecondsPerSecond + tv.tv_usec) +
83 kTimeTToMicrosecondsOffset);
84 }
85
86 // static
87 Time Time::NowFromSystemTime() {
88 // Just use Now() because Now() returns the system time.
89 return Now();
90 }
91
92 // TimeTicks ------------------------------------------------------------------
93
94 // static
95 TimeTicks TimeTicks::Now() {
96 return TimeTicks(ClockNow(CLOCK_MONOTONIC));
97 }
98
99 // static
100 TimeTicks::Clock TimeTicks::GetClock() {
101 return Clock::LINUX_CLOCK_MONOTONIC;
102 }
103
104 // static
105 bool TimeTicks::IsHighResolution() {
106 return true;
107 }
108
109 // static
110 bool TimeTicks::IsConsistentAcrossProcesses() {
111 return true;
112 }
113
114 // static
115 ThreadTicks ThreadTicks::Now() {
116 #if (defined(_POSIX_THREAD_CPUTIME) && (_POSIX_THREAD_CPUTIME >= 0)) || \
117 defined(OS_ANDROID)
118 return ThreadTicks(ClockNow(CLOCK_THREAD_CPUTIME_ID));
119 #else
120 NOTREACHED();
121 return ThreadTicks();
122 #endif
123 }
124
125 } // namespace base
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698