[前][次][番号順一覧][スレッド一覧]

ruby-changes:38444

From: shugo <ko1@a...>
Date: Mon, 18 May 2015 13:56:43 +0900 (JST)
Subject: [ruby-changes:38444] shugo:r50525 (trunk): * lib/monitor.rb (mon_try_enter, mon_enter): should reset @mon_count

shugo	2015-05-18 13:56:22 +0900 (Mon, 18 May 2015)

  New Revision: 50525

  http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=50525

  Log:
    * lib/monitor.rb (mon_try_enter, mon_enter): should reset @mon_count
      just in case the previous owner thread dies without mon_exit.
      [fix GH-874] Patch by @chrisberkhout

  Modified files:
    trunk/ChangeLog
    trunk/lib/monitor.rb
    trunk/test/monitor/test_monitor.rb
Index: ChangeLog
===================================================================
--- ChangeLog	(revision 50524)
+++ ChangeLog	(revision 50525)
@@ -1,3 +1,9 @@ https://github.com/ruby/ruby/blob/trunk/ChangeLog#L1
+Mon May 18 13:55:01 2015  Shugo Maeda  <shugo@r...>
+
+	* lib/monitor.rb (mon_try_enter, mon_enter): should reset @mon_count
+	  just in case the previous owner thread dies without mon_exit.
+	  [fix GH-874] Patch by @chrisberkhout
+
 Sun May 17 17:21:29 2015  Eric Wong  <e@8...>
 
 	* lib/webrick/utils.rb (set_non_blocking): use IO#nonblock=
Index: lib/monitor.rb
===================================================================
--- lib/monitor.rb	(revision 50524)
+++ lib/monitor.rb	(revision 50525)
@@ -170,6 +170,7 @@ module MonitorMixin https://github.com/ruby/ruby/blob/trunk/lib/monitor.rb#L170
         return false
       end
       @mon_owner = Thread.current
+      @mon_count = 0
     end
     @mon_count += 1
     return true
@@ -184,6 +185,7 @@ module MonitorMixin https://github.com/ruby/ruby/blob/trunk/lib/monitor.rb#L185
     if @mon_owner != Thread.current
       @mon_mutex.lock
       @mon_owner = Thread.current
+      @mon_count = 0
     end
     @mon_count += 1
   end
Index: test/monitor/test_monitor.rb
===================================================================
--- test/monitor/test_monitor.rb	(revision 50524)
+++ test/monitor/test_monitor.rb	(revision 50525)
@@ -33,6 +33,22 @@ class TestMonitor < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/monitor/test_monitor.rb#L33
     assert_equal((1..10).to_a, ary)
   end
 
+  def test_enter_second_after_killed_thread
+    th = Thread.start {
+      @monitor.enter
+      Thread.current.kill
+      @monitor.exit
+    }
+    th.join
+    @monitor.enter
+    @monitor.exit
+    th2 = Thread.start {
+      @monitor.enter
+      @monitor.exit
+    }
+    assert_join_threads([th, th2])
+  end
+
   def test_synchronize
     ary = []
     queue = Queue.new
@@ -110,6 +126,22 @@ class TestMonitor < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/monitor/test_monitor.rb#L126
     }
     assert_join_threads([th, th2])
   end
+
+  def test_try_enter_second_after_killed_thread
+    th = Thread.start {
+      assert_equal(true, @monitor.try_enter)
+      Thread.current.kill
+      @monitor.exit
+    }
+    th.join
+    assert_equal(true, @monitor.try_enter)
+    @monitor.exit
+    th2 = Thread.start {
+      assert_equal(true, @monitor.try_enter)
+      @monitor.exit
+    }
+    assert_join_threads([th, th2])
+  end
 
   def test_cond
     cond = @monitor.new_cond

--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]