aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/environment.c2
-rw-r--r--src/stdlib/stdlib.c10
-rw-r--r--src/stdlib/stdlib.h2
3 files changed, 10 insertions, 4 deletions
diff --git a/src/environment.c b/src/environment.c
index f84b6697..cf662749 100644
--- a/src/environment.c
+++ b/src/environment.c
@@ -541,7 +541,7 @@ env_t *global_env(bool source_mapping) {
{"print", "say", "func(text:Text, newline=yes)"},
{"say", "say", "func(text:Text, newline=yes)"},
{"setenv", "setenv_text", "func(name:Text, value:Text?)"},
- {"sleep", "sleep_num", "func(seconds:Num)"},
+ {"sleep", "sleep_seconds", "func(seconds:Num)"},
};
for (size_t i = 0; i < sizeof(global_vars) / sizeof(global_vars[0]); i++) {
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
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);