-
Notifications
You must be signed in to change notification settings - Fork 419
move attr_volatile into Concurrent::Synchronization::Volatile module #424
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
Merged
pitr-ch
merged 5 commits into
ruby-concurrency:master
from
colinsurprenant:volatile-module
Nov 2, 2015
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
9a80cbb
move attr_volatile impl into module
colinsurprenant e46a38f
expose Concurrent::Synchronization::Volatile module
colinsurprenant fcfae7f
comment voaltile threadContext
colinsurprenant 69669ea
extract attr_volatile as a shared_examples
colinsurprenant a4a9a76
correct example to use volatile method
colinsurprenant File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 |
---|---|---|
@@ -1,35 +1,43 @@ | ||
module Concurrent | ||
module Synchronization | ||
|
||
# @!visibility private | ||
# @!macro internal_implementation_note | ||
class MriObject < AbstractObject | ||
module MriAttrVolatile | ||
def self.included(base) | ||
base.extend(ClassMethods) | ||
end | ||
|
||
def initialize | ||
# nothing to do | ||
module ClassMethods | ||
def attr_volatile(*names) | ||
names.each do |name| | ||
ivar = :"@volatile_#{name}" | ||
class_eval <<-RUBY, __FILE__, __LINE__ + 1 | ||
def #{name} | ||
#{ivar} | ||
end | ||
|
||
def #{name}=(value) | ||
#{ivar} = value | ||
end | ||
RUBY | ||
end | ||
names.map { |n| [n, :"#{n}="] }.flatten | ||
end | ||
end | ||
|
||
def full_memory_barrier | ||
# relying on undocumented behavior of CRuby, GVL acquire has lock which ensures visibility of ivars | ||
# https://github.com/ruby/ruby/blob/ruby_2_2/thread_pthread.c#L204-L211 | ||
end | ||
end | ||
|
||
def self.attr_volatile(*names) | ||
names.each do |name| | ||
ivar = :"@volatile_#{name}" | ||
class_eval <<-RUBY, __FILE__, __LINE__ + 1 | ||
def #{name} | ||
#{ivar} | ||
end | ||
# @!visibility private | ||
# @!macro internal_implementation_note | ||
class MriObject < AbstractObject | ||
include MriAttrVolatile | ||
|
||
def #{name}=(value) | ||
#{ivar} = value | ||
end | ||
RUBY | ||
end | ||
names.map { |n| [n, :"#{n}="] }.flatten | ||
def initialize | ||
# nothing to do | ||
end | ||
end | ||
|
||
end | ||
end |
This file contains hidden or 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 |
---|---|---|
@@ -1,37 +1,45 @@ | ||
module Concurrent | ||
module Synchronization | ||
|
||
module RbxAttrVolatile | ||
def self.included(base) | ||
base.extend(ClassMethods) | ||
end | ||
|
||
# @!visibility private | ||
# @!macro internal_implementation_note | ||
class RbxObject < AbstractObject | ||
def initialize | ||
# nothing to do | ||
module ClassMethods | ||
def attr_volatile(*names) | ||
names.each do |name| | ||
ivar = :"@volatile_#{name}" | ||
class_eval <<-RUBY, __FILE__, __LINE__ + 1 | ||
def #{name} | ||
Rubinius.memory_barrier | ||
#{ivar} | ||
end | ||
|
||
def #{name}=(value) | ||
#{ivar} = value | ||
Rubinius.memory_barrier | ||
end | ||
RUBY | ||
end | ||
names.map { |n| [n, :"#{n}="] }.flatten | ||
end | ||
end | ||
|
||
def full_memory_barrier | ||
# Rubinius instance variables are not volatile so we need to insert barrier | ||
Rubinius.memory_barrier | ||
end | ||
end | ||
|
||
def self.attr_volatile *names | ||
names.each do |name| | ||
ivar = :"@volatile_#{name}" | ||
class_eval <<-RUBY, __FILE__, __LINE__ + 1 | ||
def #{name} | ||
Rubinius.memory_barrier | ||
#{ivar} | ||
end | ||
# @!visibility private | ||
# @!macro internal_implementation_note | ||
class RbxObject < AbstractObject | ||
include RbxAttrVolatile | ||
|
||
def #{name}=(value) | ||
#{ivar} = value | ||
Rubinius.memory_barrier | ||
end | ||
RUBY | ||
end | ||
names.map { |n| [n, :"#{n}="] }.flatten | ||
def initialize | ||
# nothing to do | ||
end | ||
end | ||
|
||
end | ||
end |
This file contains hidden or 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,35 @@ | ||
module Concurrent | ||
module Synchronization | ||
|
||
# Volatile adds the attr_volatile class method when included. | ||
# | ||
# @example | ||
# class Foo | ||
# include Concurrent::Synchronization::Volatile | ||
# | ||
# attr_volatile :bar | ||
# | ||
# def initialize | ||
# self.bar = 1 | ||
# end | ||
# end | ||
# | ||
# foo = Foo.new | ||
# foo.bar | ||
# => 1 | ||
# foo.bar = 2 | ||
# => 2 | ||
|
||
Volatile = case | ||
when Concurrent.on_cruby? | ||
MriAttrVolatile | ||
when Concurrent.on_jruby? | ||
JRubyAttrVolatile | ||
when Concurrent.on_rbx? | ||
RbxAttrVolatile | ||
else | ||
warn 'Possibly unsupported Ruby implementation' | ||
MriAttrVolatile | ||
end | ||
end | ||
end |
Oops, something went wrong.
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.
Could you use same pattern as Object is using, with the Implementation intermediate.
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.
not sure exactly what you are suggesting: I see that in Object there is
private_constant :ObjectImplementation
, but involatile.rb
theVolatile
constant must stay public since the it the module you include, which points to the correct module implementation.or do you mean just adding
to the actual module implementation, like
module JRubyAttrVolatile
?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.
Ah sorry, I did not think it through, it's a module not a class so it is not applicable.
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.
one thing we could do is to maybe DRY up this switch over the ruby implementations.