From 5ff2a1968dc3d572b25af1ee3bf2e1093fc47b2d Mon Sep 17 00:00:00 2001 From: normal Date: Wed, 13 Jun 2018 10:00:46 +0000 Subject: thread.c: use flags for sleep_* functions Same thing as https://bugs.ruby-lang.org/issues/14798 My easily-confused mind gets function call ordering confused easily: sleep_forever(..., TRUE, FALSE); sleep_forever(..., FALSE, TRUE); git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@63647 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- thread.c | 32 +++++++++++++++++++------------- 1 file changed, 19 insertions(+), 13 deletions(-) (limited to 'thread.c') diff --git a/thread.c b/thread.c index 214df5b0df..6178f477de 100644 --- a/thread.c +++ b/thread.c @@ -92,8 +92,13 @@ static VALUE sym_on_blocking; static VALUE sym_never; static ID id_locals; -static void sleep_timespec(rb_thread_t *, struct timespec, int spurious_check); -static void sleep_forever(rb_thread_t *th, int nodeadlock, int spurious_check); +enum SLEEP_FLAGS { + SLEEP_DEADLOCKABLE = 0x1, + SLEEP_SPURIOUS_CHECK = 0x2 +}; + +static void sleep_timespec(rb_thread_t *, struct timespec, unsigned int fl); +static void sleep_forever(rb_thread_t *th, unsigned int fl); static void rb_thread_sleep_deadly_allow_spurious_wakeup(void); static int rb_threadptr_dead(rb_thread_t *th); static void rb_check_deadlock(rb_vm_t *vm); @@ -1135,24 +1140,25 @@ double2timespec(struct timespec *ts, double d) } static void -sleep_forever(rb_thread_t *th, int deadlockable, int spurious_check) +sleep_forever(rb_thread_t *th, unsigned int fl) { enum rb_thread_status prev_status = th->status; - enum rb_thread_status status = deadlockable ? THREAD_STOPPED_FOREVER : THREAD_STOPPED; + enum rb_thread_status status; + status = fl & SLEEP_DEADLOCKABLE ? THREAD_STOPPED_FOREVER : THREAD_STOPPED; th->status = status; RUBY_VM_CHECK_INTS_BLOCKING(th->ec); while (th->status == status) { - if (deadlockable) { + if (fl & SLEEP_DEADLOCKABLE) { th->vm->sleeper++; rb_check_deadlock(th->vm); } native_sleep(th, 0); - if (deadlockable) { + if (fl & SLEEP_DEADLOCKABLE) { th->vm->sleeper--; } RUBY_VM_CHECK_INTS_BLOCKING(th->ec); - if (!spurious_check) + if (!(fl & SLEEP_SPURIOUS_CHECK)) break; } th->status = prev_status; @@ -1238,7 +1244,7 @@ timespec_update_expire(struct timespec *ts, const struct timespec *end) } static void -sleep_timespec(rb_thread_t *th, struct timespec ts, int spurious_check) +sleep_timespec(rb_thread_t *th, struct timespec ts, unsigned int fl) { struct timespec end; enum rb_thread_status prev_status = th->status; @@ -1252,7 +1258,7 @@ sleep_timespec(rb_thread_t *th, struct timespec ts, int spurious_check) RUBY_VM_CHECK_INTS_BLOCKING(th->ec); if (timespec_update_expire(&ts, &end)) break; - if (!spurious_check) + if (!(fl & SLEEP_SPURIOUS_CHECK)) break; } th->status = prev_status; @@ -1262,21 +1268,21 @@ void rb_thread_sleep_forever(void) { thread_debug("rb_thread_sleep_forever\n"); - sleep_forever(GET_THREAD(), FALSE, TRUE); + sleep_forever(GET_THREAD(), SLEEP_SPURIOUS_CHECK); } void rb_thread_sleep_deadly(void) { thread_debug("rb_thread_sleep_deadly\n"); - sleep_forever(GET_THREAD(), TRUE, TRUE); + sleep_forever(GET_THREAD(), SLEEP_DEADLOCKABLE|SLEEP_SPURIOUS_CHECK); } static void rb_thread_sleep_deadly_allow_spurious_wakeup(void) { thread_debug("rb_thread_sleep_deadly_allow_spurious_wakeup\n"); - sleep_forever(GET_THREAD(), TRUE, FALSE); + sleep_forever(GET_THREAD(), SLEEP_DEADLOCKABLE); } void @@ -1286,7 +1292,7 @@ rb_thread_wait_for(struct timeval time) struct timespec ts; timespec_for(&ts, &time); - sleep_timespec(th, ts, 1); + sleep_timespec(th, ts, SLEEP_SPURIOUS_CHECK); } /* -- cgit v1.2.3