dumping ground for random patches and texts
 help / color / mirror / Atom feed
From: Eric Wong <e@80x24.org>
To: spew@80x24.org
Subject: [PATCH 3/4] thread.c: avoid FP for Thread#join
Date: Fri,  2 Feb 2018 05:18:36 +0000	[thread overview]
Message-ID: <20180202051837.14192-4-e@80x24.org> (raw)
In-Reply-To: <20180202051837.14192-1-e@80x24.org>

FP arithmetic can lose precision in some cases leading to
premature wakeup and wasting CPU cycles.

Convert to use timeval_* functions for now.
---
 thread.c | 60 ++++++++++++++++++++++++++++++++++++++----------------------
 1 file changed, 38 insertions(+), 22 deletions(-)

diff --git a/thread.c b/thread.c
index a6d55f62e7..392dd757be 100644
--- a/thread.c
+++ b/thread.c
@@ -99,6 +99,10 @@ static int rb_threadptr_dead(rb_thread_t *th);
 static void rb_check_deadlock(rb_vm_t *vm);
 static int rb_threadptr_pending_interrupt_empty_p(const rb_thread_t *th);
 static const char *thread_status_name(rb_thread_t *th, int detail);
+static void timeval_add(struct timeval *, const struct timeval *);
+static void timeval_sub(struct timeval *, const struct timeval *);
+static int timeval_update_expire(struct timeval *, const struct timeval *);
+static void getclockofday(struct timeval *);
 
 #define eKillSignal INT2FIX(0)
 #define eTerminateSignal INT2FIX(1)
@@ -481,8 +485,6 @@ rb_threadptr_unlock_all_locking_mutexes(rb_thread_t *th)
     }
 }
 
-static struct timeval double2timeval(double d);
-
 void
 rb_thread_terminate_all(void)
 {
@@ -848,12 +850,9 @@ rb_thread_create(VALUE (*fn)(ANYARGS), void *arg)
 }
 
 
-/* +infty, for this purpose */
-#define DELAY_INFTY 1E30
-
 struct join_arg {
     rb_thread_t *target, *waiting;
-    double delay;
+    struct timeval *limit;
 };
 
 static VALUE
@@ -882,11 +881,15 @@ thread_join_sleep(VALUE arg)
 {
     struct join_arg *p = (struct join_arg *)arg;
     rb_thread_t *target_th = p->target, *th = p->waiting;
-    const int forever = p->delay == DELAY_INFTY;
-    const double limit = forever ? 0 : timeofday() + p->delay;
+    struct timeval to;
+
+    if (p->limit) {
+        getclockofday(&to);
+        timeval_add(&to, p->limit);
+    }
 
     while (target_th->status != THREAD_KILLED) {
-	if (forever) {
+	if (!p->limit) {
 	    th->status = THREAD_STOPPED_FOREVER;
 	    th->vm->sleeper++;
 	    rb_check_deadlock(th->vm);
@@ -894,17 +897,13 @@ thread_join_sleep(VALUE arg)
 	    th->vm->sleeper--;
 	}
 	else {
-	    double now = timeofday();
-	    struct timeval tv;
-
-	    if (now > limit) {
+            if (timeval_update_expire(p->limit, &to)) {
 		thread_debug("thread_join: timeout (thid: %"PRI_THREAD_ID")\n",
 			     thread_id_str(target_th));
 		return Qfalse;
 	    }
-	    tv = double2timeval(limit - now);
 	    th->status = THREAD_STOPPED;
-	    native_sleep(th, &tv);
+	    native_sleep(th, p->limit);
 	}
 	RUBY_VM_CHECK_INTS_BLOCKING(th->ec);
 	th->status = THREAD_RUNNABLE;
@@ -915,7 +914,7 @@ thread_join_sleep(VALUE arg)
 }
 
 static VALUE
-thread_join(rb_thread_t *target_th, double delay)
+thread_join(rb_thread_t *target_th, struct timeval *tv)
 {
     rb_thread_t *th = GET_THREAD();
     struct join_arg arg;
@@ -929,7 +928,7 @@ thread_join(rb_thread_t *target_th, double delay)
 
     arg.target = target_th;
     arg.waiting = th;
-    arg.delay = delay;
+    arg.limit = tv;
 
     thread_debug("thread_join (thid: %"PRI_THREAD_ID", status: %s)\n",
 		 thread_id_str(target_th), thread_status_name(target_th, TRUE));
@@ -974,6 +973,8 @@ thread_join(rb_thread_t *target_th, double delay)
     return target_th->self;
 }
 
+static struct timeval double2timeval(double);
+
 /*
  *  call-seq:
  *     thr.join          -> thr
@@ -1016,15 +1017,30 @@ thread_join(rb_thread_t *target_th, double delay)
 static VALUE
 thread_join_m(int argc, VALUE *argv, VALUE self)
 {
-    double delay = DELAY_INFTY;
     VALUE limit;
+    struct timeval timeval;
+    struct timeval *tv = 0;
 
     rb_scan_args(argc, argv, "01", &limit);
-    if (!NIL_P(limit)) {
-	delay = rb_num2dbl(limit);
+
+    /*
+     * This supports INFINITY and negative values, so we can't use
+     * rb_time_interval right now...
+     */
+    switch (TYPE(limit)) {
+      case T_NIL: break;
+      case T_FIXNUM:
+      case T_BIGNUM:
+        timeval.tv_sec = NUM2TIMET(limit);
+        timeval.tv_usec = 0;
+        tv = &timeval;
+        break;
+      default:
+        timeval = double2timeval(rb_num2dbl(limit));
+        tv = &timeval;
     }
 
-    return thread_join(rb_thread_ptr(self), delay);
+    return thread_join(rb_thread_ptr(self), tv);
 }
 
 /*
@@ -1045,7 +1061,7 @@ static VALUE
 thread_value(VALUE self)
 {
     rb_thread_t *th = rb_thread_ptr(self);
-    thread_join(th, DELAY_INFTY);
+    thread_join(th, 0);
     return th->value;
 }
 
-- 
EW


  parent reply	other threads:[~2018-02-02  5:18 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-02-02  5:18 [PATCHv2 0/4] remove most FP from thread.c Eric Wong
2018-02-02  5:18 ` [PATCH 1/4] thread.c (rb_thread_terminate_all): eliminate double2timeval call Eric Wong
2018-02-02  5:18 ` [PATCH 2/4] thread.c: extract timeval_sub from timeval_update_expire Eric Wong
2018-02-02  5:18 ` Eric Wong [this message]
2018-02-02  5:18 ` [PATCH 4/4] thread.c: avoid FP in C-API time calculations Eric Wong

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=20180202051837.14192-4-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).