unix: switch uv_sleep() to nanosleep()

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 <santiago.gimeno@gmail.com>
Reviewed-By: Saúl Ibarra Corretgé <saghul@gmail.com>
Reviewed-By: Richard Lau <riclau@uk.ibm.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
This commit is contained in:
Ben Noordhuis 2019-11-28 12:23:14 +01:00 committed by cjihrig
parent 5500253cac
commit 664f9c0011
No known key found for this signature in database
GPG Key ID: 7434390BDBE9B9C5

View File

@ -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);
}