ruby-changes:9608
From: ko1 <ko1@a...>
Date: Mon, 29 Dec 2008 12:03:25 +0900 (JST)
Subject: [ruby-changes:9608] Ruby:r21148 (trunk): * thread.c (rb_mutex_trylock): return false if Mutex owned
ko1 2008-12-29 12:03:09 +0900 (Mon, 29 Dec 2008) New Revision: 21148 http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=rev&revision=21148 Log: * thread.c (rb_mutex_trylock): return false if Mutex owned by current thread. [ruby-core:20943] * thread.c (rb_mutex_lock): check dead lock (recursive lock) here. * test/ruby/test_thread.rb: add a test. Modified files: trunk/ChangeLog trunk/test/ruby/test_thread.rb trunk/thread.c Index: ChangeLog =================================================================== --- ChangeLog (revision 21147) +++ ChangeLog (revision 21148) @@ -1,3 +1,12 @@ +Mon Dec 29 11:58:39 2008 Koichi Sasada <ko1@a...> + + * thread.c (rb_mutex_trylock): return false if Mutex owned + by current thread. [ruby-core:20943] + + * thread.c (rb_mutex_lock): check dead lock (recursive lock) here. + + * test/ruby/test_thread.rb: add a test. + Mon Dec 29 10:58:54 2008 NARUSE, Yui <naruse@r...> * file.c (rb_get_path): move encoding conversion of file path Index: thread.c =================================================================== --- thread.c (revision 21147) +++ thread.c (revision 21148) @@ -2796,10 +2796,6 @@ VALUE locked = Qfalse; GetMutexPtr(self, mutex); - if (mutex->th == GET_THREAD()) { - rb_raise(rb_eThreadError, "deadlock; recursive locking"); - } - native_mutex_lock(&mutex->lock); if (mutex->th == 0) { mutex->th = GET_THREAD(); @@ -2871,11 +2867,16 @@ VALUE rb_mutex_lock(VALUE self) { + if (rb_mutex_trylock(self) == Qfalse) { mutex_t *mutex; rb_thread_t *th = GET_THREAD(); GetMutexPtr(self, mutex); + if (mutex->th == GET_THREAD()) { + rb_raise(rb_eThreadError, "deadlock; recursive locking"); + } + while (mutex->th != th) { int interrupted; enum rb_thread_status prev_status = th->status; Index: test/ruby/test_thread.rb =================================================================== --- test/ruby/test_thread.rb (revision 21147) +++ test/ruby/test_thread.rb (revision 21148) @@ -438,6 +438,18 @@ assert_equal(false, m3.locked?) end + def test_mutex_trylock + m = Mutex.new + assert_equal(true, m.try_lock) + assert_equal(false, m.try_lock, '[ruby-core:20943]') + + Thread.new{ + assert_equal(false, m.try_lock) + }.join + + m.unlock + end + def test_recursive_error o = Object.new def o.inspect -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/