diff options
author | normal <normal@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2018-08-18 06:33:49 +0000 |
---|---|---|
committer | normal <normal@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2018-08-18 06:33:49 +0000 |
commit | 647fc1227a4146ecbfeb0d59358abc8d99cd8ae6 (patch) | |
tree | a5c9bd35aaf8ca67b4ff3f27b56726f8a63cca05 /spec/ruby/library/conditionvariable | |
parent | 6e0d69e4a72c7a9d9cf5172bd9b74633d7259f9d (diff) |
thread_sync.c (rb_mutex_synchronize): only unlock if we own the mutex
If an exception is raised inside Mutex#sleep (via ConditionVariable#wait),
we cannot guarantee we can own the mutex in the ensure callback.
However, who owns the mutex at that point does not matter. What
matters is the Mutex is usable after an exception occurs.
* thread_sync.c (rb_mutex_synchronize): only unlock if we own the mutex
* spec/ruby/library/conditionvariable/wait_spec.rb: only test lock
usability after thread kill. Who owns the lock at any
particular moment is an implementation detail which we cannot
easily guarantee.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@64441 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'spec/ruby/library/conditionvariable')
-rw-r--r-- | spec/ruby/library/conditionvariable/wait_spec.rb | 26 |
1 files changed, 8 insertions, 18 deletions
diff --git a/spec/ruby/library/conditionvariable/wait_spec.rb b/spec/ruby/library/conditionvariable/wait_spec.rb index d4950a7b27..99e14efe35 100644 --- a/spec/ruby/library/conditionvariable/wait_spec.rb +++ b/spec/ruby/library/conditionvariable/wait_spec.rb @@ -23,21 +23,15 @@ describe "ConditionVariable#wait" do th.join end - it "reacquires the lock even if the thread is killed" do + it "the lock remains usable even if the thread is killed" do m = Mutex.new cv = ConditionVariable.new in_synchronize = false - owned = nil th = Thread.new do m.synchronize do in_synchronize = true - begin - cv.wait(m) - ensure - owned = m.owned? - $stderr.puts "\nThe Thread doesn't own the Mutex!" unless owned - end + cv.wait(m) end end @@ -49,24 +43,19 @@ describe "ConditionVariable#wait" do th.kill th.join - owned.should == true + m.try_lock.should == true + m.unlock end - it "reacquires the lock even if the thread is killed after being signaled" do + it "lock remains usable even if the thread is killed after being signaled" do m = Mutex.new cv = ConditionVariable.new in_synchronize = false - owned = nil th = Thread.new do m.synchronize do in_synchronize = true - begin - cv.wait(m) - ensure - owned = m.owned? - $stderr.puts "\nThe Thread doesn't own the Mutex!" unless owned - end + cv.wait(m) end end @@ -84,7 +73,8 @@ describe "ConditionVariable#wait" do } th.join - owned.should == true + m.try_lock.should == true + m.unlock end it "supports multiple Threads waiting on the same ConditionVariable and Mutex" do |