| 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_LINUX) |
| 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_buf[kBufferSize]; |
| 34 | SetMessage(Utils::StrError(code, error_buf, kBufferSize)); |
| 35 | } else if (sub_system == kGetAddressInfo) { |
| 36 | SetMessage(gai_strerror(code)); |
| 37 | } else { |
| 38 | UNREACHABLE(); |
| 39 | } |
| 40 | } |
| 41 | |
| 42 | const char* StringUtils::ConsoleStringToUtf8(const char* str, |
| 43 | intptr_t len, |
| 44 | intptr_t* result_len) { |
| 45 | UNIMPLEMENTED(); |
| 46 | return NULL; |
| 47 | } |
| 48 | |
| 49 | const char* StringUtils::Utf8ToConsoleString(const char* utf8, |
| 50 | intptr_t len, |
| 51 | intptr_t* result_len) { |
| 52 | UNIMPLEMENTED(); |
| 53 | return NULL; |
| 54 | } |
| 55 | |
| 56 | char* StringUtils::ConsoleStringToUtf8(char* str, |
| 57 | intptr_t len, |
| 58 | intptr_t* result_len) { |
| 59 | UNIMPLEMENTED(); |
| 60 | return NULL; |
| 61 | } |
| 62 | |
| 63 | char* StringUtils::Utf8ToConsoleString(char* utf8, |
| 64 | intptr_t len, |
| 65 | intptr_t* result_len) { |
| 66 | UNIMPLEMENTED(); |
| 67 | return NULL; |
| 68 | } |
| 69 | |
| 70 | bool ShellUtils::GetUtf8Argv(int argc, char** argv) { |
| 71 | return false; |
| 72 | } |
| 73 | |
| 74 | void TimerUtils::InitOnce() {} |
| 75 | |
| 76 | int64_t TimerUtils::GetCurrentMonotonicMillis() { |
| 77 | return GetCurrentMonotonicMicros() / 1000; |
| 78 | } |
| 79 | |
| 80 | int64_t TimerUtils::GetCurrentMonotonicMicros() { |
| 81 | struct timespec ts; |
| 82 | if (clock_gettime(CLOCK_MONOTONIC, &ts) != 0) { |
| 83 | UNREACHABLE(); |
| 84 | return 0; |
| 85 | } |
| 86 | // Convert to microseconds. |
| 87 | int64_t result = ts.tv_sec; |
| 88 | result *= kMicrosecondsPerSecond; |
| 89 | result += (ts.tv_nsec / kNanosecondsPerMicrosecond); |
| 90 | return result; |
| 91 | } |
| 92 | |
| 93 | void TimerUtils::Sleep(int64_t millis) { |
| 94 | struct timespec req; // requested. |
| 95 | struct timespec rem; // remainder. |
| 96 | int64_t micros = millis * kMicrosecondsPerMillisecond; |
| 97 | int64_t seconds = micros / kMicrosecondsPerSecond; |
| 98 | micros = micros - seconds * kMicrosecondsPerSecond; |
| 99 | int64_t nanos = micros * kNanosecondsPerMicrosecond; |
| 100 | req.tv_sec = seconds; |
| 101 | req.tv_nsec = nanos; |
| 102 | while (true) { |
| 103 | int r = nanosleep(&req, &rem); |
| 104 | if (r == 0) { |
| 105 | break; |
| 106 | } |
| 107 | // We should only ever see an interrupt error. |
| 108 | ASSERT(errno == EINTR); |
| 109 | // Copy remainder into requested and repeat. |
| 110 | req = rem; |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | } // namespace bin |
| 115 | } // namespace dart |
| 116 | |
| 117 | #endif // defined(HOST_OS_LINUX) |
| 118 | |