Commit 1611698e

1611698e9936Merged

Yukihiro "Matz" Matsumoto <[email protected]> · 6 months ago · 2 files · ~2 modified

Description

fix(codegen,runtime): sleep(0.5) waits 500ms via sp_sleep helper

sleep(0.5) previously lowered to sleep((unsigned)0.5) which C-casts the float to unsigned, producing 0 and returning immediately. CRuby waits 500ms. Add a sp_sleep(mrb_float s) runtime helper that uses nanosleep on POSIX and Sleep() (in milliseconds) on Windows; codegen now calls sp_sleep((mrb_float)<arg>) for both int and float arguments.

EINTR-safe on POSIX: the loop continues until the remaining duration completes. Negative / NaN inputs no-op.

Co-Authored-By: Claude Opus 4.7 (1M context) [email protected]

Details

Commit
1611698e9936
Parent
2273c1ed0e30

lib/sp_runtime.hmodifiedView file

@@ -1715,6 +1715,27 @@
17151715 static volatile int sp_catch_top = 0;
17161716 static void sp_throw(const char *tag, mrb_int val) { int i = sp_catch_top - 1; while (i >= 0) { if (strcmp(sp_catch_tag[i], tag) == 0) { sp_catch_val[i] = val; sp_catch_top = i + 1; longjmp(sp_catch_stack[i], 1); } i--; } fprintf(stderr, "uncaught throw: %s\n", tag); exit(1); }
17171717
1718+/* Kernel#sleep with sub-second precision. Argument is seconds as a
1719+ double so `sleep(0.5)` actually waits 500ms; the legacy `sleep((unsigned)0.5)`
1720+ cast truncated to 0 and returned immediately. POSIX uses
1721+ nanosleep(); Windows uses Sleep() (milliseconds). Negative or NaN
1722+ inputs no-op. */
1723+static void sp_sleep(mrb_float s) {
1724+ if (!(s > 0.0)) return;
1725+#ifdef _WIN32
1726+ DWORD ms = (DWORD)(s * 1000.0);
1727+ if (ms == 0) ms = 1;
1728+ Sleep(ms);
1729+#else
1730+ struct timespec req;
1731+ req.tv_sec = (time_t)s;
1732+ req.tv_nsec = (long)((s - (double)req.tv_sec) * 1e9);
1733+ if (req.tv_nsec < 0) req.tv_nsec = 0;
1734+ if (req.tv_nsec >= 1000000000L) req.tv_nsec = 999999999L;
1735+ while (nanosleep(&req, &req) == -1 && errno == EINTR) {}
1736+#endif
1737+}
1738+
17181739 #ifdef _WIN32
17191740 static DWORD sp_file_attributes(const char *path) {
17201741 if (!path) return INVALID_FILE_ATTRIBUTES;