diff --git a/process.c b/process.c index 987fccdea7..4448f26c7b 100644 --- a/process.c +++ b/process.c @@ -17,6 +17,7 @@ #include "ruby/thread.h" #include "ruby/util.h" #include "vm_core.h" +#include "hrtime.h" #include #include @@ -931,7 +932,7 @@ void rb_native_mutex_unlock(rb_nativethread_lock_t *); void rb_native_cond_signal(rb_nativethread_cond_t *); void rb_native_cond_wait(rb_nativethread_cond_t *, rb_nativethread_lock_t *); int rb_sigwait_fd_get(const rb_thread_t *); -void rb_sigwait_sleep(const rb_thread_t *, int fd, const struct timespec *); +void rb_sigwait_sleep(const rb_thread_t *, int fd, const rb_hrtime_t *); void rb_sigwait_fd_put(const rb_thread_t *, int fd); void rb_thread_sleep_interruptible(void); @@ -1026,11 +1027,11 @@ waitpid_state_init(struct waitpid_state *w, rb_pid_t pid, int options) w->options = options; } -static const struct timespec * +static const rb_hrtime_t * sigwait_sleep_time(void) { if (SIGCHLD_LOSSY) { - static const struct timespec busy_wait = { 0, 100000000 }; + static const rb_hrtime_t busy_wait = 100 * RB_HRTIME_PER_MSEC; return &busy_wait; } diff --git a/thread_pthread.c b/thread_pthread.c index 7f57435698..b8778ee608 100644 --- a/thread_pthread.c +++ b/thread_pthread.c @@ -1945,24 +1945,25 @@ ruby_ppoll(struct pollfd *fds, nfds_t nfds, #endif void -rb_sigwait_sleep(rb_thread_t *th, int sigwait_fd, const struct timespec *ts) +rb_sigwait_sleep(rb_thread_t *th, int sigwait_fd, const rb_hrtime_t *rel) { struct pollfd pfd; + struct timespec ts; pfd.fd = sigwait_fd; pfd.events = POLLIN; if (!BUSY_WAIT_SIGNALS && ubf_threads_empty()) { - (void)ppoll(&pfd, 1, ts, 0); + (void)ppoll(&pfd, 1, rb_hrtime2timespec(&ts, rel), 0); check_signals_nogvl(th, sigwait_fd); } else { - rb_hrtime_t rel, end; + rb_hrtime_t to = RB_HRTIME_MAX, end; int n = 0; - if (ts) { - rel = rb_timespec2hrtime(ts); - end = rb_hrtime_add(rb_hrtime_now(), rel); + if (rel) { + to = *rel; + end = rb_hrtime_add(rb_hrtime_now(), to); } /* * tricky: this needs to return on spurious wakeup (no auto-retry). @@ -1970,16 +1971,15 @@ rb_sigwait_sleep(rb_thread_t *th, int sigwait_fd, const struct timespec *ts) * wakeups, so we care about the result of consume_communication_pipe */ for (;;) { - const rb_hrtime_t *sto = sigwait_timeout(th, sigwait_fd, &rel, &n); - struct timespec tmp; + const rb_hrtime_t *sto = sigwait_timeout(th, sigwait_fd, &to, &n); if (n) return; - n = ppoll(&pfd, 1, rb_hrtime2timespec(&tmp, sto), 0); + n = ppoll(&pfd, 1, rb_hrtime2timespec(&ts, sto), 0); if (check_signals_nogvl(th, sigwait_fd)) return; if (n || (th && RUBY_VM_INTERRUPTED(th->ec))) return; - if (ts && hrtime_update_expire(&rel, end)) + if (rel && hrtime_update_expire(&to, end)) return; } } @@ -2035,8 +2035,7 @@ native_sleep(rb_thread_t *th, rb_hrtime_t *rel) GVL_UNLOCK_BEGIN(th); if (!RUBY_VM_INTERRUPTED(th->ec)) { - struct timespec ts; - rb_sigwait_sleep(th, sigwait_fd, rb_hrtime2timespec(&ts, rel)); + rb_sigwait_sleep(th, sigwait_fd, rel); } else { check_signals_nogvl(th, sigwait_fd); diff --git a/thread_win32.c b/thread_win32.c index 83896d81ff..be3f614fff 100644 --- a/thread_win32.c +++ b/thread_win32.c @@ -793,9 +793,9 @@ rb_sigwait_fd_put(rb_thread_t *th, int fd) rb_bug("not implemented, should not be called"); } -NORETURN(void rb_sigwait_sleep(const rb_thread_t *, int, const struct timespec *)); +NORETURN(void rb_sigwait_sleep(const rb_thread_t *, int, const rb_hrtime_t *)); void -rb_sigwait_sleep(const rb_thread_t *th, int fd, const struct timespec *ts) +rb_sigwait_sleep(const rb_thread_t *th, int fd, const rb_hrtime_t *rel) { rb_bug("not implemented, should not be called"); }