From 664f9c00110f58c009a2e4b2e07f45ac6d0f8944 Mon Sep 17 00:00:00 2001 From: Ben Noordhuis Date: Thu, 28 Nov 2019 12:23:14 +0100 Subject: [PATCH] unix: switch uv_sleep() to nanosleep() MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Before this commit, uv_sleep() made two library calls: sleep() to sleep for the requested number of seconds, and then an usleep() call to sleep for the remaining milliseconds. Make a single nanosleep() call instead. Receiving a signal will wake up prematurely from sleep() but then re-enter sleep mode again in usleep(), which seems undesirable. PR-URL: https://github.com/libuv/libuv/pull/2552 Reviewed-By: Santiago Gimeno Reviewed-By: Saúl Ibarra Corretgé Reviewed-By: Richard Lau Reviewed-By: Colin Ihrig --- src/unix/core.c | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/src/unix/core.c b/src/unix/core.c index 2d5479af..6b10af56 100644 --- a/src/unix/core.c +++ b/src/unix/core.c @@ -1557,13 +1557,9 @@ int uv_gettimeofday(uv_timeval64_t* tv) { } void uv_sleep(unsigned int msec) { - unsigned int sec; - unsigned int usec; + struct timespec timeout; - sec = msec / 1000; - usec = (msec % 1000) * 1000; - if (sec > 0) - sleep(sec); - if (usec > 0) - usleep(usec); + timeout.tv_sec = msec / 1000; + timeout.tv_nsec = (msec % 1000) * 1000 * 1000; + nanosleep(&timeout, NULL); }