summaryrefslogtreecommitdiff
path: root/thread_pthread.c
diff options
context:
space:
mode:
authorkosaki <kosaki@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2011-05-06 16:56:06 +0000
committerkosaki <kosaki@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2011-05-06 16:56:06 +0000
commit54b50fbb9c5a62a5dbefc40568985b23eb4d1238 (patch)
tree01a7a3f559cc06408589b0ec407d9748349d4642 /thread_pthread.c
parent17707b2d487abdf1ad494b4e6c5fc539fc76817c (diff)
* 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
Diffstat (limited to 'thread_pthread.c')
-rw-r--r--thread_pthread.c25
1 files changed, 22 insertions, 3 deletions
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));