From b2ea836ae705cc690d1230476e6eb97e92e7ef52 Mon Sep 17 00:00:00 2001 From: kosaki Date: Fri, 6 May 2011 17:39:32 +0000 Subject: mutex: deadlock check timeout use monotonic time. * thread_pthread.c (native_cond_timeout): new internal api. it calculate a proper time for argument of native_cond_timedwait(). * thread_win32.c (native_cond_timeout): ditto. * thread_pthread.c (thread_timer): use native_cond_timeout() instead of get_ts. * thread.c (lock_func): ditto. * thread_pthread.c (get_ts): removed. use native_cond_timeout(). * thread.c (init_lock_timeout): ditto. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@31454 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- thread.c | 35 +++++++++-------------------------- 1 file changed, 9 insertions(+), 26 deletions(-) (limited to 'thread.c') diff --git a/thread.c b/thread.c index 1e3b957dd8..0bdc4ef6bf 100644 --- a/thread.c +++ b/thread.c @@ -3336,7 +3336,7 @@ mutex_alloc(VALUE klass) obj = TypedData_Make_Struct(klass, mutex_t, &mutex_data_type, mutex); native_mutex_initialize(&mutex->lock); - native_cond_initialize(&mutex->cond, 0); + native_cond_initialize(&mutex->cond, RB_CONDATTR_CLOCK_MONOTONIC); return obj; } @@ -3410,26 +3410,6 @@ rb_mutex_trylock(VALUE self) return locked; } -static struct timespec init_lock_timeout(int timeout_ms) -{ - struct timespec ts; - struct timeval tv; - int ret; - - ret = gettimeofday(&tv, NULL); - if (ret < 0) - rb_sys_fail(0); - - ts.tv_sec = tv.tv_sec; - ts.tv_nsec = tv.tv_usec * 1000 + timeout_ms * 1000 * 1000; - if (ts.tv_nsec >= 1000000000) { - ts.tv_sec++; - ts.tv_nsec -= 1000000000; - } - - return ts; -} - static int lock_func(rb_thread_t *th, mutex_t *mutex, int timeout_ms) { @@ -3438,9 +3418,6 @@ lock_func(rb_thread_t *th, mutex_t *mutex, int timeout_ms) native_mutex_lock(&mutex->lock); th->transition_for_lock = 0; for (;;) { - struct timespec ts; - int ret; - if (!mutex->th) { mutex->th = th; break; @@ -3448,8 +3425,14 @@ lock_func(rb_thread_t *th, mutex_t *mutex, int timeout_ms) mutex->cond_waiting++; if (timeout_ms) { - ts = init_lock_timeout(timeout_ms); - ret = native_cond_timedwait(&mutex->cond, &mutex->lock, &ts); + int ret; + struct timespec timeout_rel; + struct timespec timeout; + + timeout_rel.tv_sec = 0; + timeout_rel.tv_nsec = timeout_ms * 1000 * 1000; + timeout = native_cond_timeout(&mutex->cond, timeout_rel); + ret = native_cond_timedwait(&mutex->cond, &mutex->lock, &timeout); if (ret == ETIMEDOUT) { interrupted = 2; break; -- cgit v1.2.3