Skip to content
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

Conversation

fzakaria
Copy link
Contributor

Follow similar pattern for Rbx to wrap every superclass (::Set) instance
method with a Monitor (which is re-entrant).

Included is a test that I believe would occasionally fail
if Set was not thread safe.

fixes #796

Follow similar pattern for Rbx to wrap every superclass (::Set) instance
method with a Monitor (which is re-entrant).

Included is a test that I believe *would* occasionally fail
if Set was not thread safe.

fixes ruby-concurrency#796

class CRubySet < ::Set
def initialize(*args)
self.send(:_mon_initialize)
Copy link
Contributor Author

@fzakaria fzakaria Feb 10, 2020

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:

when initializing with a block argument
concurrent-ruby/concurrent/thread_safe/util/data_structures.rb:43: 
warning: instance variable @_monitor not initialized

I'm not certain how the Set is being initialized without going through:

 def self.new(*args)
    obj = super(*args)
    obj.send(:_mon_initialize)
    obj
  end

TBH i'm

@@ -55,6 +55,33 @@ module Concurrent
end.map(&:join)
expect(set).to be_empty
end

it 'force context switch' do
Copy link
Contributor Author

Choose a reason for hiding this comment

The 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.

@pitr-ch
Copy link
Member

pitr-ch commented Jun 4, 2021

Replaced by #911

@pitr-ch pitr-ch closed this Jun 4, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Concurrent::Set is not thread-safe in CRuby
2 participants