Index: runtime/vm/os_macos.cc |
diff --git a/runtime/vm/os_macos.cc b/runtime/vm/os_macos.cc |
index 76d9fb68379dc5caa1e6658cd2fae223744db58f..10f6dcf961095e7d2d0c80a4a42fd82876d28253 100644 |
--- a/runtime/vm/os_macos.cc |
+++ b/runtime/vm/os_macos.cc |
@@ -131,8 +131,29 @@ int OS::NumberOfAvailableProcessors() { |
void OS::Sleep(int64_t millis) { |
- // TODO(5411554): For now just use usleep we may have to revisit this. |
- usleep(millis * 1000); |
+ int64_t micros = millis * kMicrosecondsPerMillisecond; |
+ SleepMicros(micros); |
+} |
+ |
+ |
+void OS::SleepMicros(int64_t micros) { |
+ struct timespec req; // requested. |
+ struct timespec rem; // remainder. |
+ 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; |
+ } |
} |