From: Yusuke ENDOH Date: 2008-01-10T20:43:36+09:00 Subject: [ruby-dev:33010] Re: queue and timeout 遠藤と申します。 そもそも誰もロックしてない Mutex を unlock しようとしたときに Attempt to unlock a mutex which is locked by another thread と出るのは不親切ではないでしょうか。知らないとはまりそうです。 Index: thread.c =================================================================== --- thread.c (revision 14978) +++ thread.c (working copy) @@ -2362,22 +2362,30 @@ rb_mutex_unlock(VALUE self) { mutex_t *mutex; + char *err = NULL; GetMutexPtr(self, mutex); - if (mutex->th != GET_THREAD()) { - rb_raise(rb_eThreadError, - "Attempt to unlock a mutex which is locked by another thread"); + native_mutex_lock(&mutex->lock); + + if (mutex->th == 0) { + err = "Attempt to unlock a mutex which is not locked"; } + else if (mutex->th != GET_THREAD()) { + err = "Attempt to unlock a mutex which is locked by another thread"; + } + else { + mutex->th = 0; + if (mutex->cond_waiting > 0) { + /* waiting thread */ + native_cond_signal(&mutex->cond); + mutex->cond_waiting--; + } + } - native_mutex_lock(&mutex->lock); - mutex->th = 0; - if (mutex->cond_waiting > 0) { - /* waiting thread */ - native_cond_signal(&mutex->cond); - mutex->cond_waiting--; - } native_mutex_unlock(&mutex->lock); + if (err) rb_raise(rb_eThreadError, err); + return self; } -- Yusuke ENDOH