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

Side by Side Diff: runtime/bin/utils_macos.cc

Issue 1519563003: Use a monotonic clock in the implementation of Timer. (Closed) Base URL: git@github.com:dart-lang/sdk.git@master
Patch Set: Created 5 years 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
« no previous file with comments | « runtime/bin/utils_linux.cc ('k') | runtime/bin/utils_win.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file
2 // for details. All rights reserved. Use of this source code is governed by a 2 // for details. All rights reserved. Use of this source code is governed by a
3 // BSD-style license that can be found in the LICENSE file. 3 // BSD-style license that can be found in the LICENSE file.
4 4
5 #include "platform/globals.h" 5 #include "platform/globals.h"
6 #if defined(TARGET_OS_MACOS) 6 #if defined(TARGET_OS_MACOS)
7 7
8 #include <errno.h> // NOLINT 8 #include <errno.h> // NOLINT
9 #include <netdb.h> // NOLINT 9 #include <netdb.h> // NOLINT
10 #include <mach/mach.h> // NOLINT
11 #include <mach/clock.h> // NOLINT
12 #include <mach/mach_time.h> // NOLINT
10 #include <sys/time.h> // NOLINT 13 #include <sys/time.h> // NOLINT
11 #include <time.h> // NOLINT 14 #include <time.h> // NOLINT
12 15
13 #include "bin/utils.h" 16 #include "bin/utils.h"
14 #include "platform/assert.h" 17 #include "platform/assert.h"
15 #include "platform/utils.h" 18 #include "platform/utils.h"
16 19
17 20
18 namespace dart { 21 namespace dart {
19 namespace bin { 22 namespace bin {
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after
64 char* StringUtils::Utf8ToConsoleString( 67 char* StringUtils::Utf8ToConsoleString(
65 char* utf8, intptr_t len, intptr_t* result_len) { 68 char* utf8, intptr_t len, intptr_t* result_len) {
66 UNIMPLEMENTED(); 69 UNIMPLEMENTED();
67 return NULL; 70 return NULL;
68 } 71 }
69 72
70 bool ShellUtils::GetUtf8Argv(int argc, char** argv) { 73 bool ShellUtils::GetUtf8Argv(int argc, char** argv) {
71 return false; 74 return false;
72 } 75 }
73 76
74 int64_t TimerUtils::GetCurrentTimeMilliseconds() { 77 int64_t TimerUtils::GetCurrentMonotonicMillis() {
75 return GetCurrentTimeMicros() / 1000; 78 return GetCurrentMonotonicMicros() / 1000;
76 } 79 }
77 80
78 int64_t TimerUtils::GetCurrentTimeMicros() { 81 int64_t TimerUtils::GetCurrentMonotonicMicros() {
79 struct timeval tv; 82 #if TARGET_OS_IOS
80 if (gettimeofday(&tv, NULL) < 0) { 83 // On iOS mach_absolute_time stops while the device is sleeping. Instead use
81 UNREACHABLE(); 84 // now - KERN_BOOTTIME to get a time difference that is not impacted by clock
82 return 0; 85 // changes. KERN_BOOTTIME will be updated by the system whenever the system
86 // clock change.
87 struct timeval boottime;
88 int mib[2] = {CTL_KERN, KERN_BOOTTIME};
89 size_t size = sizeof(boottime);
90 int kr = sysctl(mib, sizeof(mib) / sizeof(mib[0]), &boottime, &size, NULL, 0);
91 ASSERT(KERN_SUCCESS == kr);
92 int64_t now = GetCurrentTimeMicros();
93 int64_t origin = boottime.tv_sec * kMicrosecondsPerSecond;
94 origin += boottime.tv_usec;
95 return now - origin;
96 #else
97 static mach_timebase_info_data_t timebase_info;
98 if (timebase_info.denom == 0) {
99 // Zero-initialization of statics guarantees that denom will be 0 before
100 // calling mach_timebase_info. mach_timebase_info will never set denom to
101 // 0 as that would be invalid, so the zero-check can be used to determine
102 // whether mach_timebase_info has already been called. This is
103 // recommended by Apple's QA1398.
104 kern_return_t kr = mach_timebase_info(&timebase_info);
105 ASSERT(KERN_SUCCESS == kr);
83 } 106 }
84 return (static_cast<int64_t>(tv.tv_sec) * 1000000) + tv.tv_usec; 107
108 // timebase_info converts absolute time tick units into nanoseconds. Convert
109 // to microseconds.
110 int64_t result = mach_absolute_time() / kNanosecondsPerMicrosecond;
111 result *= timebase_info.numer;
112 result /= timebase_info.denom;
113 return result;
114 #endif // TARGET_OS_IOS
85 } 115 }
86 116
87 void TimerUtils::Sleep(int64_t millis) { 117 void TimerUtils::Sleep(int64_t millis) {
88 struct timespec req; // requested. 118 struct timespec req; // requested.
89 struct timespec rem; // remainder. 119 struct timespec rem; // remainder.
90 int64_t micros = millis * kMicrosecondsPerMillisecond; 120 int64_t micros = millis * kMicrosecondsPerMillisecond;
91 int64_t seconds = micros / kMicrosecondsPerSecond; 121 int64_t seconds = micros / kMicrosecondsPerSecond;
92 micros = micros - seconds * kMicrosecondsPerSecond; 122 micros = micros - seconds * kMicrosecondsPerSecond;
93 int64_t nanos = micros * kNanosecondsPerMicrosecond; 123 int64_t nanos = micros * kNanosecondsPerMicrosecond;
94 req.tv_sec = seconds; 124 req.tv_sec = seconds;
95 req.tv_nsec = nanos; 125 req.tv_nsec = nanos;
96 while (true) { 126 while (true) {
97 int r = nanosleep(&req, &rem); 127 int r = nanosleep(&req, &rem);
98 if (r == 0) { 128 if (r == 0) {
99 break; 129 break;
100 } 130 }
101 // We should only ever see an interrupt error. 131 // We should only ever see an interrupt error.
102 ASSERT(errno == EINTR); 132 ASSERT(errno == EINTR);
103 // Copy remainder into requested and repeat. 133 // Copy remainder into requested and repeat.
104 req = rem; 134 req = rem;
105 } 135 }
106 } 136 }
107 137
108 } // namespace bin 138 } // namespace bin
109 } // namespace dart 139 } // namespace dart
110 140
111 #endif // defined(TARGET_OS_MACOS) 141 #endif // defined(TARGET_OS_MACOS)
OLDNEW
« no previous file with comments | « runtime/bin/utils_linux.cc ('k') | runtime/bin/utils_win.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698