ruby-changes:8604
From: nobu <ko1@a...>
Date: Sat, 8 Nov 2008 01:15:12 +0900 (JST)
Subject: [ruby-changes:8604] Ruby:r20138 (trunk): * thread_pthread.c (thread_timer, rb_thread_create_timer_thread):
nobu 2008-11-08 01:14:48 +0900 (Sat, 08 Nov 2008) New Revision: 20138 http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=rev&revision=20138 Log: * thread_pthread.c (thread_timer, rb_thread_create_timer_thread): handshakes properly. Modified files: trunk/ChangeLog trunk/thread_pthread.c Index: ChangeLog =================================================================== --- ChangeLog (revision 20137) +++ ChangeLog (revision 20138) @@ -1,3 +1,8 @@ +Sat Nov 8 01:14:16 2008 Nobuyoshi Nakada <nobu@r...> + + * thread_pthread.c (thread_timer, rb_thread_create_timer_thread): + handshakes properly. + Fri Nov 7 22:51:49 2008 Nobuyoshi Nakada <nobu@r...> * inits.c (rb_call_inits): do not repeat. Index: thread_pthread.c =================================================================== --- thread_pthread.c (revision 20137) +++ thread_pthread.c (revision 20138) @@ -116,7 +116,13 @@ pthread_cond_wait(cond, mutex); } +static void +native_cond_timedwait(pthread_cond_t *cond, pthread_mutex_t *mutex, struct timespec *ts) +{ + pthread_cond_timedwait(cond, mutex, ts); +} + #define native_cleanup_push pthread_cleanup_push #define native_cleanup_pop pthread_cleanup_pop #ifdef HAVE_SCHED_YIELD @@ -650,6 +656,7 @@ static pthread_t timer_thread_id; static pthread_cond_t timer_thread_cond = PTHREAD_COND_INITIALIZER; +static pthread_mutex_t timer_thread_lock = PTHREAD_MUTEX_INITIALIZER; static struct timespec * get_ts(struct timespec *ts, unsigned long nsec) @@ -669,12 +676,15 @@ thread_timer(void *dummy) { struct timespec ts; - pthread_mutex_t lock; + int err; - pthread_mutex_init(&lock, 0); - pthread_mutex_lock(&lock); -#define WAIT_FOR_10MS() (pthread_cond_timedwait(&timer_thread_cond, &lock, get_ts(&ts, PER_NANO/100)) == ETIMEDOUT) - while (WAIT_FOR_10MS()) { + native_mutex_lock(&timer_thread_lock); + native_cond_signal(&timer_thread_cond); +#define WAIT_FOR_10MS() native_cond_timedwait(&timer_thread_cond, &timer_thread_lock, get_ts(&ts, PER_NANO/100) + while ((err = WAIT_FOR_10MS()) != 0) { + if (err != ETIMEDOUT) { + rb_bug("thread_timer/timedwait: %d", err); + } #ifndef __CYGWIN__ if (signal_thread_list_anchor.next) { FGLOCK(&signal_thread_list_lock, { @@ -689,8 +699,7 @@ #endif timer_thread_function(dummy); } - pthread_mutex_unlock(&lock); - pthread_mutex_destroy(&lock); + native_mutex_unlock(&timer_thread_lock); return NULL; } @@ -708,7 +717,10 @@ pthread_attr_setstacksize(&attr, PTHREAD_STACK_MIN + (THREAD_DEBUG ? BUFSIZ : 0)); #endif + native_mutex_lock(&timer_thread_lock); err = pthread_create(&timer_thread_id, &attr, thread_timer, 0); + native_cond_wait(&timer_thread_cond, &timer_thread_lock); + native_mutex_unlock(&timer_thread_lock); if (err != 0) { rb_bug("rb_thread_create_timer_thread: return non-zero (%d)", err); } @@ -716,6 +728,12 @@ rb_disable_interrupt(); /* only timer thread recieve signal */ } -#define native_stop_timer_thread() pthread_cond_signal(&timer_thread_cond) +static void +native_stop_timer_thread(void) +{ + native_mutex_lock(&timer_thread_lock); + native_cond_signal(&timer_thread_cond); + native_mutex_unlock(&timer_thread_lock); +} #endif /* THREAD_SYSTEM_DEPENDENT_IMPLEMENTATION */ -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/