diff options
author | nobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2009-05-15 08:33:52 +0000 |
---|---|---|
committer | nobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2009-05-15 08:33:52 +0000 |
commit | 072ccff04debff32072e771bc078fe8bc14ccaad (patch) | |
tree | 8a40c5cffae187dca7a2d507b89b19de037d811d | |
parent | faa30cafd475c6f586694910df9767deff97d33e (diff) |
* eval.c (rb_thread_join), ext/thread/thread.c (wake_one): adjusts
targets of rest waiting threads to join. [ruby-core:23457]
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/branches/ruby_1_8@23432 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
-rw-r--r-- | ChangeLog | 5 | ||||
-rw-r--r-- | eval.c | 10 | ||||
-rw-r--r-- | ext/thread/thread.c | 26 | ||||
-rw-r--r-- | test/thread/test_thread.rb | 63 |
4 files changed, 102 insertions, 2 deletions
@@ -1,3 +1,8 @@ +Fri May 15 17:33:51 2009 Nobuyoshi Nakada <[email protected]> + + * eval.c (rb_thread_join), ext/thread/thread.c (wake_one): adjusts + targets of rest waiting threads to join. [ruby-core:23457] + Sat May 9 23:26:04 2009 Akinori MUSHA <[email protected]> * lib/delegate.rb (Delegator#method_missing) @@ -11670,6 +11670,16 @@ rb_thread_join(thread, limit) return rb_thread_join0(rb_thread_check(thread), limit); } +void +rb_thread_set_join(thread, join) + VALUE thread, join; +{ + rb_thread_t th = rb_thread_check(thread); + rb_thread_t jth = rb_thread_check(join); + th->wait_for = WAIT_JOIN; + th->join = jth; +} + /* * call-seq: diff --git a/ext/thread/thread.c b/ext/thread/thread.c index 298d7d08ca..b977fa77cf 100644 --- a/ext/thread/thread.c +++ b/ext/thread/thread.c @@ -205,6 +205,16 @@ array_from_list(List const *list) return ary; } +static void +adjust_join(const List *list, VALUE new) +{ + extern void rb_thread_set_join _((VALUE, VALUE)); + Entry *entry; + for (entry = list->entries; entry; entry = entry->next) { + rb_thread_set_join(entry->value, new); + } +} + static VALUE wake_thread(VALUE thread) { @@ -221,7 +231,7 @@ run_thread(VALUE thread) } static VALUE -wake_one(List *list) +wake_first(List *list) { VALUE waking; @@ -234,10 +244,22 @@ wake_one(List *list) } static VALUE +wake_one(List *list) +{ + VALUE waking = wake_first(list); + + if (!NIL_P(waking)) { + adjust_join(list, waking); + } + + return waking; +} + +static VALUE wake_all(List *list) { while (list->entries) { - wake_one(list); + wake_first(list); } return Qnil; } diff --git a/test/thread/test_thread.rb b/test/thread/test_thread.rb index f34c2ff848..73b148c92f 100644 --- a/test/thread/test_thread.rb +++ b/test/thread/test_thread.rb @@ -85,5 +85,68 @@ class TC_Thread < Test::Unit::TestCase assert_nothing_raised("[ruby-dev:37545]") {assert_equal(1, queue.pop)} assert(queue.empty?) end + + # This test checks that a thread in Mutex#lock which is raised is + # completely removed from the wait_list of the mutex + def test_mutex_exception_handling + m = Mutex.new + m.lock + + sleeping = false + t = Thread.new do + begin + m.lock + rescue + end + + sleeping = true + # Keep that thread alive: if the thread returns, the test method + # won't be able to check that +m+ has not been taken (dead mutex + # owners are ignored) + sleep + end + + # Wait for t to wait for the mutex and raise it + while true + sleep 0.1 + break if t.stop? + end + t.raise ArgumentError + assert(t.alive? || sleeping) + + # Wait for +t+ to reach the sleep + while true + sleep 0.1 + break if t.stop? + end + + # Now unlock. The mutex should be free, so Mutex#unlock should return nil + assert(! m.unlock) + end + + def test_mutex_join + m = Mutex.new + m.lock + wt2 = Thread.new do + m.lock + sleep 0.5 + m.unlock + end + + # Ensure wt2 is waiting on m + sleep 0.1 + + wt1 = Thread.new do + m.lock + m.unlock + end + # Ensure wt1 is waiting on m + sleep 0.1 + + # Give it to wt2 + m.unlock + + assert_nothing_raised {wt1.join} + end end |