| 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 <mach/clock.h> // NOLINT |
| 10 #include <mach/mach.h> // NOLINT |
| 11 #include <mach/mach_time.h> // NOLINT |
| 9 #include <netdb.h> // NOLINT | 12 #include <netdb.h> // NOLINT |
| 10 #include <mach/mach.h> // NOLINT | |
| 11 #include <mach/clock.h> // NOLINT | |
| 12 #include <mach/mach_time.h> // NOLINT | |
| 13 #include <sys/time.h> // NOLINT | |
| 14 #include <time.h> // NOLINT | |
| 15 | |
| 16 #if TARGET_OS_IOS | 13 #if TARGET_OS_IOS |
| 17 #include <sys/sysctl.h> // NOLINT | 14 #include <sys/sysctl.h> // NOLINT |
| 18 #endif | 15 #endif |
| 16 #include <sys/time.h> // NOLINT |
| 17 #include <time.h> // NOLINT |
| 19 | 18 |
| 20 #include "bin/utils.h" | 19 #include "bin/utils.h" |
| 21 #include "platform/assert.h" | 20 #include "platform/assert.h" |
| 22 #include "platform/utils.h" | 21 #include "platform/utils.h" |
| 23 | 22 |
| 24 | |
| 25 namespace dart { | 23 namespace dart { |
| 26 namespace bin { | 24 namespace bin { |
| 27 | 25 |
| 28 OSError::OSError() : sub_system_(kSystem), code_(0), message_(NULL) { | 26 OSError::OSError() : sub_system_(kSystem), code_(0), message_(NULL) { |
| 29 set_sub_system(kSystem); | 27 set_sub_system(kSystem); |
| 30 set_code(errno); | 28 set_code(errno); |
| 31 const int kBufferSize = 1024; | 29 const int kBufferSize = 1024; |
| 32 char error_message[kBufferSize]; | 30 char error_message[kBufferSize]; |
| 33 Utils::StrError(errno, error_message, kBufferSize); | 31 Utils::StrError(errno, error_message, kBufferSize); |
| 34 SetMessage(error_message); | 32 SetMessage(error_message); |
| 35 } | 33 } |
| 36 | 34 |
| 37 | 35 |
| 38 void OSError::SetCodeAndMessage(SubSystem sub_system, int code) { | 36 void OSError::SetCodeAndMessage(SubSystem sub_system, int code) { |
| 39 set_sub_system(sub_system); | 37 set_sub_system(sub_system); |
| 40 set_code(code); | 38 set_code(code); |
| 41 if (sub_system == kSystem) { | 39 if (sub_system == kSystem) { |
| 42 const int kBufferSize = 1024; | 40 const int kBufferSize = 1024; |
| 43 char error_message[kBufferSize]; | 41 char error_message[kBufferSize]; |
| 44 Utils::StrError(code, error_message, kBufferSize); | 42 Utils::StrError(code, error_message, kBufferSize); |
| 45 SetMessage(error_message); | 43 SetMessage(error_message); |
| 46 } else if (sub_system == kGetAddressInfo) { | 44 } else if (sub_system == kGetAddressInfo) { |
| 47 SetMessage(gai_strerror(code)); | 45 SetMessage(gai_strerror(code)); |
| 48 } else { | 46 } else { |
| 49 UNREACHABLE(); | 47 UNREACHABLE(); |
| 50 } | 48 } |
| 51 } | 49 } |
| 52 | 50 |
| 51 |
| 53 const char* StringUtils::ConsoleStringToUtf8( | 52 const char* StringUtils::ConsoleStringToUtf8( |
| 54 const char* str, intptr_t len, intptr_t* result_len) { | 53 const char* str, intptr_t len, intptr_t* result_len) { |
| 55 UNIMPLEMENTED(); | 54 UNIMPLEMENTED(); |
| 56 return NULL; | 55 return NULL; |
| 57 } | 56 } |
| 58 | 57 |
| 58 |
| 59 const char* StringUtils::Utf8ToConsoleString( | 59 const char* StringUtils::Utf8ToConsoleString( |
| 60 const char* utf8, intptr_t len, intptr_t* result_len) { | 60 const char* utf8, intptr_t len, intptr_t* result_len) { |
| 61 UNIMPLEMENTED(); | 61 UNIMPLEMENTED(); |
| 62 return NULL; | 62 return NULL; |
| 63 } | 63 } |
| 64 | 64 |
| 65 |
| 65 char* StringUtils::ConsoleStringToUtf8( | 66 char* StringUtils::ConsoleStringToUtf8( |
| 66 char* str, intptr_t len, intptr_t* result_len) { | 67 char* str, intptr_t len, intptr_t* result_len) { |
| 67 UNIMPLEMENTED(); | 68 UNIMPLEMENTED(); |
| 68 return NULL; | 69 return NULL; |
| 69 } | 70 } |
| 70 | 71 |
| 72 |
| 71 char* StringUtils::Utf8ToConsoleString( | 73 char* StringUtils::Utf8ToConsoleString( |
| 72 char* utf8, intptr_t len, intptr_t* result_len) { | 74 char* utf8, intptr_t len, intptr_t* result_len) { |
| 73 UNIMPLEMENTED(); | 75 UNIMPLEMENTED(); |
| 74 return NULL; | 76 return NULL; |
| 75 } | 77 } |
| 76 | 78 |
| 79 |
| 77 bool ShellUtils::GetUtf8Argv(int argc, char** argv) { | 80 bool ShellUtils::GetUtf8Argv(int argc, char** argv) { |
| 78 return false; | 81 return false; |
| 79 } | 82 } |
| 80 | 83 |
| 84 |
| 81 static mach_timebase_info_data_t timebase_info; | 85 static mach_timebase_info_data_t timebase_info; |
| 82 | 86 |
| 87 |
| 83 void TimerUtils::InitOnce() { | 88 void TimerUtils::InitOnce() { |
| 84 kern_return_t kr = mach_timebase_info(&timebase_info); | 89 kern_return_t kr = mach_timebase_info(&timebase_info); |
| 85 ASSERT(KERN_SUCCESS == kr); | 90 ASSERT(KERN_SUCCESS == kr); |
| 86 } | 91 } |
| 87 | 92 |
| 93 |
| 88 int64_t TimerUtils::GetCurrentMonotonicMillis() { | 94 int64_t TimerUtils::GetCurrentMonotonicMillis() { |
| 89 return GetCurrentMonotonicMicros() / 1000; | 95 return GetCurrentMonotonicMicros() / 1000; |
| 90 } | 96 } |
| 91 | 97 |
| 98 |
| 92 #if TARGET_OS_IOS | 99 #if TARGET_OS_IOS |
| 93 | |
| 94 static int64_t GetCurrentTimeMicros() { | 100 static int64_t GetCurrentTimeMicros() { |
| 95 // gettimeofday has microsecond resolution. | 101 // gettimeofday has microsecond resolution. |
| 96 struct timeval tv; | 102 struct timeval tv; |
| 97 if (gettimeofday(&tv, NULL) < 0) { | 103 if (gettimeofday(&tv, NULL) < 0) { |
| 98 UNREACHABLE(); | 104 UNREACHABLE(); |
| 99 return 0; | 105 return 0; |
| 100 } | 106 } |
| 101 return (static_cast<int64_t>(tv.tv_sec) * 1000000) + tv.tv_usec; | 107 return (static_cast<int64_t>(tv.tv_sec) * 1000000) + tv.tv_usec; |
| 102 } | 108 } |
| 109 #endif // TARGET_OS_IOS |
| 103 | 110 |
| 104 #endif // TARGET_OS_IOS | |
| 105 | 111 |
| 106 int64_t TimerUtils::GetCurrentMonotonicMicros() { | 112 int64_t TimerUtils::GetCurrentMonotonicMicros() { |
| 107 #if TARGET_OS_IOS | 113 #if TARGET_OS_IOS |
| 108 // On iOS mach_absolute_time stops while the device is sleeping. Instead use | 114 // On iOS mach_absolute_time stops while the device is sleeping. Instead use |
| 109 // now - KERN_BOOTTIME to get a time difference that is not impacted by clock | 115 // now - KERN_BOOTTIME to get a time difference that is not impacted by clock |
| 110 // changes. KERN_BOOTTIME will be updated by the system whenever the system | 116 // changes. KERN_BOOTTIME will be updated by the system whenever the system |
| 111 // clock change. | 117 // clock change. |
| 112 struct timeval boottime; | 118 struct timeval boottime; |
| 113 int mib[2] = {CTL_KERN, KERN_BOOTTIME}; | 119 int mib[2] = {CTL_KERN, KERN_BOOTTIME}; |
| 114 size_t size = sizeof(boottime); | 120 size_t size = sizeof(boottime); |
| 115 int kr = sysctl(mib, sizeof(mib) / sizeof(mib[0]), &boottime, &size, NULL, 0); | 121 int kr = sysctl(mib, sizeof(mib) / sizeof(mib[0]), &boottime, &size, NULL, 0); |
| 116 ASSERT(KERN_SUCCESS == kr); | 122 ASSERT(KERN_SUCCESS == kr); |
| 117 int64_t now = GetCurrentTimeMicros(); | 123 int64_t now = GetCurrentTimeMicros(); |
| 118 int64_t origin = boottime.tv_sec * kMicrosecondsPerSecond; | 124 int64_t origin = boottime.tv_sec * kMicrosecondsPerSecond; |
| 119 origin += boottime.tv_usec; | 125 origin += boottime.tv_usec; |
| 120 return now - origin; | 126 return now - origin; |
| 121 #else | 127 #else |
| 122 ASSERT(timebase_info.denom != 0); | 128 ASSERT(timebase_info.denom != 0); |
| 123 // timebase_info converts absolute time tick units into nanoseconds. Convert | 129 // timebase_info converts absolute time tick units into nanoseconds. Convert |
| 124 // to microseconds. | 130 // to microseconds. |
| 125 int64_t result = mach_absolute_time() / kNanosecondsPerMicrosecond; | 131 int64_t result = mach_absolute_time() / kNanosecondsPerMicrosecond; |
| 126 result *= timebase_info.numer; | 132 result *= timebase_info.numer; |
| 127 result /= timebase_info.denom; | 133 result /= timebase_info.denom; |
| 128 return result; | 134 return result; |
| 129 #endif // TARGET_OS_IOS | 135 #endif // TARGET_OS_IOS |
| 130 } | 136 } |
| 131 | 137 |
| 138 |
| 132 void TimerUtils::Sleep(int64_t millis) { | 139 void TimerUtils::Sleep(int64_t millis) { |
| 133 struct timespec req; // requested. | 140 struct timespec req; // requested. |
| 134 struct timespec rem; // remainder. | 141 struct timespec rem; // remainder. |
| 135 int64_t micros = millis * kMicrosecondsPerMillisecond; | 142 int64_t micros = millis * kMicrosecondsPerMillisecond; |
| 136 int64_t seconds = micros / kMicrosecondsPerSecond; | 143 int64_t seconds = micros / kMicrosecondsPerSecond; |
| 137 micros = micros - seconds * kMicrosecondsPerSecond; | 144 micros = micros - seconds * kMicrosecondsPerSecond; |
| 138 int64_t nanos = micros * kNanosecondsPerMicrosecond; | 145 int64_t nanos = micros * kNanosecondsPerMicrosecond; |
| 139 req.tv_sec = seconds; | 146 req.tv_sec = seconds; |
| 140 req.tv_nsec = nanos; | 147 req.tv_nsec = nanos; |
| 141 while (true) { | 148 while (true) { |
| 142 int r = nanosleep(&req, &rem); | 149 int r = nanosleep(&req, &rem); |
| 143 if (r == 0) { | 150 if (r == 0) { |
| 144 break; | 151 break; |
| 145 } | 152 } |
| 146 // We should only ever see an interrupt error. | 153 // We should only ever see an interrupt error. |
| 147 ASSERT(errno == EINTR); | 154 ASSERT(errno == EINTR); |
| 148 // Copy remainder into requested and repeat. | 155 // Copy remainder into requested and repeat. |
| 149 req = rem; | 156 req = rem; |
| 150 } | 157 } |
| 151 } | 158 } |
| 152 | 159 |
| 153 } // namespace bin | 160 } // namespace bin |
| 154 } // namespace dart | 161 } // namespace dart |
| 155 | 162 |
| 156 #endif // defined(TARGET_OS_MACOS) | 163 #endif // defined(TARGET_OS_MACOS) |
| OLD | NEW |