Index: runtime/bin/utils_android.cc |
diff --git a/runtime/bin/utils_android.cc b/runtime/bin/utils_android.cc |
index e88d248eacb9839179e4d10d29126384011d5cb8..615dae966a2bdc151852484e8cdd330f9f33b4e9 100644 |
--- a/runtime/bin/utils_android.cc |
+++ b/runtime/bin/utils_android.cc |
@@ -8,6 +8,7 @@ |
#include <errno.h> // NOLINT |
#include <netdb.h> // NOLINT |
#include <sys/time.h> // NOLINT |
+#include <time.h> // NOLINT |
#include "bin/utils.h" |
#include "platform/assert.h" |
@@ -98,7 +99,24 @@ int64_t TimerUtils::GetCurrentTimeMicros() { |
} |
void TimerUtils::Sleep(int64_t millis) { |
- usleep(millis * 1000); |
+ struct timespec req; // requested. |
+ struct timespec rem; // remainder. |
+ int64_t micros = millis * kMicrosecondsPerMillisecond; |
+ int64_t seconds = micros / kMicrosecondsPerSecond; |
+ micros = micros - seconds * kMicrosecondsPerSecond; |
+ int64_t nanos = micros * kNanosecondsPerMicrosecond; |
+ req.tv_sec = seconds; |
+ req.tv_nsec = nanos; |
+ while (true) { |
+ int r = nanosleep(&req, &rem); |
+ if (r == 0) { |
+ break; |
+ } |
+ // We should only ever see an interrupt error. |
+ ASSERT(errno == EINTR); |
+ // Copy remainder into requested and repeat. |
+ req = rem; |
+ } |
} |
} // namespace bin |