summaryrefslogtreecommitdiff
path: root/thread.c
diff options
context:
space:
mode:
authorkosaki <kosaki@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2011-05-06 17:39:32 +0000
committerkosaki <kosaki@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2011-05-06 17:39:32 +0000
commitb2ea836ae705cc690d1230476e6eb97e92e7ef52 (patch)
treeac115f6899ebd605a36aee02d976e29b6dad7f79 /thread.c
parent54b50fbb9c5a62a5dbefc40568985b23eb4d1238 (diff)
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
Diffstat (limited to 'thread.c')
-rw-r--r--thread.c35
1 files changed, 9 insertions, 26 deletions
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;