From 54b50fbb9c5a62a5dbefc40568985b23eb4d1238 Mon Sep 17 00:00:00 2001 From: kosaki Date: Fri, 6 May 2011 16:56:06 +0000 Subject: [PATCH] * thread_pthread.c (get_ts): add monotonic clock capability. * thread_pthread.c (rb_thread_create_timer_thread): use monotonic clock if possible. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@31453 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- ChangeLog | 6 ++++++ thread_pthread.c | 25 ++++++++++++++++++++++--- 2 files changed, 28 insertions(+), 3 deletions(-) diff --git a/ChangeLog b/ChangeLog index 379f2f652d..71c407fefa 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,9 @@ +Sat May 7 01:54:21 2011 KOSAKI Motohiro + + * thread_pthread.c (get_ts): add monotonic clock capability. + * thread_pthread.c (rb_thread_create_timer_thread): use monotonic + clock if possible. + Sat May 7 01:43:37 2011 KOSAKI Motohiro * thread_pthread.h (rb_thread_cond_t): add clockid field. it's diff --git a/thread_pthread.c b/thread_pthread.c index 4cb16544f5..7d55ba2763 100644 --- a/thread_pthread.c +++ b/thread_pthread.c @@ -920,10 +920,29 @@ static pthread_mutex_t timer_thread_lock = PTHREAD_MUTEX_INITIALIZER; static struct timespec * get_ts(struct timespec *ts, unsigned long nsec) { + int ret; struct timeval tv; - gettimeofday(&tv, 0); + +#if USE_MONOTONIC_COND + if (timer_thread_cond.clockid == CLOCK_MONOTONIC) { + ret = clock_gettime(CLOCK_MONOTONIC, ts); + if (ret != 0) + rb_sys_fail("clock_gettime(CLOCK_MONOTONIC)"); + goto out; + } +#endif + + if (timer_thread_cond.clockid != CLOCK_REALTIME) + rb_bug("unsupported clockid %d", timer_thread_cond.clockid); + + ret = gettimeofday(&tv, 0); + if (ret != 0) + rb_sys_fail(0); ts->tv_sec = tv.tv_sec; - ts->tv_nsec = tv.tv_usec * 1000 + nsec; + ts->tv_nsec = tv.tv_usec * 1000; + + out: + ts->tv_nsec += nsec; if (ts->tv_nsec >= PER_NANO) { ts->tv_sec++; ts->tv_nsec -= PER_NANO; @@ -975,7 +994,7 @@ rb_thread_create_timer_thread(void) int err; pthread_attr_init(&attr); - native_cond_initialize(&timer_thread_cond, 0); + native_cond_initialize(&timer_thread_cond, RB_CONDATTR_CLOCK_MONOTONIC); #ifdef PTHREAD_STACK_MIN pthread_attr_setstacksize(&attr, PTHREAD_STACK_MIN + (THREAD_DEBUG ? BUFSIZ : 0));