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

ruby-changes:47851

From: shugo <ko1@a...>
Date: Wed, 20 Sep 2017 10:40:57 +0900 (JST)
Subject: [ruby-changes:47851] shugo:r59971 (trunk): Add MonitorMinx#mon_locked? and #mon_owned? to check states of objects

shugo	2017-09-20 10:40:53 +0900 (Wed, 20 Sep 2017)

  New Revision: 59971

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

  Log:
    Add MonitorMinx#mon_locked? and #mon_owned? to check states of objects
    
    Patched by Satoshi "Moris" Tagomori <tagomoris@g...>.  [Fix GH-1699]

  Modified files:
    trunk/lib/monitor.rb
    trunk/test/monitor/test_monitor.rb
Index: test/monitor/test_monitor.rb
===================================================================
--- test/monitor/test_monitor.rb	(revision 59970)
+++ test/monitor/test_monitor.rb	(revision 59971)
@@ -146,6 +146,35 @@ class TestMonitor < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/monitor/test_monitor.rb#L146
     assert_join_threads([th, th2])
   end
 
+  def test_mon_locked_and_owned
+    queue1 = Queue.new
+    queue2 = Queue.new
+    th = Thread.start {
+      @monitor.enter
+      queue1.enq(nil)
+      queue2.deq
+      @monitor.exit
+      queue1.enq(nil)
+    }
+    queue1.deq
+    assert(@monitor.mon_locked?)
+    assert(!@monitor.mon_owned?)
+
+    queue2.enq(nil)
+    queue1.deq
+    assert(!@monitor.mon_locked?)
+
+    @monitor.enter
+    assert @monitor.mon_locked?
+    assert @monitor.mon_owned?
+    @monitor.exit
+
+    @monitor.synchronize do
+      assert @monitor.mon_locked?
+      assert @monitor.mon_owned?
+    end
+  end
+
   def test_cond
     cond = @monitor.new_cond
 
Index: lib/monitor.rb
===================================================================
--- lib/monitor.rb	(revision 59970)
+++ lib/monitor.rb	(revision 59971)
@@ -204,6 +204,20 @@ module MonitorMixin https://github.com/ruby/ruby/blob/trunk/lib/monitor.rb#L204
   end
 
   #
+  # Returns true if this monitor is locked by any thread
+  #
+  def mon_locked?
+    @mon_mutex.locked?
+  end
+
+  #
+  # Returns true if this monitor is locked by current thread.
+  #
+  def mon_owned?
+    @mon_mutex.locked? && @mon_owner == Thread.current
+  end
+
+  #
   # Enters exclusive section and executes the block.  Leaves the exclusive
   # section automatically when the block exits.  See example under
   # +MonitorMixin+.

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

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