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