| OLD | NEW |
| 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 | 10 #include <mach/mach.h> // NOLINT |
| (...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 67 char* StringUtils::Utf8ToConsoleString( | 67 char* StringUtils::Utf8ToConsoleString( |
| 68 char* utf8, intptr_t len, intptr_t* result_len) { | 68 char* utf8, intptr_t len, intptr_t* result_len) { |
| 69 UNIMPLEMENTED(); | 69 UNIMPLEMENTED(); |
| 70 return NULL; | 70 return NULL; |
| 71 } | 71 } |
| 72 | 72 |
| 73 bool ShellUtils::GetUtf8Argv(int argc, char** argv) { | 73 bool ShellUtils::GetUtf8Argv(int argc, char** argv) { |
| 74 return false; | 74 return false; |
| 75 } | 75 } |
| 76 | 76 |
| 77 static mach_timebase_info_data_t timebase_info; |
| 78 |
| 77 void TimerUtils::InitOnce() { | 79 void TimerUtils::InitOnce() { |
| 80 kern_return_t kr = mach_timebase_info(&timebase_info); |
| 81 ASSERT(KERN_SUCCESS == kr); |
| 78 } | 82 } |
| 79 | 83 |
| 80 int64_t TimerUtils::GetCurrentMonotonicMillis() { | 84 int64_t TimerUtils::GetCurrentMonotonicMillis() { |
| 81 return GetCurrentMonotonicMicros() / 1000; | 85 return GetCurrentMonotonicMicros() / 1000; |
| 82 } | 86 } |
| 83 | 87 |
| 84 int64_t TimerUtils::GetCurrentMonotonicMicros() { | 88 int64_t TimerUtils::GetCurrentMonotonicMicros() { |
| 85 #if TARGET_OS_IOS | 89 #if TARGET_OS_IOS |
| 86 // On iOS mach_absolute_time stops while the device is sleeping. Instead use | 90 // On iOS mach_absolute_time stops while the device is sleeping. Instead use |
| 87 // now - KERN_BOOTTIME to get a time difference that is not impacted by clock | 91 // now - KERN_BOOTTIME to get a time difference that is not impacted by clock |
| 88 // changes. KERN_BOOTTIME will be updated by the system whenever the system | 92 // changes. KERN_BOOTTIME will be updated by the system whenever the system |
| 89 // clock change. | 93 // clock change. |
| 90 struct timeval boottime; | 94 struct timeval boottime; |
| 91 int mib[2] = {CTL_KERN, KERN_BOOTTIME}; | 95 int mib[2] = {CTL_KERN, KERN_BOOTTIME}; |
| 92 size_t size = sizeof(boottime); | 96 size_t size = sizeof(boottime); |
| 93 int kr = sysctl(mib, sizeof(mib) / sizeof(mib[0]), &boottime, &size, NULL, 0); | 97 int kr = sysctl(mib, sizeof(mib) / sizeof(mib[0]), &boottime, &size, NULL, 0); |
| 94 ASSERT(KERN_SUCCESS == kr); | 98 ASSERT(KERN_SUCCESS == kr); |
| 95 int64_t now = GetCurrentTimeMicros(); | 99 int64_t now = GetCurrentTimeMicros(); |
| 96 int64_t origin = boottime.tv_sec * kMicrosecondsPerSecond; | 100 int64_t origin = boottime.tv_sec * kMicrosecondsPerSecond; |
| 97 origin += boottime.tv_usec; | 101 origin += boottime.tv_usec; |
| 98 return now - origin; | 102 return now - origin; |
| 99 #else | 103 #else |
| 100 static mach_timebase_info_data_t timebase_info; | 104 ASSERT(timebase_info.denom != 0); |
| 101 if (timebase_info.denom == 0) { | |
| 102 // Zero-initialization of statics guarantees that denom will be 0 before | |
| 103 // calling mach_timebase_info. mach_timebase_info will never set denom to | |
| 104 // 0 as that would be invalid, so the zero-check can be used to determine | |
| 105 // whether mach_timebase_info has already been called. This is | |
| 106 // recommended by Apple's QA1398. | |
| 107 kern_return_t kr = mach_timebase_info(&timebase_info); | |
| 108 ASSERT(KERN_SUCCESS == kr); | |
| 109 } | |
| 110 | |
| 111 // timebase_info converts absolute time tick units into nanoseconds. Convert | 105 // timebase_info converts absolute time tick units into nanoseconds. Convert |
| 112 // to microseconds. | 106 // to microseconds. |
| 113 int64_t result = mach_absolute_time() / kNanosecondsPerMicrosecond; | 107 int64_t result = mach_absolute_time() / kNanosecondsPerMicrosecond; |
| 114 result *= timebase_info.numer; | 108 result *= timebase_info.numer; |
| 115 result /= timebase_info.denom; | 109 result /= timebase_info.denom; |
| 116 return result; | 110 return result; |
| 117 #endif // TARGET_OS_IOS | 111 #endif // TARGET_OS_IOS |
| 118 } | 112 } |
| 119 | 113 |
| 120 void TimerUtils::Sleep(int64_t millis) { | 114 void TimerUtils::Sleep(int64_t millis) { |
| (...skipping 14 matching lines...) Expand all Loading... |
| 135 ASSERT(errno == EINTR); | 129 ASSERT(errno == EINTR); |
| 136 // Copy remainder into requested and repeat. | 130 // Copy remainder into requested and repeat. |
| 137 req = rem; | 131 req = rem; |
| 138 } | 132 } |
| 139 } | 133 } |
| 140 | 134 |
| 141 } // namespace bin | 135 } // namespace bin |
| 142 } // namespace dart | 136 } // namespace dart |
| 143 | 137 |
| 144 #endif // defined(TARGET_OS_MACOS) | 138 #endif // defined(TARGET_OS_MACOS) |
| OLD | NEW |