dumping ground for random patches and texts
 help / color / mirror / Atom feed
From: Eric Wong <e@80x24.org>
To: spew@80x24.org
Subject: [PATCH] wip
Date: Fri,  6 Jul 2018 21:31:22 +0000	[thread overview]
Message-ID: <20180706213122.5728-1-e@80x24.org> (raw)

---
 configure.ac     |  5 ++++
 thread_pthread.c | 60 ++++++++++++++++++++++++++++++------------------
 2 files changed, 43 insertions(+), 22 deletions(-)

diff --git a/configure.ac b/configure.ac
index 074a3fd394f..899f2437467 100644
--- a/configure.ac
+++ b/configure.ac
@@ -1998,6 +1998,11 @@ AS_IF([test x"$ac_cv_func_clock_gettime" != xyes], [
     ])
 ])
 AC_CHECK_FUNCS(clock_getres) # clock_getres should be tested after clock_gettime test including librt test.
+AC_CHECK_LIB([rt], [timer_create], [
+  AC_CHECK_LIB([rt], [timer_settime], [
+    AC_CHECK_LIB([rt], [timer_gettime], [
+      AC_CHECK_LIB([rt], [timer_delete], [AC_DEFINE(HAVE_POSIX_TIMERS)])
+])])])
 
 AC_CACHE_CHECK(for unsetenv returns a value, rb_cv_unsetenv_return_value,
   [AC_TRY_COMPILE([
diff --git a/thread_pthread.c b/thread_pthread.c
index 722ce444879..c4670efc9e3 100644
--- a/thread_pthread.c
+++ b/thread_pthread.c
@@ -34,6 +34,9 @@
 #if defined(__HAIKU__)
 #include <kernel/OS.h>
 #endif
+#if defined(HAVE_TIME_H)
+#include <time.h>
+#endif
 
 void rb_native_mutex_lock(rb_nativethread_lock_t *lock);
 void rb_native_mutex_unlock(rb_nativethread_lock_t *lock);
@@ -46,6 +49,18 @@ void rb_native_cond_wait(rb_nativethread_cond_t *cond, rb_nativethread_lock_t *m
 void rb_native_cond_initialize(rb_nativethread_cond_t *cond);
 void rb_native_cond_destroy(rb_nativethread_cond_t *cond);
 static void rb_thread_wakeup_timer_thread_low(void);
+
+#define TIMER_THREAD_MASK    (1)
+#define TIMER_THREAD_SLEEPY  (2|TIMER_THREAD_MASK)
+#define TIMER_THREAD_BUSY    (4|TIMER_THREAD_MASK)
+
+#if defined(HAVE_POLL) && defined(HAVE_FCNTL) && defined(F_GETFL) && defined(F_SETFL) && defined(O_NONBLOCK)
+/* The timer thread sleeps while only one Ruby thread is running. */
+# define TIMER_IMPL TIMER_THREAD_SLEEPY
+#else
+# define TIMER_IMPL TIMER_THREAD_BUSY
+#endif
+
 static struct {
     pthread_t id;
     int created;
@@ -61,13 +76,6 @@ static pthread_condattr_t *condattr_monotonic = &condattr_mono;
 static const void *const condattr_monotonic = NULL;
 #endif
 
-#if defined(HAVE_POLL) && defined(HAVE_FCNTL) && defined(F_GETFL) && defined(F_SETFL) && defined(O_NONBLOCK)
-/* The timer thread sleeps while only one Ruby thread is running. */
-# define USE_SLEEPY_TIMER_THREAD 1
-#else
-# define USE_SLEEPY_TIMER_THREAD 0
-#endif
-
 static void
 gvl_acquire_common(rb_vm_t *vm)
 {
@@ -982,6 +990,7 @@ native_thread_create(rb_thread_t *th)
     return err;
 }
 
+#if (TIMER_IMPL & TIMER_THREAD_MASK)
 static void
 native_thread_join(pthread_t th)
 {
@@ -990,6 +999,7 @@ native_thread_join(pthread_t th)
 	rb_raise(rb_eThreadError, "native_thread_join() failed (%d)", err);
     }
 }
+#endif /* TIMER_THREAD_MASK */
 
 #if USE_NATIVE_THREAD_PRIORITY
 
@@ -1138,8 +1148,10 @@ ubf_select(void *ptr)
      * In the other hands, we shouldn't call rb_thread_wakeup_timer_thread()
      * if running on timer thread because it may make endless wakeups.
      */
+#if TIMER_IMPL == TIMER_THREAD_SLEEPY
     if (!pthread_equal(pthread_self(), timer_thread.id))
 	rb_thread_wakeup_timer_thread();
+#endif
     ubf_wakeup_thread(th);
 }
 
@@ -1181,7 +1193,7 @@ static int ubf_threads_empty(void) { return 1; }
  */
 #define TIME_QUANTUM_USEC (100 * 1000)
 
-#if USE_SLEEPY_TIMER_THREAD
+#if TIMER_IMPL == TIMER_THREAD_SLEEPY
 static struct {
     /*
      * Read end of each pipe is closed inside timer thread for shutdown
@@ -1419,8 +1431,9 @@ timer_thread_sleep(rb_vm_t *vm)
 	}
     }
 }
+#endif /* TIMER_THREAD_SLEEPY */
 
-#else /* USE_SLEEPY_TIMER_THREAD */
+#if TIMER_IMPL == TIMER_THREAD_BUSY
 # define PER_NANO 1000000000
 void rb_thread_wakeup_timer_thread(void) {}
 static void rb_thread_wakeup_timer_thread_low(void) {}
@@ -1438,7 +1451,7 @@ timer_thread_sleep(rb_vm_t *unused)
 
     native_cond_timedwait(&timer_thread_cond, &timer_thread_lock, &ts);
 }
-#endif /* USE_SLEEPY_TIMER_THREAD */
+#endif /* TIMER_IMPL == TIMER_THREAD_BUSY */
 
 #if !defined(SET_CURRENT_THREAD_NAME) && defined(__linux__) && defined(PR_SET_NAME)
 # define SET_CURRENT_THREAD_NAME(name) prctl(PR_SET_NAME, name)
@@ -1508,7 +1521,7 @@ thread_timer(void *p)
     SET_CURRENT_THREAD_NAME("ruby-timer-thr");
 #endif
 
-#if !USE_SLEEPY_TIMER_THREAD
+#if TIMER_IMPL == TIMER_THREAD_BUSY
     rb_native_mutex_initialize(&timer_thread_lock);
     rb_native_cond_initialize(&timer_thread_cond);
     rb_native_mutex_lock(&timer_thread_lock);
@@ -1524,10 +1537,11 @@ thread_timer(void *p)
         /* wait */
 	timer_thread_sleep(vm);
     }
-#if USE_SLEEPY_TIMER_THREAD
+#if TIMER_IMPL == TIMER_THREAD_SLEEPY
     CLOSE_INVALIDATE(normal[0]);
     CLOSE_INVALIDATE(low[0]);
-#else
+#endif
+#if TIMER_IMPL == TIMER_THREAD_BUSY
     rb_native_mutex_unlock(&timer_thread_lock);
     rb_native_cond_destroy(&timer_thread_cond);
     rb_native_mutex_destroy(&timer_thread_lock);
@@ -1537,6 +1551,7 @@ thread_timer(void *p)
     return NULL;
 }
 
+#if (TIMER_IMPL & TIMER_THREAD_MASK)
 static void
 rb_thread_create_timer_thread(void)
 {
@@ -1581,14 +1596,14 @@ rb_thread_create_timer_thread(void)
 	}
 # endif
 
-#if USE_SLEEPY_TIMER_THREAD
+#if TIMER_IMPL == TIMER_THREAD_SLEEPY
 	err = setup_communication_pipe();
 	if (err != 0) {
 	    rb_warn("pipe creation failed for timer: %s, scheduling broken",
 		    strerror(err));
 	    return;
 	}
-#endif /* USE_SLEEPY_TIMER_THREAD */
+#endif /* TIMER_THREAD_SLEEPY */
 
 	/* create timer thread */
 	if (timer_thread.created) {
@@ -1617,21 +1632,22 @@ rb_thread_create_timer_thread(void)
 		rb_warn("timer thread stack size: system default");
 	    }
 	    VM_ASSERT(err == 0);
-#if USE_SLEEPY_TIMER_THREAD
+#if TIMER_IMPL == TIMER_THREAD_SLEEPY
 	    CLOSE_INVALIDATE(normal[0]);
 	    CLOSE_INVALIDATE(normal[1]);
 	    CLOSE_INVALIDATE(low[0]);
 	    CLOSE_INVALIDATE(low[1]);
-#endif
+#endif /* TIMER_THREAD_SLEEPY */
 	    return;
 	}
-#if USE_SLEEPY_TIMER_THREAD
+#if TIMER_IMPL == TIMER_THREAD_SLEEPY
 	/* validate pipe on this process */
 	timer_thread_pipe.owner_process = getpid();
-#endif
+#endif /* TIMER_THREAD_SLEEPY */
 	timer_thread.created = 1;
     }
 }
+#endif /* TIMER_IMPL & TIMER_THREAD_MASK */
 
 static int
 native_stop_timer_thread(void)
@@ -1641,7 +1657,7 @@ native_stop_timer_thread(void)
 
     if (TT_DEBUG) fprintf(stderr, "stop timer thread\n");
     if (stopped) {
-#if USE_SLEEPY_TIMER_THREAD
+#if TIMER_IMPL == TIMER_THREAD_SLEEPY
 	/* prevent wakeups from signal handler ASAP */
 	timer_thread_pipe.owner_process = 0;
 
@@ -1662,7 +1678,7 @@ native_stop_timer_thread(void)
 	/* timer thread will stop looping when system_working <= 0: */
 	native_thread_join(timer_thread.id);
 
-#if USE_SLEEPY_TIMER_THREAD
+#if TIMER_IMPL == TIMER_THREAD_SLEEPY
 	/* timer thread will close the read end on exit: */
 	VM_ASSERT(timer_thread_pipe.normal[0] == -1);
 	VM_ASSERT(timer_thread_pipe.low[0] == -1);
@@ -1727,7 +1743,7 @@ ruby_stack_overflowed_p(const rb_thread_t *th, const void *addr)
 int
 rb_reserved_fd_p(int fd)
 {
-#if USE_SLEEPY_TIMER_THREAD
+#if TIMER_IMPL == TIMER_THREAD_SLEEPY
     if ((fd == timer_thread_pipe.normal[0] ||
 	 fd == timer_thread_pipe.normal[1] ||
 	 fd == timer_thread_pipe.low[0] ||
-- 
EW


             reply	other threads:[~2018-07-06 21:31 UTC|newest]

Thread overview: 23+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-07-06 21:31 Eric Wong [this message]
  -- strict thread matches above, loose matches on Subject: below --
2021-10-27 20:16 [PATCH] wip Eric Wong
2021-06-05 19:58 Eric Wong
2021-04-05  7:42 Eric Wong
2021-03-08  7:11 Eric Wong
2021-01-21  4:24 [PATCH] WIP Eric Wong
2021-01-03 22:57 [PATCH] wip Eric Wong
2020-12-27 11:36 [PATCH] WIP Eric Wong
2020-11-15  7:35 [PATCH] wip Eric Wong
2020-04-23  4:27 Eric Wong
2020-04-20  7:14 Eric Wong
2020-01-13  9:24 [PATCH] WIP Eric Wong
2019-05-11 22:55 Eric Wong
2019-01-02  9:21 [PATCH] wip Eric Wong
2018-06-24 11:55 Eric Wong
2018-06-24  8:39 Eric Wong
2017-07-15  1:42 [PATCH] WIP Eric Wong
2017-04-12 20:17 [PATCH] wip Eric Wong
2017-04-05 18:40 Eric Wong
2016-08-23 20:07 Eric Wong
2016-08-18  2:16 Eric Wong
2016-06-26  3:46 Eric Wong
2015-12-22  0:15 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=20180706213122.5728-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).