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_LINUX) | 6 #if defined(TARGET_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 | 12 |
12 #include "bin/utils.h" | 13 #include "bin/utils.h" |
13 #include "platform/assert.h" | 14 #include "platform/assert.h" |
14 | 15 |
15 | 16 |
16 namespace dart { | 17 namespace dart { |
17 namespace bin { | 18 namespace bin { |
18 | 19 |
19 OSError::OSError() : sub_system_(kSystem), code_(0), message_(NULL) { | 20 OSError::OSError() : sub_system_(kSystem), code_(0), message_(NULL) { |
20 set_sub_system(kSystem); | 21 set_sub_system(kSystem); |
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
89 int64_t TimerUtils::GetCurrentTimeMicros() { | 90 int64_t TimerUtils::GetCurrentTimeMicros() { |
90 struct timeval tv; | 91 struct timeval tv; |
91 if (gettimeofday(&tv, NULL) < 0) { | 92 if (gettimeofday(&tv, NULL) < 0) { |
92 UNREACHABLE(); | 93 UNREACHABLE(); |
93 return 0; | 94 return 0; |
94 } | 95 } |
95 return (static_cast<int64_t>(tv.tv_sec) * 1000000) + tv.tv_usec; | 96 return (static_cast<int64_t>(tv.tv_sec) * 1000000) + tv.tv_usec; |
96 } | 97 } |
97 | 98 |
98 void TimerUtils::Sleep(int64_t millis) { | 99 void TimerUtils::Sleep(int64_t millis) { |
99 // We must loop here because SIGPROF will interrupt usleep. | 100 struct timespec req; // requested. |
100 int64_t micros = millis * 1000; | 101 struct timespec rem; // remainder. |
101 int64_t start = GetCurrentTimeMicros(); | 102 int64_t micros = millis * kMicrosecondsPerMillisecond; |
102 while (micros > 0) { | 103 int64_t seconds = micros / kMicrosecondsPerSecond; |
103 usleep(micros); | 104 micros = micros - seconds * kMicrosecondsPerSecond; |
104 int64_t now = GetCurrentTimeMicros(); | 105 int64_t nanos = micros * kNanosecondsPerMicrosecond; |
105 int64_t delta = now - start; | 106 req.tv_sec = seconds; |
106 ASSERT(delta >= 0); | 107 req.tv_nsec = nanos; |
107 start = now; | 108 while (true) { |
108 micros -= delta; | 109 int r = nanosleep(&req, &rem); |
| 110 if (r == 0) { |
| 111 break; |
| 112 } |
| 113 // We should only ever see an interrupt error. |
| 114 ASSERT(errno == EINTR); |
| 115 // Copy remainder into requested and repeat. |
| 116 req = rem; |
109 } | 117 } |
110 } | 118 } |
111 | 119 |
112 } // namespace bin | 120 } // namespace bin |
113 } // namespace dart | 121 } // namespace dart |
114 | 122 |
115 #endif // defined(TARGET_OS_LINUX) | 123 #endif // defined(TARGET_OS_LINUX) |
OLD | NEW |