Update concurrent-ruby to 1.1.3.
This commit is contained in:
parent
81021e6a60
commit
26e7820943
@ -3,7 +3,7 @@ require 'rbconfig'
|
||||
ruby_engine = defined?(RUBY_ENGINE) ? RUBY_ENGINE : 'ruby'
|
||||
ruby_version = RbConfig::CONFIG["ruby_version"]
|
||||
path = File.expand_path('..', __FILE__)
|
||||
$:.unshift "#{path}/../#{ruby_engine}/#{ruby_version}/gems/concurrent-ruby-1.0.5/lib"
|
||||
$:.unshift "#{path}/../#{ruby_engine}/#{ruby_version}/gems/concurrent-ruby-1.1.3/lib"
|
||||
$:.unshift "#{path}/../#{ruby_engine}/#{ruby_version}/gems/i18n-1.1.1/lib"
|
||||
$:.unshift "#{path}/../#{ruby_engine}/#{ruby_version}/gems/minitest-5.11.3/lib"
|
||||
$:.unshift "#{path}/../#{ruby_engine}/#{ruby_version}/gems/thread_safe-0.3.6/lib"
|
||||
|
||||
@ -1,240 +0,0 @@
|
||||
require 'thread'
|
||||
require 'concurrent/constants'
|
||||
require 'concurrent/synchronization'
|
||||
|
||||
module Concurrent
|
||||
# @!visibility private
|
||||
module Collection
|
||||
|
||||
# @!visibility private
|
||||
MapImplementation = if Concurrent.java_extensions_loaded?
|
||||
# noinspection RubyResolve
|
||||
JRubyMapBackend
|
||||
elsif defined?(RUBY_ENGINE)
|
||||
case RUBY_ENGINE
|
||||
when 'ruby'
|
||||
require 'concurrent/collection/map/mri_map_backend'
|
||||
MriMapBackend
|
||||
when 'rbx'
|
||||
require 'concurrent/collection/map/atomic_reference_map_backend'
|
||||
AtomicReferenceMapBackend
|
||||
when 'jruby+truffle'
|
||||
require 'concurrent/collection/map/atomic_reference_map_backend'
|
||||
AtomicReferenceMapBackend
|
||||
else
|
||||
warn 'Concurrent::Map: unsupported Ruby engine, using a fully synchronized Concurrent::Map implementation' if $VERBOSE
|
||||
require 'concurrent/collection/map/synchronized_map_backend'
|
||||
SynchronizedMapBackend
|
||||
end
|
||||
else
|
||||
MriMapBackend
|
||||
end
|
||||
end
|
||||
|
||||
# `Concurrent::Map` is a hash-like object and should have much better performance
|
||||
# characteristics, especially under high concurrency, than `Concurrent::Hash`.
|
||||
# However, `Concurrent::Map `is not strictly semantically equivalent to a ruby `Hash`
|
||||
# -- for instance, it does not necessarily retain ordering by insertion time as `Hash`
|
||||
# does. For most uses it should do fine though, and we recommend you consider
|
||||
# `Concurrent::Map` instead of `Concurrent::Hash` for your concurrency-safe hash needs.
|
||||
#
|
||||
# > require 'concurrent'
|
||||
# >
|
||||
# > map = Concurrent::Map.new
|
||||
class Map < Collection::MapImplementation
|
||||
|
||||
# @!macro [new] map_method_is_atomic
|
||||
# This method is atomic. Atomic methods of `Map` which accept a block
|
||||
# do not allow the `self` instance to be used within the block. Doing
|
||||
# so will cause a deadlock.
|
||||
|
||||
# @!method put_if_absent
|
||||
# @!macro map_method_is_atomic
|
||||
|
||||
# @!method compute_if_absent
|
||||
# @!macro map_method_is_atomic
|
||||
|
||||
# @!method compute_if_present
|
||||
# @!macro map_method_is_atomic
|
||||
|
||||
# @!method compute
|
||||
# @!macro map_method_is_atomic
|
||||
|
||||
# @!method merge_pair
|
||||
# @!macro map_method_is_atomic
|
||||
|
||||
# @!method replace_pair
|
||||
# @!macro map_method_is_atomic
|
||||
|
||||
# @!method replace_if_exists
|
||||
# @!macro map_method_is_atomic
|
||||
|
||||
# @!method get_and_set
|
||||
# @!macro map_method_is_atomic
|
||||
|
||||
# @!method delete
|
||||
# @!macro map_method_is_atomic
|
||||
|
||||
# @!method delete_pair
|
||||
# @!macro map_method_is_atomic
|
||||
|
||||
def initialize(options = nil, &block)
|
||||
if options.kind_of?(::Hash)
|
||||
validate_options_hash!(options)
|
||||
else
|
||||
options = nil
|
||||
end
|
||||
|
||||
super(options)
|
||||
@default_proc = block
|
||||
end
|
||||
|
||||
def [](key)
|
||||
if value = super # non-falsy value is an existing mapping, return it right away
|
||||
value
|
||||
# re-check is done with get_or_default(key, NULL) instead of a simple !key?(key) in order to avoid a race condition, whereby by the time the current thread gets to the key?(key) call
|
||||
# a key => value mapping might have already been created by a different thread (key?(key) would then return true, this elsif branch wouldn't be taken and an incorrent +nil+ value
|
||||
# would be returned)
|
||||
# note: nil == value check is not technically necessary
|
||||
elsif @default_proc && nil == value && NULL == (value = get_or_default(key, NULL))
|
||||
@default_proc.call(self, key)
|
||||
else
|
||||
value
|
||||
end
|
||||
end
|
||||
|
||||
alias_method :get, :[]
|
||||
alias_method :put, :[]=
|
||||
|
||||
# @!macro [attach] map_method_not_atomic
|
||||
# The "fetch-then-act" methods of `Map` are not atomic. `Map` is intended
|
||||
# to be use as a concurrency primitive with strong happens-before
|
||||
# guarantees. It is not intended to be used as a high-level abstraction
|
||||
# supporting complex operations. All read and write operations are
|
||||
# thread safe, but no guarantees are made regarding race conditions
|
||||
# between the fetch operation and yielding to the block. Additionally,
|
||||
# this method does not support recursion. This is due to internal
|
||||
# constraints that are very unlikely to change in the near future.
|
||||
def fetch(key, default_value = NULL)
|
||||
if NULL != (value = get_or_default(key, NULL))
|
||||
value
|
||||
elsif block_given?
|
||||
yield key
|
||||
elsif NULL != default_value
|
||||
default_value
|
||||
else
|
||||
raise_fetch_no_key
|
||||
end
|
||||
end
|
||||
|
||||
# @!macro map_method_not_atomic
|
||||
def fetch_or_store(key, default_value = NULL)
|
||||
fetch(key) do
|
||||
put(key, block_given? ? yield(key) : (NULL == default_value ? raise_fetch_no_key : default_value))
|
||||
end
|
||||
end
|
||||
|
||||
# @!macro map_method_is_atomic
|
||||
def put_if_absent(key, value)
|
||||
computed = false
|
||||
result = compute_if_absent(key) do
|
||||
computed = true
|
||||
value
|
||||
end
|
||||
computed ? nil : result
|
||||
end unless method_defined?(:put_if_absent)
|
||||
|
||||
def value?(value)
|
||||
each_value do |v|
|
||||
return true if value.equal?(v)
|
||||
end
|
||||
false
|
||||
end
|
||||
|
||||
def keys
|
||||
arr = []
|
||||
each_pair {|k, v| arr << k}
|
||||
arr
|
||||
end unless method_defined?(:keys)
|
||||
|
||||
def values
|
||||
arr = []
|
||||
each_pair {|k, v| arr << v}
|
||||
arr
|
||||
end unless method_defined?(:values)
|
||||
|
||||
def each_key
|
||||
each_pair {|k, v| yield k}
|
||||
end unless method_defined?(:each_key)
|
||||
|
||||
def each_value
|
||||
each_pair {|k, v| yield v}
|
||||
end unless method_defined?(:each_value)
|
||||
|
||||
alias_method :each, :each_pair unless method_defined?(:each)
|
||||
|
||||
def key(value)
|
||||
each_pair {|k, v| return k if v == value}
|
||||
nil
|
||||
end unless method_defined?(:key)
|
||||
alias_method :index, :key if RUBY_VERSION < '1.9'
|
||||
|
||||
def empty?
|
||||
each_pair {|k, v| return false}
|
||||
true
|
||||
end unless method_defined?(:empty?)
|
||||
|
||||
def size
|
||||
count = 0
|
||||
each_pair {|k, v| count += 1}
|
||||
count
|
||||
end unless method_defined?(:size)
|
||||
|
||||
def marshal_dump
|
||||
raise TypeError, "can't dump hash with default proc" if @default_proc
|
||||
h = {}
|
||||
each_pair {|k, v| h[k] = v}
|
||||
h
|
||||
end
|
||||
|
||||
def marshal_load(hash)
|
||||
initialize
|
||||
populate_from(hash)
|
||||
end
|
||||
|
||||
undef :freeze
|
||||
|
||||
# @!visibility private
|
||||
DEFAULT_OBJ_ID_STR_WIDTH = 0.size == 4 ? 7 : 14 # we want to look "native", 7 for 32-bit, 14 for 64-bit
|
||||
# override default #inspect() method: firstly, we don't want to be spilling our guts (i-vars), secondly, MRI backend's
|
||||
# #inspect() call on its @backend i-var will bump @backend's iter level while possibly yielding GVL
|
||||
def inspect
|
||||
id_str = (object_id << 1).to_s(16).rjust(DEFAULT_OBJ_ID_STR_WIDTH, '0')
|
||||
"#<#{self.class.name}:0x#{id_str} entries=#{size} default_proc=#{@default_proc.inspect}>"
|
||||
end
|
||||
|
||||
private
|
||||
def raise_fetch_no_key
|
||||
raise KeyError, 'key not found'
|
||||
end
|
||||
|
||||
def initialize_copy(other)
|
||||
super
|
||||
populate_from(other)
|
||||
end
|
||||
|
||||
def populate_from(hash)
|
||||
hash.each_pair {|k, v| self[k] = v}
|
||||
self
|
||||
end
|
||||
|
||||
def validate_options_hash!(options)
|
||||
if (initial_capacity = options[:initial_capacity]) && (!initial_capacity.kind_of?(Integer) || initial_capacity < 0)
|
||||
raise ArgumentError, ":initial_capacity must be a positive Integer"
|
||||
end
|
||||
if (load_factor = options[:load_factor]) && (!load_factor.kind_of?(Numeric) || load_factor <= 0 || load_factor > 1)
|
||||
raise ArgumentError, ":load_factor must be a number between 0 and 1"
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
@ -1,9 +0,0 @@
|
||||
module Concurrent
|
||||
module Synchronization
|
||||
class TruffleLockableObject < AbstractLockableObject
|
||||
def new(*)
|
||||
raise NotImplementedError
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
@ -1,31 +0,0 @@
|
||||
module Concurrent
|
||||
module Synchronization
|
||||
|
||||
module TruffleAttrVolatile
|
||||
def self.included(base)
|
||||
base.extend(ClassMethods)
|
||||
end
|
||||
|
||||
module ClassMethods
|
||||
def attr_volatile(*names)
|
||||
# TODO may not always be available
|
||||
attr_atomic(*names)
|
||||
end
|
||||
end
|
||||
|
||||
def full_memory_barrier
|
||||
Truffle::System.full_memory_barrier
|
||||
end
|
||||
end
|
||||
|
||||
# @!visibility private
|
||||
# @!macro internal_implementation_note
|
||||
class TruffleObject < AbstractObject
|
||||
include TruffleAttrVolatile
|
||||
|
||||
def initialize
|
||||
# nothing to do
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
@ -1,73 +0,0 @@
|
||||
require 'concurrent/utility/engine'
|
||||
|
||||
module Concurrent
|
||||
|
||||
module Utility
|
||||
|
||||
# @!visibility private
|
||||
module NativeExtensionLoader
|
||||
|
||||
def allow_c_extensions?
|
||||
Concurrent.on_cruby?
|
||||
end
|
||||
|
||||
def c_extensions_loaded?
|
||||
@c_extensions_loaded ||= false
|
||||
end
|
||||
|
||||
def java_extensions_loaded?
|
||||
@java_extensions_loaded ||= false
|
||||
end
|
||||
|
||||
def set_c_extensions_loaded
|
||||
@c_extensions_loaded = true
|
||||
end
|
||||
|
||||
def set_java_extensions_loaded
|
||||
@java_extensions_loaded = true
|
||||
end
|
||||
|
||||
def load_native_extensions
|
||||
unless defined? Synchronization::AbstractObject
|
||||
raise 'native_extension_loader loaded before Synchronization::AbstractObject'
|
||||
end
|
||||
|
||||
if Concurrent.on_cruby? && !c_extensions_loaded?
|
||||
tries = [
|
||||
lambda do
|
||||
require 'concurrent/extension'
|
||||
set_c_extensions_loaded
|
||||
end,
|
||||
lambda do
|
||||
# may be a Windows cross-compiled native gem
|
||||
require "concurrent/#{RUBY_VERSION[0..2]}/extension"
|
||||
set_c_extensions_loaded
|
||||
end]
|
||||
|
||||
tries.each do |try|
|
||||
begin
|
||||
try.call
|
||||
break
|
||||
rescue LoadError
|
||||
next
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
if Concurrent.on_jruby? && !java_extensions_loaded?
|
||||
begin
|
||||
require 'concurrent_ruby_ext'
|
||||
set_java_extensions_loaded
|
||||
rescue LoadError
|
||||
# move on with pure-Ruby implementations
|
||||
raise 'On JRuby but Java extensions failed to load.'
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
# @!visibility private
|
||||
extend Utility::NativeExtensionLoader
|
||||
end
|
||||
|
||||
@ -10,7 +10,7 @@ module Concurrent
|
||||
|
||||
# WARNING: all public methods of the class must operate on the @backend
|
||||
# directly without calling each other. This is important because of the
|
||||
# SynchronizedMapBackend which uses a non-reentrant mutex for perfomance
|
||||
# SynchronizedMapBackend which uses a non-reentrant mutex for performance
|
||||
# reasons.
|
||||
def initialize(options = nil)
|
||||
@backend = {}
|
||||
@ -95,7 +95,6 @@ module Concurrent
|
||||
end
|
||||
|
||||
def each_pair
|
||||
return enum_for :each_pair unless block_given?
|
||||
dupped_backend.each_pair do |k, v|
|
||||
yield k, v
|
||||
end
|
||||
@ -3,6 +3,6 @@ module Concurrent
|
||||
# Various classes within allows for +nil+ values to be stored,
|
||||
# so a special +NULL+ token is required to indicate the "nil-ness".
|
||||
# @!visibility private
|
||||
NULL = Object.new
|
||||
NULL = ::Object.new
|
||||
|
||||
end
|
||||
@ -0,0 +1,337 @@
|
||||
require 'thread'
|
||||
require 'concurrent/constants'
|
||||
require 'concurrent/synchronization'
|
||||
require 'concurrent/utility/engine'
|
||||
|
||||
module Concurrent
|
||||
# @!visibility private
|
||||
module Collection
|
||||
|
||||
# @!visibility private
|
||||
MapImplementation = case
|
||||
when Concurrent.on_jruby?
|
||||
# noinspection RubyResolve
|
||||
JRubyMapBackend
|
||||
when Concurrent.on_cruby?
|
||||
require 'concurrent/collection/map/mri_map_backend'
|
||||
MriMapBackend
|
||||
when Concurrent.on_rbx? || Concurrent.on_truffleruby?
|
||||
require 'concurrent/collection/map/atomic_reference_map_backend'
|
||||
AtomicReferenceMapBackend
|
||||
else
|
||||
warn 'Concurrent::Map: unsupported Ruby engine, using a fully synchronized Concurrent::Map implementation'
|
||||
require 'concurrent/collection/map/synchronized_map_backend'
|
||||
SynchronizedMapBackend
|
||||
end
|
||||
end
|
||||
|
||||
# `Concurrent::Map` is a hash-like object and should have much better performance
|
||||
# characteristics, especially under high concurrency, than `Concurrent::Hash`.
|
||||
# However, `Concurrent::Map `is not strictly semantically equivalent to a ruby `Hash`
|
||||
# -- for instance, it does not necessarily retain ordering by insertion time as `Hash`
|
||||
# does. For most uses it should do fine though, and we recommend you consider
|
||||
# `Concurrent::Map` instead of `Concurrent::Hash` for your concurrency-safe hash needs.
|
||||
class Map < Collection::MapImplementation
|
||||
|
||||
# @!macro map.atomic_method
|
||||
# This method is atomic.
|
||||
|
||||
# @!macro map.atomic_method_with_block
|
||||
# This method is atomic.
|
||||
# @note Atomic methods taking a block do not allow the `self` instance
|
||||
# to be used within the block. Doing so will cause a deadlock.
|
||||
|
||||
# @!method compute_if_absent(key)
|
||||
# Compute and store new value for key if the key is absent.
|
||||
# @param [Object] key
|
||||
# @yield new value
|
||||
# @yieldreturn [Object] new value
|
||||
# @return [Object] new value or current value
|
||||
# @!macro map.atomic_method_with_block
|
||||
|
||||
# @!method compute_if_present(key)
|
||||
# Compute and store new value for key if the key is present.
|
||||
# @param [Object] key
|
||||
# @yield new value
|
||||
# @yieldparam old_value [Object]
|
||||
# @yieldreturn [Object, nil] new value, when nil the key is removed
|
||||
# @return [Object, nil] new value or nil
|
||||
# @!macro map.atomic_method_with_block
|
||||
|
||||
# @!method compute(key)
|
||||
# Compute and store new value for key.
|
||||
# @param [Object] key
|
||||
# @yield compute new value from old one
|
||||
# @yieldparam old_value [Object, nil] old_value, or nil when key is absent
|
||||
# @yieldreturn [Object, nil] new value, when nil the key is removed
|
||||
# @return [Object, nil] new value or nil
|
||||
# @!macro map.atomic_method_with_block
|
||||
|
||||
# @!method merge_pair(key, value)
|
||||
# If the key is absent, the value is stored, otherwise new value is
|
||||
# computed with a block.
|
||||
# @param [Object] key
|
||||
# @param [Object] value
|
||||
# @yield compute new value from old one
|
||||
# @yieldparam old_value [Object] old value
|
||||
# @yieldreturn [Object, nil] new value, when nil the key is removed
|
||||
# @return [Object, nil] new value or nil
|
||||
# @!macro map.atomic_method_with_block
|
||||
|
||||
# @!method replace_pair(key, old_value, new_value)
|
||||
# Replaces old_value with new_value if key exists and current value
|
||||
# matches old_value
|
||||
# @param [Object] key
|
||||
# @param [Object] old_value
|
||||
# @param [Object] new_value
|
||||
# @return [true, false] true if replaced
|
||||
# @!macro map.atomic_method
|
||||
|
||||
# @!method replace_if_exists(key, new_value)
|
||||
# Replaces current value with new_value if key exists
|
||||
# @param [Object] key
|
||||
# @param [Object] new_value
|
||||
# @return [Object, nil] old value or nil
|
||||
# @!macro map.atomic_method
|
||||
|
||||
# @!method get_and_set(key, value)
|
||||
# Get the current value under key and set new value.
|
||||
# @param [Object] key
|
||||
# @param [Object] value
|
||||
# @return [Object, nil] old value or nil when the key was absent
|
||||
# @!macro map.atomic_method
|
||||
|
||||
# @!method delete(key)
|
||||
# Delete key and its value.
|
||||
# @param [Object] key
|
||||
# @return [Object, nil] old value or nil when the key was absent
|
||||
# @!macro map.atomic_method
|
||||
|
||||
# @!method delete_pair(key, value)
|
||||
# Delete pair and its value if current value equals the provided value.
|
||||
# @param [Object] key
|
||||
# @param [Object] value
|
||||
# @return [true, false] true if deleted
|
||||
# @!macro map.atomic_method
|
||||
|
||||
|
||||
def initialize(options = nil, &block)
|
||||
if options.kind_of?(::Hash)
|
||||
validate_options_hash!(options)
|
||||
else
|
||||
options = nil
|
||||
end
|
||||
|
||||
super(options)
|
||||
@default_proc = block
|
||||
end
|
||||
|
||||
# Get a value with key
|
||||
# @param [Object] key
|
||||
# @return [Object] the value
|
||||
def [](key)
|
||||
if value = super # non-falsy value is an existing mapping, return it right away
|
||||
value
|
||||
# re-check is done with get_or_default(key, NULL) instead of a simple !key?(key) in order to avoid a race condition, whereby by the time the current thread gets to the key?(key) call
|
||||
# a key => value mapping might have already been created by a different thread (key?(key) would then return true, this elsif branch wouldn't be taken and an incorrent +nil+ value
|
||||
# would be returned)
|
||||
# note: nil == value check is not technically necessary
|
||||
elsif @default_proc && nil == value && NULL == (value = get_or_default(key, NULL))
|
||||
@default_proc.call(self, key)
|
||||
else
|
||||
value
|
||||
end
|
||||
end
|
||||
|
||||
alias_method :get, :[]
|
||||
# TODO (pitr-ch 30-Oct-2018): doc
|
||||
alias_method :put, :[]=
|
||||
|
||||
# Get a value with key, or default_value when key is absent,
|
||||
# or fail when no default_value is given.
|
||||
# @param [Object] key
|
||||
# @param [Object] default_value
|
||||
# @yield default value for a key
|
||||
# @yieldparam key [Object]
|
||||
# @yieldreturn [Object] default value
|
||||
# @return [Object] the value or default value
|
||||
# @raise [KeyError] when key is missing and no default_value is provided
|
||||
# @!macro map_method_not_atomic
|
||||
# @note The "fetch-then-act" methods of `Map` are not atomic. `Map` is intended
|
||||
# to be use as a concurrency primitive with strong happens-before
|
||||
# guarantees. It is not intended to be used as a high-level abstraction
|
||||
# supporting complex operations. All read and write operations are
|
||||
# thread safe, but no guarantees are made regarding race conditions
|
||||
# between the fetch operation and yielding to the block. Additionally,
|
||||
# this method does not support recursion. This is due to internal
|
||||
# constraints that are very unlikely to change in the near future.
|
||||
def fetch(key, default_value = NULL)
|
||||
if NULL != (value = get_or_default(key, NULL))
|
||||
value
|
||||
elsif block_given?
|
||||
yield key
|
||||
elsif NULL != default_value
|
||||
default_value
|
||||
else
|
||||
raise_fetch_no_key
|
||||
end
|
||||
end
|
||||
|
||||
# Fetch value with key, or store default value when key is absent,
|
||||
# or fail when no default_value is given. This is a two step operation,
|
||||
# therefore not atomic. The store can overwrite other concurrently
|
||||
# stored value.
|
||||
# @param [Object] key
|
||||
# @param [Object] default_value
|
||||
# @yield default value for a key
|
||||
# @yieldparam key [Object]
|
||||
# @yieldreturn [Object] default value
|
||||
# @return [Object] the value or default value
|
||||
# @!macro map.atomic_method_with_block
|
||||
def fetch_or_store(key, default_value = NULL)
|
||||
fetch(key) do
|
||||
put(key, block_given? ? yield(key) : (NULL == default_value ? raise_fetch_no_key : default_value))
|
||||
end
|
||||
end
|
||||
|
||||
# Insert value into map with key if key is absent in one atomic step.
|
||||
# @param [Object] key
|
||||
# @param [Object] value
|
||||
# @return [Object, nil] the value or nil when key was present
|
||||
def put_if_absent(key, value)
|
||||
computed = false
|
||||
result = compute_if_absent(key) do
|
||||
computed = true
|
||||
value
|
||||
end
|
||||
computed ? nil : result
|
||||
end unless method_defined?(:put_if_absent)
|
||||
|
||||
# Is the value stored in the map. Iterates over all values.
|
||||
# @param [Object] value
|
||||
# @return [true, false]
|
||||
def value?(value)
|
||||
each_value do |v|
|
||||
return true if value.equal?(v)
|
||||
end
|
||||
false
|
||||
end
|
||||
|
||||
# All keys
|
||||
# @return [::Array<Object>] keys
|
||||
def keys
|
||||
arr = []
|
||||
each_pair { |k, v| arr << k }
|
||||
arr
|
||||
end unless method_defined?(:keys)
|
||||
|
||||
# All values
|
||||
# @return [::Array<Object>] values
|
||||
def values
|
||||
arr = []
|
||||
each_pair { |k, v| arr << v }
|
||||
arr
|
||||
end unless method_defined?(:values)
|
||||
|
||||
# Iterates over each key.
|
||||
# @yield for each key in the map
|
||||
# @yieldparam key [Object]
|
||||
# @return [self]
|
||||
# @!macro map.atomic_method_with_block
|
||||
def each_key
|
||||
each_pair { |k, v| yield k }
|
||||
end unless method_defined?(:each_key)
|
||||
|
||||
# Iterates over each value.
|
||||
# @yield for each value in the map
|
||||
# @yieldparam value [Object]
|
||||
# @return [self]
|
||||
# @!macro map.atomic_method_with_block
|
||||
def each_value
|
||||
each_pair { |k, v| yield v }
|
||||
end unless method_defined?(:each_value)
|
||||
|
||||
# Iterates over each key value pair.
|
||||
# @yield for each key value pair in the map
|
||||
# @yieldparam key [Object]
|
||||
# @yieldparam value [Object]
|
||||
# @return [self]
|
||||
# @!macro map.atomic_method_with_block
|
||||
def each_pair
|
||||
return enum_for :each_pair unless block_given?
|
||||
super
|
||||
end
|
||||
|
||||
alias_method :each, :each_pair unless method_defined?(:each)
|
||||
|
||||
# Find key of a value.
|
||||
# @param [Object] value
|
||||
# @return [Object, nil] key or nil when not found
|
||||
def key(value)
|
||||
each_pair { |k, v| return k if v == value }
|
||||
nil
|
||||
end unless method_defined?(:key)
|
||||
alias_method :index, :key if RUBY_VERSION < '1.9'
|
||||
|
||||
# Is map empty?
|
||||
# @return [true, false]
|
||||
def empty?
|
||||
each_pair { |k, v| return false }
|
||||
true
|
||||
end unless method_defined?(:empty?)
|
||||
|
||||
# The size of map.
|
||||
# @return [Integer] size
|
||||
def size
|
||||
count = 0
|
||||
each_pair { |k, v| count += 1 }
|
||||
count
|
||||
end unless method_defined?(:size)
|
||||
|
||||
# @!visibility private
|
||||
def marshal_dump
|
||||
raise TypeError, "can't dump hash with default proc" if @default_proc
|
||||
h = {}
|
||||
each_pair { |k, v| h[k] = v }
|
||||
h
|
||||
end
|
||||
|
||||
# @!visibility private
|
||||
def marshal_load(hash)
|
||||
initialize
|
||||
populate_from(hash)
|
||||
end
|
||||
|
||||
undef :freeze
|
||||
|
||||
# @!visibility private
|
||||
def inspect
|
||||
format '%s entries=%d default_proc=%s>', to_s[0..-2], size.to_s, @default_proc.inspect
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def raise_fetch_no_key
|
||||
raise KeyError, 'key not found'
|
||||
end
|
||||
|
||||
def initialize_copy(other)
|
||||
super
|
||||
populate_from(other)
|
||||
end
|
||||
|
||||
def populate_from(hash)
|
||||
hash.each_pair { |k, v| self[k] = v }
|
||||
self
|
||||
end
|
||||
|
||||
def validate_options_hash!(options)
|
||||
if (initial_capacity = options[:initial_capacity]) && (!initial_capacity.kind_of?(Integer) || initial_capacity < 0)
|
||||
raise ArgumentError, ":initial_capacity must be a positive Integer"
|
||||
end
|
||||
if (load_factor = options[:load_factor]) && (!load_factor.kind_of?(Numeric) || load_factor <= 0 || load_factor > 1)
|
||||
raise ArgumentError, ":load_factor must be a number between 0 and 1"
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
@ -7,15 +7,14 @@ Concurrent.load_native_extensions
|
||||
require 'concurrent/synchronization/mri_object'
|
||||
require 'concurrent/synchronization/jruby_object'
|
||||
require 'concurrent/synchronization/rbx_object'
|
||||
require 'concurrent/synchronization/truffle_object'
|
||||
require 'concurrent/synchronization/truffleruby_object'
|
||||
require 'concurrent/synchronization/object'
|
||||
require 'concurrent/synchronization/volatile'
|
||||
|
||||
require 'concurrent/synchronization/abstract_lockable_object'
|
||||
require 'concurrent/synchronization/mri_lockable_object'
|
||||
require 'concurrent/synchronization/mutex_lockable_object'
|
||||
require 'concurrent/synchronization/jruby_lockable_object'
|
||||
require 'concurrent/synchronization/rbx_lockable_object'
|
||||
require 'concurrent/synchronization/truffle_lockable_object'
|
||||
|
||||
require 'concurrent/synchronization/lockable_object'
|
||||
|
||||
@ -23,8 +22,8 @@ require 'concurrent/synchronization/condition'
|
||||
require 'concurrent/synchronization/lock'
|
||||
|
||||
module Concurrent
|
||||
# {include:file:doc/synchronization.md}
|
||||
# {include:file:doc/synchronization-notes.md}
|
||||
# {include:file:docs-source/synchronization.md}
|
||||
# {include:file:docs-source/synchronization-notes.md}
|
||||
module Synchronization
|
||||
end
|
||||
end
|
||||
@ -6,7 +6,7 @@ module Concurrent
|
||||
|
||||
protected
|
||||
|
||||
# @!macro [attach] synchronization_object_method_synchronize
|
||||
# @!macro synchronization_object_method_synchronize
|
||||
#
|
||||
# @yield runs the block synchronized against this object,
|
||||
# equivalent of java's `synchronize(this) {}`
|
||||
@ -15,7 +15,7 @@ module Concurrent
|
||||
raise NotImplementedError
|
||||
end
|
||||
|
||||
# @!macro [attach] synchronization_object_method_ns_wait_until
|
||||
# @!macro synchronization_object_method_ns_wait_until
|
||||
#
|
||||
# Wait until condition is met or timeout passes,
|
||||
# protects against spurious wake-ups.
|
||||
@ -45,7 +45,7 @@ module Concurrent
|
||||
end
|
||||
end
|
||||
|
||||
# @!macro [attach] synchronization_object_method_ns_wait
|
||||
# @!macro synchronization_object_method_ns_wait
|
||||
#
|
||||
# Wait until another thread calls #signal or #broadcast,
|
||||
# spurious wake-ups can happen.
|
||||
@ -63,7 +63,7 @@ module Concurrent
|
||||
raise NotImplementedError
|
||||
end
|
||||
|
||||
# @!macro [attach] synchronization_object_method_ns_signal
|
||||
# @!macro synchronization_object_method_ns_signal
|
||||
#
|
||||
# Signal one waiting thread.
|
||||
# @return [self]
|
||||
@ -78,7 +78,7 @@ module Concurrent
|
||||
raise NotImplementedError
|
||||
end
|
||||
|
||||
# @!macro [attach] synchronization_object_method_ns_broadcast
|
||||
# @!macro synchronization_object_method_ns_broadcast
|
||||
#
|
||||
# Broadcast to all waiting threads.
|
||||
# @return [self]
|
||||
@ -5,18 +5,18 @@ module Concurrent
|
||||
# @!macro internal_implementation_note
|
||||
LockableObjectImplementation = case
|
||||
when Concurrent.on_cruby? && Concurrent.ruby_version(:<=, 1, 9, 3)
|
||||
MriMonitorLockableObject
|
||||
MonitorLockableObject
|
||||
when Concurrent.on_cruby? && Concurrent.ruby_version(:>, 1, 9, 3)
|
||||
MriMutexLockableObject
|
||||
MutexLockableObject
|
||||
when Concurrent.on_jruby?
|
||||
JRubyLockableObject
|
||||
when Concurrent.on_rbx?
|
||||
RbxLockableObject
|
||||
when Concurrent.on_truffle?
|
||||
MriMutexLockableObject
|
||||
when Concurrent.on_truffleruby?
|
||||
MutexLockableObject
|
||||
else
|
||||
warn 'Possibly unsupported Ruby implementation'
|
||||
MriMonitorLockableObject
|
||||
MonitorLockableObject
|
||||
end
|
||||
private_constant :LockableObjectImplementation
|
||||
|
||||
@ -31,7 +31,7 @@ module Concurrent
|
||||
# `Thread#sleep` and `Thread#wakeup` will work as expected but mixing `Synchronization::Object#wait` and
|
||||
# `Thread#wakeup` will not work on all platforms.
|
||||
#
|
||||
# @see {Event} implementation as an example of this class use
|
||||
# @see Event implementation as an example of this class use
|
||||
#
|
||||
# @example simple
|
||||
# class AnClass < Synchronization::Object
|
||||
@ -1,18 +1,19 @@
|
||||
module Concurrent
|
||||
# noinspection RubyInstanceVariableNamingConvention
|
||||
module Synchronization
|
||||
|
||||
# @!visibility private
|
||||
# @!macro internal_implementation_note
|
||||
class MriLockableObject < AbstractLockableObject
|
||||
module ConditionSignalling
|
||||
protected
|
||||
|
||||
def ns_signal
|
||||
@__condition__.signal
|
||||
@__Condition__.signal
|
||||
self
|
||||
end
|
||||
|
||||
def ns_broadcast
|
||||
@__condition__.broadcast
|
||||
@__Condition__.broadcast
|
||||
self
|
||||
end
|
||||
end
|
||||
@ -20,50 +21,54 @@ module Concurrent
|
||||
|
||||
# @!visibility private
|
||||
# @!macro internal_implementation_note
|
||||
class MriMutexLockableObject < MriLockableObject
|
||||
class MutexLockableObject < AbstractLockableObject
|
||||
include ConditionSignalling
|
||||
|
||||
safe_initialization!
|
||||
|
||||
def initialize(*defaults)
|
||||
super(*defaults)
|
||||
@__lock__ = ::Mutex.new
|
||||
@__condition__ = ::ConditionVariable.new
|
||||
@__Lock__ = ::Mutex.new
|
||||
@__Condition__ = ::ConditionVariable.new
|
||||
end
|
||||
|
||||
protected
|
||||
|
||||
def synchronize
|
||||
if @__lock__.owned?
|
||||
if @__Lock__.owned?
|
||||
yield
|
||||
else
|
||||
@__lock__.synchronize { yield }
|
||||
@__Lock__.synchronize { yield }
|
||||
end
|
||||
end
|
||||
|
||||
def ns_wait(timeout = nil)
|
||||
@__condition__.wait @__lock__, timeout
|
||||
@__Condition__.wait @__Lock__, timeout
|
||||
self
|
||||
end
|
||||
end
|
||||
|
||||
# @!visibility private
|
||||
# @!macro internal_implementation_note
|
||||
class MriMonitorLockableObject < MriLockableObject
|
||||
class MonitorLockableObject < AbstractLockableObject
|
||||
include ConditionSignalling
|
||||
|
||||
safe_initialization!
|
||||
|
||||
def initialize(*defaults)
|
||||
super(*defaults)
|
||||
@__lock__ = ::Monitor.new
|
||||
@__condition__ = @__lock__.new_cond
|
||||
@__Lock__ = ::Monitor.new
|
||||
@__Condition__ = @__Lock__.new_cond
|
||||
end
|
||||
|
||||
protected
|
||||
|
||||
def synchronize # TODO may be a problem with lock.synchronize { lock.wait }
|
||||
@__lock__.synchronize { yield }
|
||||
@__Lock__.synchronize { yield }
|
||||
end
|
||||
|
||||
def ns_wait(timeout = nil)
|
||||
@__condition__.wait timeout
|
||||
@__Condition__.wait timeout
|
||||
self
|
||||
end
|
||||
end
|
||||
@ -10,9 +10,10 @@ module Concurrent
|
||||
JRubyObject
|
||||
when Concurrent.on_rbx?
|
||||
RbxObject
|
||||
when Concurrent.on_truffle?
|
||||
TruffleObject
|
||||
when Concurrent.on_truffleruby?
|
||||
TruffleRubyObject
|
||||
else
|
||||
warn 'Possibly unsupported Ruby implementation'
|
||||
MriObject
|
||||
end
|
||||
private_constant :ObjectImplementation
|
||||
@ -134,8 +135,11 @@ module Concurrent
|
||||
private
|
||||
|
||||
def self.define_initialize_volatile_with_cas
|
||||
assignments = @volatile_cas_fields.map { |name| "@Atomic#{name.to_s.gsub(/(?:^|_)(.)/) { $1.upcase }} = AtomicReference.new(nil)" }.join("\n")
|
||||
class_eval <<-RUBY
|
||||
assignments = @volatile_cas_fields.map do |name|
|
||||
"@Atomic#{name.to_s.gsub(/(?:^|_)(.)/) { $1.upcase }} = Concurrent::AtomicReference.new(nil)"
|
||||
end.join("\n")
|
||||
|
||||
class_eval <<-RUBY, __FILE__, __LINE__ + 1
|
||||
def initialize_volatile_with_cas
|
||||
super
|
||||
#{assignments}
|
||||
@ -0,0 +1,46 @@
|
||||
module Concurrent
|
||||
module Synchronization
|
||||
|
||||
module TruffleRubyAttrVolatile
|
||||
def self.included(base)
|
||||
base.extend(ClassMethods)
|
||||
end
|
||||
|
||||
module ClassMethods
|
||||
def attr_volatile(*names)
|
||||
names.each do |name|
|
||||
ivar = :"@volatile_#{name}"
|
||||
|
||||
class_eval <<-RUBY, __FILE__, __LINE__ + 1
|
||||
def #{name}
|
||||
full_memory_barrier
|
||||
#{ivar}
|
||||
end
|
||||
|
||||
def #{name}=(value)
|
||||
#{ivar} = value
|
||||
full_memory_barrier
|
||||
end
|
||||
RUBY
|
||||
end
|
||||
|
||||
names.map { |n| [n, :"#{n}="] }.flatten
|
||||
end
|
||||
end
|
||||
|
||||
def full_memory_barrier
|
||||
TruffleRuby.full_memory_barrier
|
||||
end
|
||||
end
|
||||
|
||||
# @!visibility private
|
||||
# @!macro internal_implementation_note
|
||||
class TruffleRubyObject < AbstractObject
|
||||
include TruffleRubyAttrVolatile
|
||||
|
||||
def initialize
|
||||
# nothing to do
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
@ -25,8 +25,10 @@ module Concurrent
|
||||
MriAttrVolatile
|
||||
when Concurrent.on_jruby?
|
||||
JRubyAttrVolatile
|
||||
when Concurrent.on_rbx? || Concurrent.on_truffle?
|
||||
when Concurrent.on_rbx?
|
||||
RbxAttrVolatile
|
||||
when Concurrent.on_truffleruby?
|
||||
TruffleRubyAttrVolatile
|
||||
else
|
||||
MriAttrVolatile
|
||||
end
|
||||
@ -8,7 +8,7 @@ module Concurrent
|
||||
end
|
||||
|
||||
def on_jruby_9000?
|
||||
on_jruby? && ruby_version(:>=, 9, 0, 0, JRUBY_VERSION)
|
||||
on_jruby? && ruby_version(JRUBY_VERSION, :>=, 9, 0, 0)
|
||||
end
|
||||
|
||||
def on_cruby?
|
||||
@ -19,8 +19,8 @@ module Concurrent
|
||||
ruby_engine == 'rbx'
|
||||
end
|
||||
|
||||
def on_truffle?
|
||||
ruby_engine == 'jruby+truffle'
|
||||
def on_truffleruby?
|
||||
ruby_engine == 'truffleruby'
|
||||
end
|
||||
|
||||
def on_windows?
|
||||
@ -39,7 +39,7 @@ module Concurrent
|
||||
defined?(RUBY_ENGINE) ? RUBY_ENGINE : 'ruby'
|
||||
end
|
||||
|
||||
def ruby_version(comparison, major, minor, patch, version = RUBY_VERSION)
|
||||
def ruby_version(version = RUBY_VERSION, comparison, major, minor, patch)
|
||||
result = (version.split('.').map(&:to_i) <=> [major, minor, patch])
|
||||
comparisons = { :== => [0],
|
||||
:>= => [1, 0],
|
||||
@ -0,0 +1,79 @@
|
||||
require 'concurrent/utility/engine'
|
||||
|
||||
module Concurrent
|
||||
|
||||
module Utility
|
||||
|
||||
# @!visibility private
|
||||
module NativeExtensionLoader
|
||||
|
||||
def allow_c_extensions?
|
||||
Concurrent.on_cruby?
|
||||
end
|
||||
|
||||
def c_extensions_loaded?
|
||||
defined?(@c_extensions_loaded) && @c_extensions_loaded
|
||||
end
|
||||
|
||||
def java_extensions_loaded?
|
||||
defined?(@java_extensions_loaded) && @java_extensions_loaded
|
||||
end
|
||||
|
||||
def load_native_extensions
|
||||
unless defined? Synchronization::AbstractObject
|
||||
raise 'native_extension_loader loaded before Synchronization::AbstractObject'
|
||||
end
|
||||
|
||||
if Concurrent.on_cruby? && !c_extensions_loaded?
|
||||
['concurrent/concurrent_ruby_ext',
|
||||
"concurrent/#{RUBY_VERSION[0..2]}/concurrent_ruby_ext"
|
||||
].each { |p| try_load_c_extension p }
|
||||
end
|
||||
|
||||
if Concurrent.on_jruby? && !java_extensions_loaded?
|
||||
begin
|
||||
require 'concurrent/concurrent_ruby.jar'
|
||||
set_java_extensions_loaded
|
||||
rescue LoadError => e
|
||||
raise e, "Java extensions are required for JRuby.\n" + e.message, e.backtrace
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def load_error_path(error)
|
||||
if error.respond_to? :path
|
||||
error.path
|
||||
else
|
||||
error.message.split(' -- ').last
|
||||
end
|
||||
end
|
||||
|
||||
def set_c_extensions_loaded
|
||||
@c_extensions_loaded = true
|
||||
end
|
||||
|
||||
def set_java_extensions_loaded
|
||||
@java_extensions_loaded = true
|
||||
end
|
||||
|
||||
def try_load_c_extension(path)
|
||||
require path
|
||||
set_c_extensions_loaded
|
||||
rescue LoadError => e
|
||||
if load_error_path(e) == path
|
||||
# move on with pure-Ruby implementations
|
||||
# TODO (pitr-ch 12-Jul-2018): warning on verbose?
|
||||
else
|
||||
raise e
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
|
||||
# @!visibility private
|
||||
extend Utility::NativeExtensionLoader
|
||||
end
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user