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

ruby-changes:26131

From: kosaki <ko1@a...>
Date: Wed, 5 Dec 2012 01:02:17 +0900 (JST)
Subject: [ruby-changes:26131] kosaki:r38188 (trunk): * thread.c (rb_mutex_owned_p): new method that return current

kosaki	2012-12-05 01:02:07 +0900 (Wed, 05 Dec 2012)

  New Revision: 38188

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

  Log:
    * thread.c (rb_mutex_owned_p): new method that return current
      thread have the target mutex or not. [Feature #7505] [ruby-dev:46697]
    * test/ruby/test_thread.rb (test_mutex_owned, test_mutex_owned2):
      test for the above.
    * NEWS: new for the above.

  Modified files:
    trunk/ChangeLog
    trunk/NEWS
    trunk/test/ruby/test_thread.rb
    trunk/thread.c

Index: ChangeLog
===================================================================
--- ChangeLog	(revision 38187)
+++ ChangeLog	(revision 38188)
@@ -1,3 +1,11 @@
+Wed Dec  5 00:56:21 2012  KOSAKI Motohiro  <kosaki.motohiro@g...>
+
+	* thread.c (rb_mutex_owned_p): new method that return current
+	  thread have the target mutex or not. [Feature #7505] [ruby-dev:46697]
+	* test/ruby/test_thread.rb (test_mutex_owned, test_mutex_owned2):
+	  test for the above.
+	* NEWS: new for the above.
+
 Wed Dec  5 00:05:47 2012  Masatoshi SEKI  <m_seki@m...>
 
 	* lib/erb.rb (make_compiler, add_put_cmd, add_insert_cmd): extract
Index: thread.c
===================================================================
--- thread.c	(revision 38187)
+++ thread.c	(revision 38188)
@@ -4251,6 +4251,28 @@
     return self;
 }
 
+/*
+ * call-seq:
+ *    mutex.owned?  -> true or false
+ *
+ * Returns +true+ if this lock is currently held by current thread.
+ * <em>This API is experimental, and subject to change.</em>
+ */
+static VALUE
+rb_mutex_owned_p(VALUE self)
+{
+    VALUE owned = Qfalse;
+    rb_thread_t *th = GET_THREAD();
+    rb_mutex_t *mutex;
+
+    GetMutexPtr(self, mutex);
+
+    if (mutex->th == th)
+	owned = Qtrue;
+
+    return owned;
+}
+
 static const char *
 rb_mutex_unlock_th(rb_mutex_t *mutex, rb_thread_t volatile *th)
 {
@@ -4874,6 +4896,7 @@
     rb_define_method(rb_cMutex, "unlock", rb_mutex_unlock, 0);
     rb_define_method(rb_cMutex, "sleep", mutex_sleep, -1);
     rb_define_method(rb_cMutex, "synchronize", rb_mutex_synchronize_m, 0);
+    rb_define_method(rb_cMutex, "owned?", rb_mutex_owned_p, 0);
 
     recursive_key = rb_intern("__recursive_key__");
     rb_eThreadError = rb_define_class("ThreadError", rb_eStandardError);
Index: NEWS
===================================================================
--- NEWS	(revision 38187)
+++ NEWS	(revision 38188)
@@ -113,6 +113,9 @@
         Object.const_get("Foo::Bar::Baz")
 
   * Mutex
+    * added method:
+      * added Mutex#owned? which returns the mutex is held by current
+        thread or not. [experimental]
     * incompatible changes:
       * Mutex#lock, Mutex#unlock, Mutex#try_lock, Mutex#synchronize
         and Mutex#sleep are no longer allowed to be used from trap handler
Index: test/ruby/test_thread.rb
===================================================================
--- test/ruby/test_thread.rb	(revision 38187)
+++ test/ruby/test_thread.rb	(revision 38188)
@@ -797,4 +797,33 @@
     sleep 0.01
     assert_equal(ary, ["run", "aborting", "aborting"])
   end
+
+  def test_mutex_owned
+    mutex = Mutex.new
+
+    assert_equal(mutex.owned?, false)
+    mutex.synchronize {
+      # Now, I have the mutex
+      assert_equal(mutex.owned?, true)
+    }
+    assert_equal(mutex.owned?, false)
+  end
+
+  def test_mutex_owned2
+    begin
+      mutex = Mutex.new
+      th = Thread.new {
+        # lock forever
+        mutex.lock
+        sleep
+      }
+
+      sleep 0.01 until th.status == "sleep"
+      # acquired another thread.
+      assert_equal(mutex.locked?, true)
+      assert_equal(mutex.owned?, false)
+    ensure
+      th.kill if th
+    end
+  end
 end

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

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