New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Make CRuby Set thread safe #847
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
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 |
---|---|---|
|
@@ -42,7 +42,7 @@ module Concurrent | |
end | ||
|
||
context 'concurrency' do | ||
it do | ||
it 'simple' do | ||
(1..Concurrent::ThreadSafe::Test::THREADS).map do |i| | ||
in_thread do | ||
1000.times do | ||
|
@@ -55,6 +55,33 @@ module Concurrent | |
end.map(&:join) | ||
expect(set).to be_empty | ||
end | ||
|
||
it 'force context switch' do | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. writing a test for this is actually not easy lol. What this ended up testing was that during a method of set it grabs a monitor and locks out any other changes. expect(set.add?(1)).to eq set the above line should occasionally return nil if the methods were not synchronized. |
||
barrier = Concurrent::CyclicBarrier.new(2) | ||
|
||
# methods like include? or delete? are implemented for CRuby in Ruby itself | ||
# @see https://github.com/ruby/ruby/blob/master/lib/set.rb | ||
set.clear | ||
|
||
# add a single element | ||
set.add(1) | ||
|
||
# This thread should start and `Set#reject!` in CRuby should cache a value of `0` for size | ||
thread_reject = in_thread do | ||
# we expect this to return nil since nothing should have changed. | ||
expect(set.reject! do |v| | ||
barrier.wait | ||
v == 1 # only delete the 1 value | ||
end).to eq set | ||
end | ||
|
||
thread_add = in_thread do | ||
barrier.wait | ||
expect(set.add?(1)).to eq set | ||
end | ||
|
||
join_with [thread_reject, thread_add] | ||
end | ||
end | ||
end | ||
end |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Without this -- certain Sets are being created not through initialize
I would see:
I'm not certain how the Set is being initialized without going through:
TBH i'm