Skip to content

Instantly share code, notes, and snippets.

@nahi
Created July 9, 2009 06:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nahi/143475 to your computer and use it in GitHub Desktop.
Save nahi/143475 to your computer and use it in GitHub Desktop.
Index: lib/monitor.rb
===================================================================
--- lib/monitor.rb (revision 23996)
+++ lib/monitor.rb (working copy)
@@ -288,11 +288,15 @@
@mon_owner = Thread.current
end
+ # mon_release requires Thread.critical == true
def mon_release
@mon_owner = nil
- t = @mon_waiting_queue.shift
- t = @mon_entering_queue.shift unless t
- t.wakeup if t
+ while t = @mon_waiting_queue.shift || @mon_entering_queue.shift
+ if t.alive?
+ t.wakeup
+ return
+ end
+ end
end
def mon_enter_for_cond(count)
Index: test/monitor/test_monitor.rb
===================================================================
--- test/monitor/test_monitor.rb (revision 23996)
+++ test/monitor/test_monitor.rb (working copy)
@@ -54,6 +54,31 @@
assert_equal((1..10).to_a, ary)
end
+ def test_killed_thread_in_synchronize
+ ary = []
+ queue = Queue.new
+ t1 = Thread.start {
+ queue.pop
+ @monitor.synchronize {
+ ary << :t1
+ }
+ }
+ t2 = Thread.start {
+ queue.pop
+ @monitor.synchronize {
+ ary << :t2
+ }
+ }
+ @monitor.synchronize do
+ queue.enq(nil)
+ queue.enq(nil)
+ t1.kill
+ t2.kill
+ ary << :main
+ end
+ assert_equal([:main], ary)
+ end
+
def test_try_enter
queue1 = Queue.new
queue2 = Queue.new
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment