aboutsummaryrefslogtreecommitdiff
path: root/src/stdlib/stdlib.c
diff options
context:
space:
mode:
authorBruce Hill <bruce@bruce-hill.com>2025-12-07 23:08:08 -0500
committerBruce Hill <bruce@bruce-hill.com>2025-12-07 23:08:08 -0500
commit08c47e1fabd1a2fb43c18828db8ad845f7db3a99 (patch)
tree5448c2707496c5dfeed2f34803582b5e7fe94fae /src/stdlib/stdlib.c
parent4c5d15115d2d6c08c080d7d4a39efe039658d616 (diff)
More correct handling for sleep()
Diffstat (limited to 'src/stdlib/stdlib.c')
-rw-r--r--src/stdlib/stdlib.c10
1 files changed, 8 insertions, 2 deletions
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 <fcntl.h>
#include <gc.h>
#include <locale.h>
+#include <math.h>
#include <signal.h>
#include <stdbool.h>
#include <stdint.h>
@@ -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