def attr_volatile(*attr_names)
return if attr_names.empty?
include(Module.new do
atomic_ref_setup = attr_names.map {|attr_name| "@__#{attr_name} = ThreadSafe::Util::AtomicReference.new"}
initialize_copy_setup = attr_names.zip(atomic_ref_setup).map do |attr_name, ref_setup|
"#{ref_setup}(other.instance_variable_get(:@__#{attr_name}).get)"
end
class_eval "def initialize(*)\nsuper\n\#{atomic_ref_setup.join('; ')}\nend\n\ndef initialize_copy(other)\nsuper\n\#{initialize_copy_setup.join('; ')}\nend\n", __FILE__, __LINE__ + 1
attr_names.each do |attr_name|
class_eval "def \#{attr_name}\n@__\#{attr_name}.get\nend\n\ndef \#{attr_name}=(value)\n@__\#{attr_name}.set(value)\nend\n\ndef compare_and_set_\#{attr_name}(old_value, new_value)\n@__\#{attr_name}.compare_and_set(old_value, new_value)\nend\n", __FILE__, __LINE__ + 1
alias_method "cas_#{attr_name}""cas_#{attr_name}", "compare_and_set_#{attr_name}""compare_and_set_#{attr_name}"
alias_method "lazy_set_#{attr_name}""lazy_set_#{attr_name}", "#{attr_name}=""#{attr_name}="
end
end