dumping ground for random patches and texts
 help / color / mirror / Atom feed
From: Eric Wong <e@80x24.org>
To: spew@80x24.org
Subject: [PATCH] thread.c: use flags for sleep_* functions
Date: Fri,  8 Jun 2018 09:39:51 +0000	[thread overview]
Message-ID: <20180608093951.21762-1-e@80x24.org> (raw)

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


                 reply	other threads:[~2018-06-08  9:39 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20180608093951.21762-1-e@80x24.org \
    --to=e@80x24.org \
    --cc=spew@80x24.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).