concurrent-ruby 1.1.5 → 1.2.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +84 -1
- data/Gemfile +5 -10
- data/{LICENSE.md → LICENSE.txt} +18 -20
- data/README.md +55 -31
- data/Rakefile +84 -92
- data/ext/concurrent-ruby/com/concurrent_ruby/ext/JavaAtomicFixnumLibrary.java +0 -0
- data/ext/concurrent-ruby/com/concurrent_ruby/ext/JavaSemaphoreLibrary.java +52 -22
- data/ext/concurrent-ruby/com/concurrent_ruby/ext/SynchronizationLibrary.java +10 -25
- data/lib/{concurrent → concurrent-ruby/concurrent}/agent.rb +2 -1
- data/lib/{concurrent → concurrent-ruby/concurrent}/array.rb +6 -16
- data/lib/{concurrent → concurrent-ruby/concurrent}/async.rb +10 -20
- data/lib/{concurrent → concurrent-ruby/concurrent}/atom.rb +2 -2
- data/lib/{concurrent → concurrent-ruby/concurrent}/atomic/atomic_boolean.rb +7 -6
- data/lib/{concurrent → concurrent-ruby/concurrent}/atomic/atomic_fixnum.rb +5 -4
- data/lib/{concurrent → concurrent-ruby/concurrent}/atomic/atomic_markable_reference.rb +3 -0
- data/lib/concurrent-ruby/concurrent/atomic/atomic_reference.rb +135 -0
- data/lib/{concurrent → concurrent-ruby/concurrent}/atomic/cyclic_barrier.rb +1 -1
- data/lib/{concurrent → concurrent-ruby/concurrent}/atomic/event.rb +3 -3
- data/lib/concurrent-ruby/concurrent/atomic/fiber_local_var.rb +109 -0
- data/lib/{concurrent → concurrent-ruby/concurrent}/atomic/java_count_down_latch.rb +1 -0
- data/lib/concurrent-ruby/concurrent/atomic/locals.rb +189 -0
- data/lib/concurrent-ruby/concurrent/atomic/lock_local_var.rb +28 -0
- data/lib/{concurrent → concurrent-ruby/concurrent}/atomic/mutex_atomic_boolean.rb +11 -5
- data/lib/{concurrent → concurrent-ruby/concurrent}/atomic/mutex_atomic_fixnum.rb +11 -5
- data/lib/{concurrent → concurrent-ruby/concurrent}/atomic/mutex_count_down_latch.rb +1 -1
- data/lib/{concurrent → concurrent-ruby/concurrent}/atomic/mutex_semaphore.rb +19 -3
- data/lib/{concurrent → concurrent-ruby/concurrent}/atomic/read_write_lock.rb +2 -1
- data/lib/{concurrent → concurrent-ruby/concurrent}/atomic/reentrant_read_write_lock.rb +9 -9
- data/lib/{concurrent → concurrent-ruby/concurrent}/atomic/semaphore.rb +32 -14
- data/lib/concurrent-ruby/concurrent/atomic/thread_local_var.rb +111 -0
- data/lib/concurrent-ruby/concurrent/atomic_reference/atomic_direct_update.rb +37 -0
- data/lib/{concurrent → concurrent-ruby/concurrent}/atomic_reference/mutex_atomic.rb +15 -4
- data/lib/{concurrent → concurrent-ruby/concurrent}/collection/copy_on_notify_observer_set.rb +1 -1
- data/lib/{concurrent → concurrent-ruby/concurrent}/collection/copy_on_write_observer_set.rb +1 -1
- data/lib/{concurrent → concurrent-ruby/concurrent}/collection/lock_free_stack.rb +2 -0
- data/lib/{concurrent → concurrent-ruby/concurrent}/collection/map/mri_map_backend.rb +3 -3
- data/lib/{concurrent → concurrent-ruby/concurrent}/collection/map/non_concurrent_map_backend.rb +16 -8
- data/lib/concurrent-ruby/concurrent/collection/map/truffleruby_map_backend.rb +14 -0
- data/lib/{concurrent → concurrent-ruby/concurrent}/collection/ruby_non_concurrent_priority_queue.rb +11 -1
- data/lib/{concurrent → concurrent-ruby/concurrent}/concern/dereferenceable.rb +2 -2
- data/lib/concurrent-ruby/concurrent/concern/logging.rb +116 -0
- data/lib/concurrent-ruby/concurrent/concurrent_ruby.jar +0 -0
- data/lib/concurrent-ruby/concurrent/configuration.rb +105 -0
- data/lib/{concurrent → concurrent-ruby/concurrent}/delay.rb +2 -2
- data/lib/{concurrent → concurrent-ruby/concurrent}/errors.rb +5 -0
- data/lib/{concurrent → concurrent-ruby/concurrent}/exchanger.rb +1 -0
- data/lib/{concurrent → concurrent-ruby/concurrent}/executor/abstract_executor_service.rb +34 -37
- data/lib/{concurrent → concurrent-ruby/concurrent}/executor/cached_thread_pool.rb +4 -4
- data/lib/{concurrent → concurrent-ruby/concurrent}/executor/executor_service.rb +2 -2
- data/lib/{concurrent → concurrent-ruby/concurrent}/executor/fixed_thread_pool.rb +29 -15
- data/lib/{concurrent → concurrent-ruby/concurrent}/executor/java_executor_service.rb +21 -9
- data/lib/{concurrent → concurrent-ruby/concurrent}/executor/java_single_thread_executor.rb +4 -3
- data/lib/{concurrent → concurrent-ruby/concurrent}/executor/java_thread_pool_executor.rb +19 -2
- data/lib/{concurrent → concurrent-ruby/concurrent}/executor/ruby_executor_service.rb +10 -6
- data/lib/{concurrent → concurrent-ruby/concurrent}/executor/ruby_single_thread_executor.rb +0 -1
- data/lib/{concurrent → concurrent-ruby/concurrent}/executor/ruby_thread_pool_executor.rb +46 -42
- data/lib/{concurrent → concurrent-ruby/concurrent}/executor/safe_task_executor.rb +6 -6
- data/lib/{concurrent → concurrent-ruby/concurrent}/executor/serialized_execution.rb +1 -1
- data/lib/{concurrent → concurrent-ruby/concurrent}/executor/simple_executor_service.rb +5 -2
- data/lib/{concurrent → concurrent-ruby/concurrent}/executor/single_thread_executor.rb +1 -0
- data/lib/{concurrent → concurrent-ruby/concurrent}/executor/thread_pool_executor.rb +2 -1
- data/lib/{concurrent → concurrent-ruby/concurrent}/executor/timer_set.rb +0 -1
- data/lib/{concurrent → concurrent-ruby/concurrent}/hash.rb +1 -10
- data/lib/{concurrent → concurrent-ruby/concurrent}/immutable_struct.rb +10 -2
- data/lib/{concurrent → concurrent-ruby/concurrent}/ivar.rb +2 -1
- data/lib/{concurrent → concurrent-ruby/concurrent}/map.rb +44 -31
- data/lib/{concurrent → concurrent-ruby/concurrent}/maybe.rb +1 -1
- data/lib/{concurrent → concurrent-ruby/concurrent}/mutable_struct.rb +13 -3
- data/lib/{concurrent → concurrent-ruby/concurrent}/mvar.rb +1 -1
- data/lib/{concurrent → concurrent-ruby/concurrent}/promise.rb +2 -1
- data/lib/{concurrent → concurrent-ruby/concurrent}/promises.rb +7 -6
- data/lib/{concurrent → concurrent-ruby/concurrent}/re_include.rb +2 -0
- data/lib/{concurrent → concurrent-ruby/concurrent}/scheduled_task.rb +30 -17
- data/lib/{concurrent → concurrent-ruby/concurrent}/set.rb +17 -19
- data/lib/{concurrent → concurrent-ruby/concurrent}/settable_struct.rb +13 -3
- data/lib/{concurrent → concurrent-ruby/concurrent}/synchronization/abstract_lockable_object.rb +5 -1
- data/lib/{concurrent → concurrent-ruby/concurrent}/synchronization/abstract_object.rb +1 -3
- data/lib/{concurrent → concurrent-ruby/concurrent}/synchronization/abstract_struct.rb +11 -0
- data/lib/{concurrent → concurrent-ruby/concurrent}/synchronization/condition.rb +2 -0
- data/lib/concurrent-ruby/concurrent/synchronization/full_memory_barrier.rb +29 -0
- data/lib/{concurrent → concurrent-ruby/concurrent}/synchronization/jruby_lockable_object.rb +3 -1
- data/lib/{concurrent → concurrent-ruby/concurrent}/synchronization/lock.rb +2 -0
- data/lib/{concurrent → concurrent-ruby/concurrent}/synchronization/lockable_object.rb +8 -7
- data/lib/{concurrent → concurrent-ruby/concurrent}/synchronization/mutex_lockable_object.rb +18 -5
- data/lib/{concurrent → concurrent-ruby/concurrent}/synchronization/object.rb +12 -44
- data/lib/concurrent-ruby/concurrent/synchronization/safe_initialization.rb +36 -0
- data/lib/concurrent-ruby/concurrent/synchronization/volatile.rb +101 -0
- data/lib/concurrent-ruby/concurrent/synchronization.rb +13 -0
- data/lib/concurrent-ruby/concurrent/thread_safe/synchronized_delegator.rb +47 -0
- data/lib/{concurrent → concurrent-ruby/concurrent}/thread_safe/util/cheap_lockable.rb +2 -39
- data/lib/concurrent-ruby/concurrent/thread_safe/util/data_structures.rb +52 -0
- data/lib/{concurrent → concurrent-ruby/concurrent}/thread_safe/util/striped64.rb +1 -1
- data/lib/{concurrent → concurrent-ruby/concurrent}/timer_task.rb +11 -34
- data/lib/{concurrent → concurrent-ruby/concurrent}/tuple.rb +1 -5
- data/lib/{concurrent → concurrent-ruby/concurrent}/tvar.rb +21 -57
- data/lib/{concurrent → concurrent-ruby/concurrent}/utility/engine.rb +5 -16
- data/lib/concurrent-ruby/concurrent/utility/monotonic_time.rb +19 -0
- data/lib/{concurrent → concurrent-ruby/concurrent}/utility/native_extension_loader.rb +8 -10
- data/lib/{concurrent → concurrent-ruby/concurrent}/utility/native_integer.rb +1 -0
- data/lib/concurrent-ruby/concurrent/utility/processor_counter.rb +110 -0
- data/lib/concurrent-ruby/concurrent/version.rb +3 -0
- data/lib/concurrent-ruby/concurrent-ruby.rb +5 -0
- metadata +127 -129
- data/lib/concurrent/atomic/abstract_thread_local_var.rb +0 -66
- data/lib/concurrent/atomic/atomic_reference.rb +0 -204
- data/lib/concurrent/atomic/java_thread_local_var.rb +0 -37
- data/lib/concurrent/atomic/ruby_thread_local_var.rb +0 -161
- data/lib/concurrent/atomic/thread_local_var.rb +0 -104
- data/lib/concurrent/concern/logging.rb +0 -32
- data/lib/concurrent/concurrent_ruby.jar +0 -0
- data/lib/concurrent/configuration.rb +0 -184
- data/lib/concurrent/synchronization/jruby_object.rb +0 -45
- data/lib/concurrent/synchronization/mri_object.rb +0 -44
- data/lib/concurrent/synchronization/rbx_lockable_object.rb +0 -65
- data/lib/concurrent/synchronization/rbx_object.rb +0 -49
- data/lib/concurrent/synchronization/truffleruby_object.rb +0 -47
- data/lib/concurrent/synchronization/volatile.rb +0 -36
- data/lib/concurrent/synchronization.rb +0 -30
- data/lib/concurrent/thread_safe/synchronized_delegator.rb +0 -50
- data/lib/concurrent/thread_safe/util/data_structures.rb +0 -63
- data/lib/concurrent/utility/at_exit.rb +0 -97
- data/lib/concurrent/utility/monotonic_time.rb +0 -58
- data/lib/concurrent/utility/processor_counter.rb +0 -158
- data/lib/concurrent/version.rb +0 -3
- data/lib/concurrent-ruby.rb +0 -1
- data/lib/{concurrent → concurrent-ruby/concurrent}/atomic/count_down_latch.rb +1 -1
- data/lib/{concurrent → concurrent-ruby/concurrent}/atomic_reference/numeric_cas_wrapper.rb +0 -0
- data/lib/{concurrent → concurrent-ruby/concurrent}/atomics.rb +0 -0
- data/lib/{concurrent → concurrent-ruby/concurrent}/collection/java_non_concurrent_priority_queue.rb +0 -0
- data/lib/{concurrent → concurrent-ruby/concurrent}/collection/map/atomic_reference_map_backend.rb +0 -0
- data/lib/{concurrent → concurrent-ruby/concurrent}/collection/map/synchronized_map_backend.rb +0 -0
- data/lib/{concurrent → concurrent-ruby/concurrent}/collection/non_concurrent_priority_queue.rb +1 -1
- /data/lib/{concurrent → concurrent-ruby/concurrent}/concern/deprecation.rb +0 -0
- /data/lib/{concurrent → concurrent-ruby/concurrent}/concern/obligation.rb +0 -0
- /data/lib/{concurrent → concurrent-ruby/concurrent}/concern/observable.rb +0 -0
- /data/lib/{concurrent → concurrent-ruby/concurrent}/constants.rb +0 -0
- /data/lib/{concurrent → concurrent-ruby/concurrent}/dataflow.rb +0 -0
- /data/lib/{concurrent → concurrent-ruby/concurrent}/executor/immediate_executor.rb +0 -0
- /data/lib/{concurrent → concurrent-ruby/concurrent}/executor/indirect_immediate_executor.rb +0 -0
- /data/lib/{concurrent → concurrent-ruby/concurrent}/executor/serial_executor_service.rb +0 -0
- /data/lib/{concurrent → concurrent-ruby/concurrent}/executor/serialized_execution_delegator.rb +0 -0
- /data/lib/{concurrent → concurrent-ruby/concurrent}/executors.rb +0 -0
- /data/lib/{concurrent → concurrent-ruby/concurrent}/future.rb +0 -0
- /data/lib/{concurrent → concurrent-ruby/concurrent}/options.rb +0 -0
- /data/lib/{concurrent → concurrent-ruby/concurrent}/thread_safe/util/adder.rb +0 -0
- /data/lib/{concurrent → concurrent-ruby/concurrent}/thread_safe/util/power_of_two_tuple.rb +0 -0
- /data/lib/{concurrent → concurrent-ruby/concurrent}/thread_safe/util/volatile.rb +0 -0
- /data/lib/{concurrent → concurrent-ruby/concurrent}/thread_safe/util/xor_shift_random.rb +0 -0
- /data/lib/{concurrent → concurrent-ruby/concurrent}/thread_safe/util.rb +0 -0
- /data/lib/{concurrent.rb → concurrent-ruby/concurrent.rb} +0 -0
@@ -0,0 +1,105 @@
|
|
1
|
+
require 'thread'
|
2
|
+
require 'concurrent/delay'
|
3
|
+
require 'concurrent/errors'
|
4
|
+
require 'concurrent/concern/deprecation'
|
5
|
+
require 'concurrent/executor/immediate_executor'
|
6
|
+
require 'concurrent/executor/fixed_thread_pool'
|
7
|
+
require 'concurrent/executor/cached_thread_pool'
|
8
|
+
require 'concurrent/utility/processor_counter'
|
9
|
+
|
10
|
+
module Concurrent
|
11
|
+
extend Concern::Deprecation
|
12
|
+
|
13
|
+
autoload :Options, 'concurrent/options'
|
14
|
+
autoload :TimerSet, 'concurrent/executor/timer_set'
|
15
|
+
autoload :ThreadPoolExecutor, 'concurrent/executor/thread_pool_executor'
|
16
|
+
|
17
|
+
# @!visibility private
|
18
|
+
GLOBAL_FAST_EXECUTOR = Delay.new { Concurrent.new_fast_executor }
|
19
|
+
private_constant :GLOBAL_FAST_EXECUTOR
|
20
|
+
|
21
|
+
# @!visibility private
|
22
|
+
GLOBAL_IO_EXECUTOR = Delay.new { Concurrent.new_io_executor }
|
23
|
+
private_constant :GLOBAL_IO_EXECUTOR
|
24
|
+
|
25
|
+
# @!visibility private
|
26
|
+
GLOBAL_TIMER_SET = Delay.new { TimerSet.new }
|
27
|
+
private_constant :GLOBAL_TIMER_SET
|
28
|
+
|
29
|
+
# @!visibility private
|
30
|
+
GLOBAL_IMMEDIATE_EXECUTOR = ImmediateExecutor.new
|
31
|
+
private_constant :GLOBAL_IMMEDIATE_EXECUTOR
|
32
|
+
|
33
|
+
# Disables AtExit handlers including pool auto-termination handlers.
|
34
|
+
# When disabled it will be the application programmer's responsibility
|
35
|
+
# to ensure that the handlers are shutdown properly prior to application
|
36
|
+
# exit by calling `AtExit.run` method.
|
37
|
+
#
|
38
|
+
# @note this option should be needed only because of `at_exit` ordering
|
39
|
+
# issues which may arise when running some of the testing frameworks.
|
40
|
+
# E.g. Minitest's test-suite runs itself in `at_exit` callback which
|
41
|
+
# executes after the pools are already terminated. Then auto termination
|
42
|
+
# needs to be disabled and called manually after test-suite ends.
|
43
|
+
# @note This method should *never* be called
|
44
|
+
# from within a gem. It should *only* be used from within the main
|
45
|
+
# application and even then it should be used only when necessary.
|
46
|
+
# @deprecated Has no effect since it is no longer needed, see https://github.com/ruby-concurrency/concurrent-ruby/pull/841.
|
47
|
+
#
|
48
|
+
def self.disable_at_exit_handlers!
|
49
|
+
deprecated "Method #disable_at_exit_handlers! has no effect since it is no longer needed, see https://github.com/ruby-concurrency/concurrent-ruby/pull/841."
|
50
|
+
end
|
51
|
+
|
52
|
+
# Global thread pool optimized for short, fast *operations*.
|
53
|
+
#
|
54
|
+
# @return [ThreadPoolExecutor] the thread pool
|
55
|
+
def self.global_fast_executor
|
56
|
+
GLOBAL_FAST_EXECUTOR.value!
|
57
|
+
end
|
58
|
+
|
59
|
+
# Global thread pool optimized for long, blocking (IO) *tasks*.
|
60
|
+
#
|
61
|
+
# @return [ThreadPoolExecutor] the thread pool
|
62
|
+
def self.global_io_executor
|
63
|
+
GLOBAL_IO_EXECUTOR.value!
|
64
|
+
end
|
65
|
+
|
66
|
+
def self.global_immediate_executor
|
67
|
+
GLOBAL_IMMEDIATE_EXECUTOR
|
68
|
+
end
|
69
|
+
|
70
|
+
# Global thread pool user for global *timers*.
|
71
|
+
#
|
72
|
+
# @return [Concurrent::TimerSet] the thread pool
|
73
|
+
def self.global_timer_set
|
74
|
+
GLOBAL_TIMER_SET.value!
|
75
|
+
end
|
76
|
+
|
77
|
+
# General access point to global executors.
|
78
|
+
# @param [Symbol, Executor] executor_identifier symbols:
|
79
|
+
# - :fast - {Concurrent.global_fast_executor}
|
80
|
+
# - :io - {Concurrent.global_io_executor}
|
81
|
+
# - :immediate - {Concurrent.global_immediate_executor}
|
82
|
+
# @return [Executor]
|
83
|
+
def self.executor(executor_identifier)
|
84
|
+
Options.executor(executor_identifier)
|
85
|
+
end
|
86
|
+
|
87
|
+
def self.new_fast_executor(opts = {})
|
88
|
+
FixedThreadPool.new(
|
89
|
+
[2, Concurrent.processor_count].max,
|
90
|
+
auto_terminate: opts.fetch(:auto_terminate, true),
|
91
|
+
idletime: 60, # 1 minute
|
92
|
+
max_queue: 0, # unlimited
|
93
|
+
fallback_policy: :abort, # shouldn't matter -- 0 max queue
|
94
|
+
name: "fast"
|
95
|
+
)
|
96
|
+
end
|
97
|
+
|
98
|
+
def self.new_io_executor(opts = {})
|
99
|
+
CachedThreadPool.new(
|
100
|
+
auto_terminate: opts.fetch(:auto_terminate, true),
|
101
|
+
fallback_policy: :abort, # shouldn't matter -- 0 max queue
|
102
|
+
name: "io"
|
103
|
+
)
|
104
|
+
end
|
105
|
+
end
|
@@ -1,7 +1,7 @@
|
|
1
1
|
require 'thread'
|
2
2
|
require 'concurrent/concern/obligation'
|
3
3
|
require 'concurrent/executor/immediate_executor'
|
4
|
-
require 'concurrent/synchronization'
|
4
|
+
require 'concurrent/synchronization/lockable_object'
|
5
5
|
|
6
6
|
module Concurrent
|
7
7
|
|
@@ -67,7 +67,7 @@ module Concurrent
|
|
67
67
|
|
68
68
|
# Return the value this object represents after applying the options
|
69
69
|
# specified by the `#set_deref_options` method. If the delayed operation
|
70
|
-
# raised an exception this method will return nil. The
|
70
|
+
# raised an exception this method will return nil. The exception object
|
71
71
|
# can be accessed via the `#reason` method.
|
72
72
|
#
|
73
73
|
# @param [Numeric] timeout the maximum number of seconds to wait
|
@@ -66,4 +66,9 @@ module Concurrent
|
|
66
66
|
end
|
67
67
|
end
|
68
68
|
|
69
|
+
# @!macro internal_implementation_note
|
70
|
+
class ConcurrentUpdateError < ThreadError
|
71
|
+
# frozen pre-allocated backtrace to speed ConcurrentUpdateError
|
72
|
+
CONC_UP_ERR_BACKTRACE = ['backtrace elided; set verbose to enable'].freeze
|
73
|
+
end
|
69
74
|
end
|
@@ -1,7 +1,7 @@
|
|
1
1
|
require 'concurrent/errors'
|
2
|
+
require 'concurrent/concern/deprecation'
|
2
3
|
require 'concurrent/executor/executor_service'
|
3
|
-
require 'concurrent/synchronization'
|
4
|
-
require 'concurrent/utility/at_exit'
|
4
|
+
require 'concurrent/synchronization/lockable_object'
|
5
5
|
|
6
6
|
module Concurrent
|
7
7
|
|
@@ -9,6 +9,7 @@ module Concurrent
|
|
9
9
|
# @!visibility private
|
10
10
|
class AbstractExecutorService < Synchronization::LockableObject
|
11
11
|
include ExecutorService
|
12
|
+
include Concern::Deprecation
|
12
13
|
|
13
14
|
# The set of possible fallback policies that may be set at thread pool creation.
|
14
15
|
FALLBACK_POLICIES = [:abort, :discard, :caller_runs].freeze
|
@@ -16,10 +17,20 @@ module Concurrent
|
|
16
17
|
# @!macro executor_service_attr_reader_fallback_policy
|
17
18
|
attr_reader :fallback_policy
|
18
19
|
|
20
|
+
attr_reader :name
|
21
|
+
|
19
22
|
# Create a new thread pool.
|
20
|
-
def initialize(
|
23
|
+
def initialize(opts = {}, &block)
|
21
24
|
super(&nil)
|
22
|
-
synchronize
|
25
|
+
synchronize do
|
26
|
+
@auto_terminate = opts.fetch(:auto_terminate, true)
|
27
|
+
@name = opts.fetch(:name) if opts.key?(:name)
|
28
|
+
ns_initialize(opts, &block)
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
def to_s
|
33
|
+
name ? "#{super[0..-2]} name: #{name}>" : super
|
23
34
|
end
|
24
35
|
|
25
36
|
# @!macro executor_service_method_shutdown
|
@@ -54,38 +65,41 @@ module Concurrent
|
|
54
65
|
|
55
66
|
# @!macro executor_service_method_auto_terminate_question
|
56
67
|
def auto_terminate?
|
57
|
-
synchronize {
|
68
|
+
synchronize { @auto_terminate }
|
58
69
|
end
|
59
70
|
|
60
71
|
# @!macro executor_service_method_auto_terminate_setter
|
61
72
|
def auto_terminate=(value)
|
62
|
-
|
73
|
+
deprecated "Method #auto_terminate= has no effect. Set :auto_terminate option when executor is initialized."
|
63
74
|
end
|
64
75
|
|
65
76
|
private
|
66
77
|
|
67
|
-
#
|
68
|
-
# reaches `max_queue`.
|
78
|
+
# Returns an action which executes the `fallback_policy` once the queue
|
79
|
+
# size reaches `max_queue`. The reason for the indirection of an action
|
80
|
+
# is so that the work can be deferred outside of synchronization.
|
69
81
|
#
|
70
82
|
# @param [Array] args the arguments to the task which is being handled.
|
71
83
|
#
|
72
84
|
# @!visibility private
|
73
|
-
def
|
85
|
+
def fallback_action(*args)
|
74
86
|
case fallback_policy
|
75
87
|
when :abort
|
76
|
-
raise RejectedExecutionError
|
88
|
+
lambda { raise RejectedExecutionError }
|
77
89
|
when :discard
|
78
|
-
false
|
90
|
+
lambda { false }
|
79
91
|
when :caller_runs
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
92
|
+
lambda {
|
93
|
+
begin
|
94
|
+
yield(*args)
|
95
|
+
rescue => ex
|
96
|
+
# let it fail
|
97
|
+
log DEBUG, ex
|
98
|
+
end
|
99
|
+
true
|
100
|
+
}
|
87
101
|
else
|
88
|
-
fail "Unknown fallback policy #{fallback_policy}"
|
102
|
+
lambda { fail "Unknown fallback policy #{fallback_policy}" }
|
89
103
|
end
|
90
104
|
end
|
91
105
|
|
@@ -110,25 +124,8 @@ module Concurrent
|
|
110
124
|
end
|
111
125
|
|
112
126
|
def ns_auto_terminate?
|
113
|
-
|
127
|
+
@auto_terminate
|
114
128
|
end
|
115
129
|
|
116
|
-
def ns_auto_terminate=(value)
|
117
|
-
case value
|
118
|
-
when true
|
119
|
-
AtExit.add(self) { terminate_at_exit }
|
120
|
-
@auto_terminate = true
|
121
|
-
when false
|
122
|
-
AtExit.delete(self)
|
123
|
-
@auto_terminate = false
|
124
|
-
else
|
125
|
-
raise ArgumentError
|
126
|
-
end
|
127
|
-
end
|
128
|
-
|
129
|
-
def terminate_at_exit
|
130
|
-
kill # TODO be gentle first
|
131
|
-
wait_for_termination(10)
|
132
|
-
end
|
133
130
|
end
|
134
131
|
end
|
@@ -37,7 +37,7 @@ module Concurrent
|
|
37
37
|
#
|
38
38
|
# @see http://docs.oracle.com/javase/8/docs/api/java/util/concurrent/Executors.html#newCachedThreadPool--
|
39
39
|
def initialize(opts = {})
|
40
|
-
defaults = { idletime:
|
40
|
+
defaults = { idletime: DEFAULT_THREAD_IDLETIMEOUT }
|
41
41
|
overrides = { min_threads: 0,
|
42
42
|
max_threads: DEFAULT_MAX_POOL_SIZE,
|
43
43
|
max_queue: DEFAULT_MAX_QUEUE_SIZE }
|
@@ -51,11 +51,11 @@ module Concurrent
|
|
51
51
|
def ns_initialize(opts)
|
52
52
|
super(opts)
|
53
53
|
if Concurrent.on_jruby?
|
54
|
-
@max_queue
|
55
|
-
@executor
|
54
|
+
@max_queue = 0
|
55
|
+
@executor = java.util.concurrent.Executors.newCachedThreadPool(
|
56
|
+
DaemonThreadFactory.new(ns_auto_terminate?))
|
56
57
|
@executor.setRejectedExecutionHandler(FALLBACK_POLICY_CLASSES[@fallback_policy].new)
|
57
58
|
@executor.setKeepAliveTime(opts.fetch(:idletime, DEFAULT_THREAD_IDLETIMEOUT), java.util.concurrent.TimeUnit::SECONDS)
|
58
|
-
self.auto_terminate = opts.fetch(:auto_terminate, true)
|
59
59
|
end
|
60
60
|
end
|
61
61
|
end
|
@@ -111,10 +111,10 @@ module Concurrent
|
|
111
111
|
|
112
112
|
# @!macro executor_service_method_auto_terminate_setter
|
113
113
|
#
|
114
|
-
# Set the auto-terminate behavior for this executor.
|
115
114
|
#
|
115
|
+
# Set the auto-terminate behavior for this executor.
|
116
|
+
# @deprecated Has no effect
|
116
117
|
# @param [Boolean] value The new auto-terminate value to set for this executor.
|
117
|
-
#
|
118
118
|
# @return [Boolean] `true` when auto-termination is enabled else `false`.
|
119
119
|
|
120
120
|
###################################################################
|
@@ -16,6 +16,9 @@ module Concurrent
|
|
16
16
|
# Default maximum number of seconds a thread in the pool may remain idle
|
17
17
|
# before being reclaimed.
|
18
18
|
|
19
|
+
# @!macro thread_pool_executor_constant_default_synchronous
|
20
|
+
# Default value of the :synchronous option.
|
21
|
+
|
19
22
|
# @!macro thread_pool_executor_attr_reader_max_length
|
20
23
|
# The maximum number of threads that may be created in the pool.
|
21
24
|
# @return [Integer] The maximum number of threads that may be created in the pool.
|
@@ -40,6 +43,10 @@ module Concurrent
|
|
40
43
|
# The number of seconds that a thread may be idle before being reclaimed.
|
41
44
|
# @return [Integer] The number of seconds that a thread may be idle before being reclaimed.
|
42
45
|
|
46
|
+
# @!macro thread_pool_executor_attr_reader_synchronous
|
47
|
+
# Whether or not a value of 0 for :max_queue option means the queue must perform direct hand-off or rather unbounded queue.
|
48
|
+
# @return [true, false]
|
49
|
+
|
43
50
|
# @!macro thread_pool_executor_attr_reader_max_queue
|
44
51
|
# The maximum number of tasks that may be waiting in the work queue at any one time.
|
45
52
|
# When the queue size reaches `max_queue` subsequent tasks will be rejected in
|
@@ -64,9 +71,16 @@ module Concurrent
|
|
64
71
|
# @return [Integer] Number of tasks that may be enqueued before reaching `max_queue` and rejecting
|
65
72
|
# new tasks. A value of -1 indicates that the queue may grow without bound.
|
66
73
|
|
67
|
-
|
68
|
-
|
69
|
-
|
74
|
+
# @!macro thread_pool_executor_method_prune_pool
|
75
|
+
# Prune the thread pool of unneeded threads
|
76
|
+
#
|
77
|
+
# What is being pruned is controlled by the min_threads and idletime
|
78
|
+
# parameters passed at pool creation time
|
79
|
+
#
|
80
|
+
# This is a no-op on some pool implementation (e.g. the Java one). The Ruby
|
81
|
+
# pool will auto-prune each time a new job is posted. You will need to call
|
82
|
+
# this method explicitely in case your application post jobs in bursts (a
|
83
|
+
# lot of jobs and then nothing for long periods)
|
70
84
|
|
71
85
|
# @!macro thread_pool_executor_public_api
|
72
86
|
#
|
@@ -104,6 +118,9 @@ module Concurrent
|
|
104
118
|
#
|
105
119
|
# @!method can_overflow?
|
106
120
|
# @!macro executor_service_method_can_overflow_question
|
121
|
+
#
|
122
|
+
# @!method prune_pool
|
123
|
+
# @!macro thread_pool_executor_method_prune_pool
|
107
124
|
|
108
125
|
|
109
126
|
|
@@ -115,12 +132,13 @@ module Concurrent
|
|
115
132
|
# Thread pools support several configuration options:
|
116
133
|
#
|
117
134
|
# * `idletime`: The number of seconds that a thread may be idle before being reclaimed.
|
135
|
+
# * `name`: The name of the executor (optional). Printed in the executor's `#to_s` output and
|
136
|
+
# a `<name>-worker-<id>` name is given to its threads if supported by used Ruby
|
137
|
+
# implementation. `<id>` is uniq for each thread.
|
118
138
|
# * `max_queue`: The maximum number of tasks that may be waiting in the work queue at
|
119
139
|
# any one time. When the queue size reaches `max_queue` and no new threads can be created,
|
120
140
|
# subsequent tasks will be rejected in accordance with the configured `fallback_policy`.
|
121
|
-
# * `auto_terminate`: When true (default)
|
122
|
-
# will stop the thread pool when the application exits. See below for more information
|
123
|
-
# on shutting down thread pools.
|
141
|
+
# * `auto_terminate`: When true (default), the threads started will be marked as daemon.
|
124
142
|
# * `fallback_policy`: The policy defining how rejected tasks are handled.
|
125
143
|
#
|
126
144
|
# Three fallback policies are supported:
|
@@ -145,16 +163,12 @@ module Concurrent
|
|
145
163
|
#
|
146
164
|
# On some runtime platforms (most notably the JVM) the application will not
|
147
165
|
# exit until all thread pools have been shutdown. To prevent applications from
|
148
|
-
# "hanging" on exit all
|
149
|
-
#
|
150
|
-
# force method to stop the pool and makes no guarantees regarding resources being
|
151
|
-
# used by any tasks still running. Registration of this `at_exit` handler can be
|
152
|
-
# prevented by setting the thread pool's constructor `:auto_terminate` option to
|
153
|
-
# `false` when the thread pool is created. All thread pools support this option.
|
166
|
+
# "hanging" on exit, all threads can be marked as daemon according to the
|
167
|
+
# `:auto_terminate` option.
|
154
168
|
#
|
155
169
|
# ```ruby
|
156
|
-
# pool1 = Concurrent::FixedThreadPool.new(5) #
|
157
|
-
# pool2 = Concurrent::FixedThreadPool.new(5, auto_terminate: false) #
|
170
|
+
# pool1 = Concurrent::FixedThreadPool.new(5) # threads will be marked as daemon
|
171
|
+
# pool2 = Concurrent::FixedThreadPool.new(5, auto_terminate: false) # mark threads as non-daemon
|
158
172
|
# ```
|
159
173
|
#
|
160
174
|
# @note Failure to properly shutdown a thread pool can lead to unpredictable results.
|
@@ -163,7 +177,7 @@ module Concurrent
|
|
163
177
|
# @see http://docs.oracle.com/javase/tutorial/essential/concurrency/pools.html Java Tutorials: Thread Pools
|
164
178
|
# @see http://docs.oracle.com/javase/7/docs/api/java/util/concurrent/Executors.html Java Executors class
|
165
179
|
# @see http://docs.oracle.com/javase/8/docs/api/java/util/concurrent/ExecutorService.html Java ExecutorService interface
|
166
|
-
# @see
|
180
|
+
# @see https://docs.oracle.com/javase/8/docs/api/java/lang/Thread.html#setDaemon-boolean-
|
167
181
|
|
168
182
|
|
169
183
|
|
@@ -1,7 +1,7 @@
|
|
1
|
-
|
1
|
+
require 'concurrent/utility/engine'
|
2
2
|
|
3
|
+
if Concurrent.on_jruby?
|
3
4
|
require 'concurrent/errors'
|
4
|
-
require 'concurrent/utility/engine'
|
5
5
|
require 'concurrent/executor/abstract_executor_service'
|
6
6
|
|
7
7
|
module Concurrent
|
@@ -18,13 +18,9 @@ if Concurrent.on_jruby?
|
|
18
18
|
}.freeze
|
19
19
|
private_constant :FALLBACK_POLICY_CLASSES
|
20
20
|
|
21
|
-
def initialize(*args, &block)
|
22
|
-
super
|
23
|
-
end
|
24
|
-
|
25
21
|
def post(*args, &task)
|
26
22
|
raise ArgumentError.new('no block given') unless block_given?
|
27
|
-
return
|
23
|
+
return fallback_action(*args, &task).call unless running?
|
28
24
|
@executor.submit Job.new(args, task)
|
29
25
|
true
|
30
26
|
rescue Java::JavaUtilConcurrent::RejectedExecutionException
|
@@ -42,7 +38,6 @@ if Concurrent.on_jruby?
|
|
42
38
|
|
43
39
|
def shutdown
|
44
40
|
synchronize do
|
45
|
-
self.ns_auto_terminate = false
|
46
41
|
@executor.shutdown
|
47
42
|
nil
|
48
43
|
end
|
@@ -50,7 +45,6 @@ if Concurrent.on_jruby?
|
|
50
45
|
|
51
46
|
def kill
|
52
47
|
synchronize do
|
53
|
-
self.ns_auto_terminate = false
|
54
48
|
@executor.shutdownNow
|
55
49
|
nil
|
56
50
|
end
|
@@ -87,5 +81,23 @@ if Concurrent.on_jruby?
|
|
87
81
|
end
|
88
82
|
private_constant :Job
|
89
83
|
end
|
84
|
+
|
85
|
+
class DaemonThreadFactory
|
86
|
+
# hide include from YARD
|
87
|
+
send :include, java.util.concurrent.ThreadFactory
|
88
|
+
|
89
|
+
def initialize(daemonize = true)
|
90
|
+
@daemonize = daemonize
|
91
|
+
end
|
92
|
+
|
93
|
+
def newThread(runnable)
|
94
|
+
thread = java.util.concurrent.Executors.defaultThreadFactory().newThread(runnable)
|
95
|
+
thread.setDaemon(@daemonize)
|
96
|
+
return thread
|
97
|
+
end
|
98
|
+
end
|
99
|
+
|
100
|
+
private_constant :DaemonThreadFactory
|
101
|
+
|
90
102
|
end
|
91
103
|
end
|
@@ -17,12 +17,13 @@ if Concurrent.on_jruby?
|
|
17
17
|
end
|
18
18
|
|
19
19
|
private
|
20
|
-
|
20
|
+
|
21
21
|
def ns_initialize(opts)
|
22
|
-
@executor = java.util.concurrent.Executors.newSingleThreadExecutor
|
22
|
+
@executor = java.util.concurrent.Executors.newSingleThreadExecutor(
|
23
|
+
DaemonThreadFactory.new(ns_auto_terminate?)
|
24
|
+
)
|
23
25
|
@fallback_policy = opts.fetch(:fallback_policy, :discard)
|
24
26
|
raise ArgumentError.new("#{@fallback_policy} is not a valid fallback policy") unless FALLBACK_POLICY_CLASSES.keys.include?(@fallback_policy)
|
25
|
-
self.auto_terminate = opts.fetch(:auto_terminate, true)
|
26
27
|
end
|
27
28
|
end
|
28
29
|
end
|
@@ -21,12 +21,18 @@ if Concurrent.on_jruby?
|
|
21
21
|
# @!macro thread_pool_executor_constant_default_thread_timeout
|
22
22
|
DEFAULT_THREAD_IDLETIMEOUT = 60
|
23
23
|
|
24
|
+
# @!macro thread_pool_executor_constant_default_synchronous
|
25
|
+
DEFAULT_SYNCHRONOUS = false
|
26
|
+
|
24
27
|
# @!macro thread_pool_executor_attr_reader_max_length
|
25
28
|
attr_reader :max_length
|
26
29
|
|
27
30
|
# @!macro thread_pool_executor_attr_reader_max_queue
|
28
31
|
attr_reader :max_queue
|
29
32
|
|
33
|
+
# @!macro thread_pool_executor_attr_reader_synchronous
|
34
|
+
attr_reader :synchronous
|
35
|
+
|
30
36
|
# @!macro thread_pool_executor_method_initialize
|
31
37
|
def initialize(opts = {})
|
32
38
|
super(opts)
|
@@ -87,6 +93,10 @@ if Concurrent.on_jruby?
|
|
87
93
|
super && !@executor.isTerminating
|
88
94
|
end
|
89
95
|
|
96
|
+
# @!macro thread_pool_executor_method_prune_pool
|
97
|
+
def prune_pool
|
98
|
+
end
|
99
|
+
|
90
100
|
private
|
91
101
|
|
92
102
|
def ns_initialize(opts)
|
@@ -94,8 +104,10 @@ if Concurrent.on_jruby?
|
|
94
104
|
max_length = opts.fetch(:max_threads, DEFAULT_MAX_POOL_SIZE).to_i
|
95
105
|
idletime = opts.fetch(:idletime, DEFAULT_THREAD_IDLETIMEOUT).to_i
|
96
106
|
@max_queue = opts.fetch(:max_queue, DEFAULT_MAX_QUEUE_SIZE).to_i
|
107
|
+
@synchronous = opts.fetch(:synchronous, DEFAULT_SYNCHRONOUS)
|
97
108
|
@fallback_policy = opts.fetch(:fallback_policy, :abort)
|
98
109
|
|
110
|
+
raise ArgumentError.new("`synchronous` cannot be set unless `max_queue` is 0") if @synchronous && @max_queue > 0
|
99
111
|
raise ArgumentError.new("`max_threads` cannot be less than #{DEFAULT_MIN_POOL_SIZE}") if max_length < DEFAULT_MIN_POOL_SIZE
|
100
112
|
raise ArgumentError.new("`max_threads` cannot be greater than #{DEFAULT_MAX_POOL_SIZE}") if max_length > DEFAULT_MAX_POOL_SIZE
|
101
113
|
raise ArgumentError.new("`min_threads` cannot be less than #{DEFAULT_MIN_POOL_SIZE}") if min_length < DEFAULT_MIN_POOL_SIZE
|
@@ -103,7 +115,11 @@ if Concurrent.on_jruby?
|
|
103
115
|
raise ArgumentError.new("#{fallback_policy} is not a valid fallback policy") unless FALLBACK_POLICY_CLASSES.include?(@fallback_policy)
|
104
116
|
|
105
117
|
if @max_queue == 0
|
106
|
-
|
118
|
+
if @synchronous
|
119
|
+
queue = java.util.concurrent.SynchronousQueue.new
|
120
|
+
else
|
121
|
+
queue = java.util.concurrent.LinkedBlockingQueue.new
|
122
|
+
end
|
107
123
|
else
|
108
124
|
queue = java.util.concurrent.LinkedBlockingQueue.new(@max_queue)
|
109
125
|
end
|
@@ -114,10 +130,11 @@ if Concurrent.on_jruby?
|
|
114
130
|
idletime,
|
115
131
|
java.util.concurrent.TimeUnit::SECONDS,
|
116
132
|
queue,
|
133
|
+
DaemonThreadFactory.new(ns_auto_terminate?),
|
117
134
|
FALLBACK_POLICY_CLASSES[@fallback_policy].new)
|
118
135
|
|
119
|
-
self.auto_terminate = opts.fetch(:auto_terminate, true)
|
120
136
|
end
|
121
137
|
end
|
138
|
+
|
122
139
|
end
|
123
140
|
end
|
@@ -16,10 +16,16 @@ module Concurrent
|
|
16
16
|
|
17
17
|
def post(*args, &task)
|
18
18
|
raise ArgumentError.new('no block given') unless block_given?
|
19
|
-
synchronize
|
20
|
-
|
21
|
-
|
22
|
-
|
19
|
+
deferred_action = synchronize {
|
20
|
+
if running?
|
21
|
+
ns_execute(*args, &task)
|
22
|
+
else
|
23
|
+
fallback_action(*args, &task)
|
24
|
+
end
|
25
|
+
}
|
26
|
+
if deferred_action
|
27
|
+
deferred_action.call
|
28
|
+
else
|
23
29
|
true
|
24
30
|
end
|
25
31
|
end
|
@@ -27,7 +33,6 @@ module Concurrent
|
|
27
33
|
def shutdown
|
28
34
|
synchronize do
|
29
35
|
break unless running?
|
30
|
-
self.ns_auto_terminate = false
|
31
36
|
stop_event.set
|
32
37
|
ns_shutdown_execution
|
33
38
|
end
|
@@ -37,7 +42,6 @@ module Concurrent
|
|
37
42
|
def kill
|
38
43
|
synchronize do
|
39
44
|
break if shutdown?
|
40
|
-
self.ns_auto_terminate = false
|
41
45
|
stop_event.set
|
42
46
|
ns_kill_execution
|
43
47
|
stopped_event.set
|