From bbfbf662cceb6e7d8dc769ca365c563b9c073592 Mon Sep 17 00:00:00 2001 From: kosaki Date: Tue, 4 Dec 2012 16:02:07 +0000 Subject: * 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. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@38188 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- test/ruby/test_thread.rb | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) (limited to 'test/ruby') diff --git a/test/ruby/test_thread.rb b/test/ruby/test_thread.rb index 0149e17d53..e180310505 100644 --- a/test/ruby/test_thread.rb +++ b/test/ruby/test_thread.rb @@ -797,4 +797,33 @@ Thread.new(Thread.current) {|mth| 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 -- cgit v1.2.3