-
Notifications
You must be signed in to change notification settings - Fork 186
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
PullRequest: truffleruby/2771
- Loading branch information
Showing
17 changed files
with
361 additions
and
115 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
# Copyright (c) 2021 Oracle and/or its affiliates. All rights reserved. This | ||
# code is released under a tri EPL/GPL/LGPL license. You can use it, | ||
# redistribute it and/or modify it under the terms of the: | ||
# | ||
# Eclipse Public License version 2.0, or | ||
# GNU General Public License version 2, or | ||
# GNU Lesser General Public License version 2.1. | ||
|
||
require 'monitor' | ||
|
||
m = Monitor.new | ||
benchmark 'monitor-synchronize' do | ||
m.synchronize do | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
require_relative '../../spec_helper' | ||
require 'monitor' | ||
|
||
describe "Monitor#enter" do | ||
it "acquires the monitor" do | ||
monitor = Monitor.new | ||
10.times do | ||
wait_q = Queue.new | ||
continue_q = Queue.new | ||
|
||
thread = Thread.new do | ||
begin | ||
monitor.enter | ||
wait_q << true | ||
continue_q.pop | ||
ensure | ||
monitor.exit | ||
end | ||
end | ||
|
||
wait_q.pop | ||
monitor.mon_locked?.should == true | ||
continue_q << true | ||
thread.join | ||
monitor.mon_locked?.should == false | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
require_relative '../../spec_helper' | ||
require 'monitor' | ||
|
||
describe "Monitor#new_cond" do | ||
it "creates a MonitorMixin::ConditionVariable" do | ||
m = Monitor.new | ||
c = m.new_cond | ||
c.class.should == MonitorMixin::ConditionVariable | ||
end | ||
|
||
it 'returns a condition variable which can be waited on by a thread holding the monitor' do | ||
m = Monitor.new | ||
c = m.new_cond | ||
|
||
10.times do | ||
|
||
wait_q = Queue.new | ||
thread = Thread.new do | ||
m.synchronize do | ||
wait_q << true | ||
c.wait | ||
end | ||
:done | ||
end | ||
|
||
wait_q.pop | ||
|
||
# Synchronize can't happen until the other thread is waiting. | ||
m.synchronize { c.signal } | ||
|
||
thread.join | ||
thread.value.should == :done | ||
end | ||
end | ||
|
||
it 'returns a condition variable which can be waited on by a thread holding the monitor inside multiple synchronize blocks' do | ||
m = Monitor.new | ||
c = m.new_cond | ||
|
||
10.times do | ||
|
||
wait_q = Queue.new | ||
thread = Thread.new do | ||
m.synchronize do | ||
m.synchronize do | ||
wait_q << true | ||
c.wait | ||
end | ||
end | ||
:done | ||
end | ||
|
||
wait_q.pop | ||
|
||
#No need to wait here as we cannot synchronize until the other thread is waiting. | ||
m.synchronize { c.signal } | ||
|
||
thread.join | ||
thread.value.should == :done | ||
end | ||
end | ||
|
||
it 'returns a condition variable which can be signalled by a thread holding the monitor inside multiple synchronize blocks' do | ||
m = Monitor.new | ||
c = m.new_cond | ||
|
||
10.times do | ||
|
||
wait_q = Queue.new | ||
thread = Thread.new do | ||
m.synchronize do | ||
wait_q << true | ||
c.wait | ||
end | ||
:done | ||
end | ||
|
||
wait_q.pop | ||
|
||
# Synchronize can't happen until the other thread is waiting. | ||
m.synchronize { m.synchronize { c.signal } } | ||
|
||
thread.join | ||
thread.value.should == :done | ||
end | ||
end | ||
|
||
end |
Oops, something went wrong.