Implement SleepNanoseconds() on Windows.
This uses the naïve implementation originally written
https://codereview.chromium.org/807973002/#ps180001.
Bug: crashpad:192
Change-Id: Id00908dafb8886d6163a8b17213d3b7c33b81963
Reviewed-on: https://chromium-review.googlesource.com/606998
Commit-Queue: Robert Sesek <rsesek@chromium.org>
Reviewed-by: Mark Mentovai <mark@chromium.org>
diff --git a/util/misc/clock.h b/util/misc/clock.h
index a07e12a..ed88cdc 100644
--- a/util/misc/clock.h
+++ b/util/misc/clock.h
@@ -34,19 +34,15 @@
//! \return The value of the system’s monotonic clock, in nanoseconds.
uint64_t ClockMonotonicNanoseconds();
-#if !defined(OS_WIN) // Not implemented on Windows yet.
-
//! \brief Sleeps for the specified duration.
//!
//! \param[in] nanoseconds The number of nanoseconds to sleep. The actual sleep
//! may be slightly longer due to latencies and timer resolution.
//!
-//! This function is resilient against the underlying `nanosleep()` system call
-//! being interrupted by a signal.
+//! On POSIX, this function is resilient against the underlying `nanosleep()`
+//! system call being interrupted by a signal.
void SleepNanoseconds(uint64_t nanoseconds);
-#endif
-
} // namespace crashpad
#endif // CRASHPAD_UTIL_MISC_CLOCK_H_
diff --git a/util/misc/clock_win.cc b/util/misc/clock_win.cc
index 18d0754..1079c42 100644
--- a/util/misc/clock_win.cc
+++ b/util/misc/clock_win.cc
@@ -45,4 +45,12 @@
((leftover_ticks * kNanosecondsPerSecond) / frequency);
}
+void SleepNanoseconds(uint64_t nanoseconds) {
+ // This is both inaccurate (will be way too long for short sleeps) and
+ // incorrect (can sleep for less than requested). But it's what's available
+ // without implementing a busy loop.
+ constexpr uint64_t kNanosecondsPerMillisecond = static_cast<uint64_t>(1E6);
+ Sleep(static_cast<DWORD>(nanoseconds / kNanosecondsPerMillisecond));
+}
+
} // namespace crashpad