summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--ChangeLog9
-rw-r--r--test/ruby/test_thread.rb12
-rw-r--r--thread.c9
3 files changed, 26 insertions, 4 deletions
diff --git a/ChangeLog b/ChangeLog
index 1eb49c5d02..a433bb4b36 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,12 @@
+Mon Dec 29 11:58:39 2008 Koichi Sasada <ko1@atdot.net>
+
+ * 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@ruby-lang.org>
* file.c (rb_get_path): move encoding conversion of file path
diff --git a/test/ruby/test_thread.rb b/test/ruby/test_thread.rb
index 97e58cd9ea..b460cadea9 100644
--- a/test/ruby/test_thread.rb
+++ b/test/ruby/test_thread.rb
@@ -438,6 +438,18 @@ class TestThread < Test::Unit::TestCase
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
diff --git a/thread.c b/thread.c
index b21af09db9..4f5f150650 100644
--- a/thread.c
+++ b/thread.c
@@ -2796,10 +2796,6 @@ rb_mutex_trylock(VALUE self)
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 @@ lock_interrupt(void *ptr)
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;