From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.1 (2015-04-28) on dcvr.yhbt.net X-Spam-Level: ** X-Spam-ASN: AS6939 216.218.128.0/17 X-Spam-Status: No, score=2.2 required=3.0 tests=BAYES_00,RCVD_IN_MSPIKE_BL, RCVD_IN_MSPIKE_ZBI,RCVD_IN_XBL,RDNS_NONE,SPF_FAIL,SPF_HELO_FAIL, TO_EQ_FM_DOM_SPF_FAIL shortcircuit=no autolearn=no autolearn_force=no version=3.4.1 Received: from 80x24.org (unknown [216.218.222.14]) by dcvr.yhbt.net (Postfix) with ESMTP id 470A81F403 for ; Fri, 8 Jun 2018 09:39:53 +0000 (UTC) From: Eric Wong To: spew@80x24.org Subject: [PATCH] thread.c: use flags for sleep_* functions Date: Fri, 8 Jun 2018 09:39:51 +0000 Message-Id: <20180608093951.21762-1-e@80x24.org> List-Id: 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); --- thread.c | 32 +++++++++++++++++++------------- 1 file changed, 19 insertions(+), 13 deletions(-) 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); } /* -- EW