From 08c47e1fabd1a2fb43c18828db8ad845f7db3a99 Mon Sep 17 00:00:00 2001 From: Bruce Hill Date: Sun, 7 Dec 2025 23:08:08 -0500 Subject: More correct handling for sleep() --- src/stdlib/stdlib.c | 10 ++++++++-- src/stdlib/stdlib.h | 2 +- 2 files changed, 9 insertions(+), 3 deletions(-) (limited to 'src/stdlib') diff --git a/src/stdlib/stdlib.c b/src/stdlib/stdlib.c index 8ec9e90b..defb263c 100644 --- a/src/stdlib/stdlib.c +++ b/src/stdlib/stdlib.c @@ -5,6 +5,7 @@ #include #include #include +#include #include #include #include @@ -205,11 +206,16 @@ cleanup: } public -void sleep_num(double seconds) { +void sleep_seconds(double seconds) { + if (seconds < 0) fail("Cannot sleep for a negative amount of time: ", seconds); + else if (isnan(seconds)) fail("Cannot sleep for a time that is NaN"); struct timespec ts; ts.tv_sec = (time_t)seconds; ts.tv_nsec = (long)((seconds - (double)ts.tv_sec) * 1e9); - nanosleep(&ts, NULL); + while (nanosleep(&ts, NULL) != 0) { + if (errno == EINTR) continue; + fail("Failed to sleep for the requested time (", strerror(errno), ")"); + } } public diff --git a/src/stdlib/stdlib.h b/src/stdlib/stdlib.h index 392b5f23..3afe3529 100644 --- a/src/stdlib/stdlib.h +++ b/src/stdlib/stdlib.h @@ -79,6 +79,6 @@ Text_t ask(Text_t prompt, bool bold, bool force_tty); _Noreturn void tomo_exit(Text_t text, int32_t status); Closure_t spawn(Closure_t fn); -void sleep_num(double seconds); +void sleep_seconds(double seconds); OptionalText_t getenv_text(Text_t name); void setenv_text(Text_t name, Text_t value); -- cgit v1.2.3