From 7ac6b706b418db4311ce64219c6156ec628453de Mon Sep 17 00:00:00 2001 From: kosaki Date: Fri, 6 May 2011 18:17:14 +0000 Subject: sleep_cond use monotonic time if possible. * thread_pthread.c (native_thread_init): change sleep_cond attribute to monotonic. * thread_pthread.c (native_sleep): use native_cond_timeout(). * thread_pthread.c (native_cond_timeout): add overflow care. * thread_win32.c (native_cond_timeout): ditto. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@31457 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- thread_win32.c | 26 +++++++++++++++++++++++--- 1 file changed, 23 insertions(+), 3 deletions(-) (limited to 'thread_win32.c') diff --git a/thread_win32.c b/thread_win32.c index 0288e772a1..80c3890975 100644 --- a/thread_win32.c +++ b/thread_win32.c @@ -491,29 +491,49 @@ native_cond_timedwait(rb_thread_cond_t *cond, rb_thread_lock_t *mutex, struct ti return __cond_timedwait(cond, mutex, timeout_ms); } +#if SIZEOF_TIME_T == SIZEOF_LONG +typedef unsigned long unsigned_time_t; +#elif SIZEOF_TIME_T == SIZEOF_INT +typedef unsigned int unsigned_time_t; +#elif SIZEOF_TIME_T == SIZEOF_LONG_LONG +typedef unsigned LONG_LONG unsigned_time_t; +#else +# error cannot find integer type which size is same as time_t. +#endif + +#define TIMET_MAX (~(time_t)0 <= 0 ? (time_t)((~(unsigned_time_t)0) >> 1) : (time_t)(~(unsigned_time_t)0)) + static struct timespec native_cond_timeout(rb_thread_cond_t *cond, struct timespec timeout_rel) { int ret; struct timeval tv; struct timespec timeout; + struct timespec now; ret = gettimeofday(&tv, 0); if (ret != 0) rb_sys_fail(0); - timeout.tv_sec = tv.tv_sec; - timeout.tv_nsec = tv.tv_usec * 1000; + now.tv_sec = tv.tv_sec; + now.tv_nsec = tv.tv_usec * 1000; + out: + timeout.tv_sec = now.tv_sec; + timeout.tv_nsec = now.tv_nsec; timeout.tv_sec += timeout_rel.tv_sec; timeout.tv_nsec += timeout_rel.tv_nsec; + if (timeout.tv_nsec >= 1000*1000*1000) { timeout.tv_sec++; timeout.tv_nsec -= 1000*1000*1000; } + + if (timeout.tv_sec < now.tv_sec) + timeout.tv_sec = TIMET_MAX; + return timeout; } - static void native_cond_initialize(rb_thread_cond_t *cond, int flags) { -- cgit v1.2.3