| 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(HOST_OS_LINUX) | 6 #if defined(HOST_OS_LINUX) |
| 7 | 7 |
| 8 #include <errno.h> // NOLINT | 8 #include <errno.h> // NOLINT |
| 9 #include <netdb.h> // NOLINT | 9 #include <netdb.h> // NOLINT |
| 10 #include <sys/time.h> // NOLINT | 10 #include <sys/time.h> // NOLINT |
| 11 #include <time.h> // NOLINT | 11 #include <time.h> // NOLINT |
| 12 | 12 |
| 13 #include "bin/utils.h" | 13 #include "bin/utils.h" |
| 14 #include "platform/assert.h" | 14 #include "platform/assert.h" |
| 15 #include "platform/utils.h" | 15 #include "platform/utils.h" |
| 16 | 16 |
| 17 namespace dart { | 17 namespace dart { |
| 18 namespace bin { | 18 namespace bin { |
| 19 | 19 |
| 20 OSError::OSError() : sub_system_(kSystem), code_(0), message_(NULL) { | 20 OSError::OSError() : sub_system_(kSystem), code_(0), message_(NULL) { |
| 21 set_sub_system(kSystem); | 21 set_sub_system(kSystem); |
| 22 set_code(errno); | 22 set_code(errno); |
| 23 const int kBufferSize = 1024; | 23 const int kBufferSize = 1024; |
| 24 char error_buf[kBufferSize]; | 24 char error_buf[kBufferSize]; |
| 25 SetMessage(Utils::StrError(errno, error_buf, kBufferSize)); | 25 SetMessage(Utils::StrError(errno, error_buf, kBufferSize)); |
| 26 } | 26 } |
| 27 | 27 |
| 28 | |
| 29 void OSError::SetCodeAndMessage(SubSystem sub_system, int code) { | 28 void OSError::SetCodeAndMessage(SubSystem sub_system, int code) { |
| 30 set_sub_system(sub_system); | 29 set_sub_system(sub_system); |
| 31 set_code(code); | 30 set_code(code); |
| 32 if (sub_system == kSystem) { | 31 if (sub_system == kSystem) { |
| 33 const int kBufferSize = 1024; | 32 const int kBufferSize = 1024; |
| 34 char error_buf[kBufferSize]; | 33 char error_buf[kBufferSize]; |
| 35 SetMessage(Utils::StrError(code, error_buf, kBufferSize)); | 34 SetMessage(Utils::StrError(code, error_buf, kBufferSize)); |
| 36 } else if (sub_system == kGetAddressInfo) { | 35 } else if (sub_system == kGetAddressInfo) { |
| 37 SetMessage(gai_strerror(code)); | 36 SetMessage(gai_strerror(code)); |
| 38 } else { | 37 } else { |
| 39 UNREACHABLE(); | 38 UNREACHABLE(); |
| 40 } | 39 } |
| 41 } | 40 } |
| 42 | 41 |
| 43 | |
| 44 const char* StringUtils::ConsoleStringToUtf8(const char* str, | 42 const char* StringUtils::ConsoleStringToUtf8(const char* str, |
| 45 intptr_t len, | 43 intptr_t len, |
| 46 intptr_t* result_len) { | 44 intptr_t* result_len) { |
| 47 UNIMPLEMENTED(); | 45 UNIMPLEMENTED(); |
| 48 return NULL; | 46 return NULL; |
| 49 } | 47 } |
| 50 | 48 |
| 51 | |
| 52 const char* StringUtils::Utf8ToConsoleString(const char* utf8, | 49 const char* StringUtils::Utf8ToConsoleString(const char* utf8, |
| 53 intptr_t len, | 50 intptr_t len, |
| 54 intptr_t* result_len) { | 51 intptr_t* result_len) { |
| 55 UNIMPLEMENTED(); | 52 UNIMPLEMENTED(); |
| 56 return NULL; | 53 return NULL; |
| 57 } | 54 } |
| 58 | 55 |
| 59 | |
| 60 char* StringUtils::ConsoleStringToUtf8(char* str, | 56 char* StringUtils::ConsoleStringToUtf8(char* str, |
| 61 intptr_t len, | 57 intptr_t len, |
| 62 intptr_t* result_len) { | 58 intptr_t* result_len) { |
| 63 UNIMPLEMENTED(); | 59 UNIMPLEMENTED(); |
| 64 return NULL; | 60 return NULL; |
| 65 } | 61 } |
| 66 | 62 |
| 67 | |
| 68 char* StringUtils::Utf8ToConsoleString(char* utf8, | 63 char* StringUtils::Utf8ToConsoleString(char* utf8, |
| 69 intptr_t len, | 64 intptr_t len, |
| 70 intptr_t* result_len) { | 65 intptr_t* result_len) { |
| 71 UNIMPLEMENTED(); | 66 UNIMPLEMENTED(); |
| 72 return NULL; | 67 return NULL; |
| 73 } | 68 } |
| 74 | 69 |
| 75 | |
| 76 char* StringUtils::StrNDup(const char* s, intptr_t n) { | 70 char* StringUtils::StrNDup(const char* s, intptr_t n) { |
| 77 return strndup(s, n); | 71 return strndup(s, n); |
| 78 } | 72 } |
| 79 | 73 |
| 80 | |
| 81 bool ShellUtils::GetUtf8Argv(int argc, char** argv) { | 74 bool ShellUtils::GetUtf8Argv(int argc, char** argv) { |
| 82 return false; | 75 return false; |
| 83 } | 76 } |
| 84 | 77 |
| 85 | |
| 86 void TimerUtils::InitOnce() {} | 78 void TimerUtils::InitOnce() {} |
| 87 | 79 |
| 88 | |
| 89 int64_t TimerUtils::GetCurrentMonotonicMillis() { | 80 int64_t TimerUtils::GetCurrentMonotonicMillis() { |
| 90 return GetCurrentMonotonicMicros() / 1000; | 81 return GetCurrentMonotonicMicros() / 1000; |
| 91 } | 82 } |
| 92 | 83 |
| 93 | |
| 94 int64_t TimerUtils::GetCurrentMonotonicMicros() { | 84 int64_t TimerUtils::GetCurrentMonotonicMicros() { |
| 95 struct timespec ts; | 85 struct timespec ts; |
| 96 if (clock_gettime(CLOCK_MONOTONIC, &ts) != 0) { | 86 if (clock_gettime(CLOCK_MONOTONIC, &ts) != 0) { |
| 97 UNREACHABLE(); | 87 UNREACHABLE(); |
| 98 return 0; | 88 return 0; |
| 99 } | 89 } |
| 100 // Convert to microseconds. | 90 // Convert to microseconds. |
| 101 int64_t result = ts.tv_sec; | 91 int64_t result = ts.tv_sec; |
| 102 result *= kMicrosecondsPerSecond; | 92 result *= kMicrosecondsPerSecond; |
| 103 result += (ts.tv_nsec / kNanosecondsPerMicrosecond); | 93 result += (ts.tv_nsec / kNanosecondsPerMicrosecond); |
| 104 return result; | 94 return result; |
| 105 } | 95 } |
| 106 | 96 |
| 107 | |
| 108 void TimerUtils::Sleep(int64_t millis) { | 97 void TimerUtils::Sleep(int64_t millis) { |
| 109 struct timespec req; // requested. | 98 struct timespec req; // requested. |
| 110 struct timespec rem; // remainder. | 99 struct timespec rem; // remainder. |
| 111 int64_t micros = millis * kMicrosecondsPerMillisecond; | 100 int64_t micros = millis * kMicrosecondsPerMillisecond; |
| 112 int64_t seconds = micros / kMicrosecondsPerSecond; | 101 int64_t seconds = micros / kMicrosecondsPerSecond; |
| 113 micros = micros - seconds * kMicrosecondsPerSecond; | 102 micros = micros - seconds * kMicrosecondsPerSecond; |
| 114 int64_t nanos = micros * kNanosecondsPerMicrosecond; | 103 int64_t nanos = micros * kNanosecondsPerMicrosecond; |
| 115 req.tv_sec = seconds; | 104 req.tv_sec = seconds; |
| 116 req.tv_nsec = nanos; | 105 req.tv_nsec = nanos; |
| 117 while (true) { | 106 while (true) { |
| 118 int r = nanosleep(&req, &rem); | 107 int r = nanosleep(&req, &rem); |
| 119 if (r == 0) { | 108 if (r == 0) { |
| 120 break; | 109 break; |
| 121 } | 110 } |
| 122 // We should only ever see an interrupt error. | 111 // We should only ever see an interrupt error. |
| 123 ASSERT(errno == EINTR); | 112 ASSERT(errno == EINTR); |
| 124 // Copy remainder into requested and repeat. | 113 // Copy remainder into requested and repeat. |
| 125 req = rem; | 114 req = rem; |
| 126 } | 115 } |
| 127 } | 116 } |
| 128 | 117 |
| 129 } // namespace bin | 118 } // namespace bin |
| 130 } // namespace dart | 119 } // namespace dart |
| 131 | 120 |
| 132 #endif // defined(HOST_OS_LINUX) | 121 #endif // defined(HOST_OS_LINUX) |
| OLD | NEW |