This commit is contained in:
+131
@@ -0,0 +1,131 @@
|
||||
require 'concurrent/errors'
|
||||
require 'concurrent/concern/deprecation'
|
||||
require 'concurrent/executor/executor_service'
|
||||
require 'concurrent/synchronization/lockable_object'
|
||||
|
||||
module Concurrent
|
||||
|
||||
# @!macro abstract_executor_service_public_api
|
||||
# @!visibility private
|
||||
class AbstractExecutorService < Synchronization::LockableObject
|
||||
include ExecutorService
|
||||
include Concern::Deprecation
|
||||
|
||||
# The set of possible fallback policies that may be set at thread pool creation.
|
||||
FALLBACK_POLICIES = [:abort, :discard, :caller_runs].freeze
|
||||
|
||||
# @!macro executor_service_attr_reader_fallback_policy
|
||||
attr_reader :fallback_policy
|
||||
|
||||
attr_reader :name
|
||||
|
||||
# Create a new thread pool.
|
||||
def initialize(opts = {}, &block)
|
||||
super(&nil)
|
||||
synchronize do
|
||||
@auto_terminate = opts.fetch(:auto_terminate, true)
|
||||
@name = opts.fetch(:name) if opts.key?(:name)
|
||||
ns_initialize(opts, &block)
|
||||
end
|
||||
end
|
||||
|
||||
def to_s
|
||||
name ? "#{super[0..-2]} name: #{name}>" : super
|
||||
end
|
||||
|
||||
# @!macro executor_service_method_shutdown
|
||||
def shutdown
|
||||
raise NotImplementedError
|
||||
end
|
||||
|
||||
# @!macro executor_service_method_kill
|
||||
def kill
|
||||
raise NotImplementedError
|
||||
end
|
||||
|
||||
# @!macro executor_service_method_wait_for_termination
|
||||
def wait_for_termination(timeout = nil)
|
||||
raise NotImplementedError
|
||||
end
|
||||
|
||||
# @!macro executor_service_method_running_question
|
||||
def running?
|
||||
synchronize { ns_running? }
|
||||
end
|
||||
|
||||
# @!macro executor_service_method_shuttingdown_question
|
||||
def shuttingdown?
|
||||
synchronize { ns_shuttingdown? }
|
||||
end
|
||||
|
||||
# @!macro executor_service_method_shutdown_question
|
||||
def shutdown?
|
||||
synchronize { ns_shutdown? }
|
||||
end
|
||||
|
||||
# @!macro executor_service_method_auto_terminate_question
|
||||
def auto_terminate?
|
||||
synchronize { @auto_terminate }
|
||||
end
|
||||
|
||||
# @!macro executor_service_method_auto_terminate_setter
|
||||
def auto_terminate=(value)
|
||||
deprecated "Method #auto_terminate= has no effect. Set :auto_terminate option when executor is initialized."
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
# Returns an action which executes the `fallback_policy` once the queue
|
||||
# size reaches `max_queue`. The reason for the indirection of an action
|
||||
# is so that the work can be deferred outside of synchronization.
|
||||
#
|
||||
# @param [Array] args the arguments to the task which is being handled.
|
||||
#
|
||||
# @!visibility private
|
||||
def fallback_action(*args)
|
||||
case fallback_policy
|
||||
when :abort
|
||||
lambda { raise RejectedExecutionError }
|
||||
when :discard
|
||||
lambda { false }
|
||||
when :caller_runs
|
||||
lambda {
|
||||
begin
|
||||
yield(*args)
|
||||
rescue => ex
|
||||
# let it fail
|
||||
log DEBUG, ex
|
||||
end
|
||||
true
|
||||
}
|
||||
else
|
||||
lambda { fail "Unknown fallback policy #{fallback_policy}" }
|
||||
end
|
||||
end
|
||||
|
||||
def ns_execute(*args, &task)
|
||||
raise NotImplementedError
|
||||
end
|
||||
|
||||
# @!macro executor_service_method_ns_shutdown_execution
|
||||
#
|
||||
# Callback method called when an orderly shutdown has completed.
|
||||
# The default behavior is to signal all waiting threads.
|
||||
def ns_shutdown_execution
|
||||
# do nothing
|
||||
end
|
||||
|
||||
# @!macro executor_service_method_ns_kill_execution
|
||||
#
|
||||
# Callback method called when the executor has been killed.
|
||||
# The default behavior is to do nothing.
|
||||
def ns_kill_execution
|
||||
# do nothing
|
||||
end
|
||||
|
||||
def ns_auto_terminate?
|
||||
@auto_terminate
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
+62
@@ -0,0 +1,62 @@
|
||||
require 'concurrent/utility/engine'
|
||||
require 'concurrent/executor/thread_pool_executor'
|
||||
|
||||
module Concurrent
|
||||
|
||||
# A thread pool that dynamically grows and shrinks to fit the current workload.
|
||||
# New threads are created as needed, existing threads are reused, and threads
|
||||
# that remain idle for too long are killed and removed from the pool. These
|
||||
# pools are particularly suited to applications that perform a high volume of
|
||||
# short-lived tasks.
|
||||
#
|
||||
# On creation a `CachedThreadPool` has zero running threads. New threads are
|
||||
# created on the pool as new operations are `#post`. The size of the pool
|
||||
# will grow until `#max_length` threads are in the pool or until the number
|
||||
# of threads exceeds the number of running and pending operations. When a new
|
||||
# operation is post to the pool the first available idle thread will be tasked
|
||||
# with the new operation.
|
||||
#
|
||||
# Should a thread crash for any reason the thread will immediately be removed
|
||||
# from the pool. Similarly, threads which remain idle for an extended period
|
||||
# of time will be killed and reclaimed. Thus these thread pools are very
|
||||
# efficient at reclaiming unused resources.
|
||||
#
|
||||
# The API and behavior of this class are based on Java's `CachedThreadPool`
|
||||
#
|
||||
# @!macro thread_pool_options
|
||||
class CachedThreadPool < ThreadPoolExecutor
|
||||
|
||||
# @!macro cached_thread_pool_method_initialize
|
||||
#
|
||||
# Create a new thread pool.
|
||||
#
|
||||
# @param [Hash] opts the options defining pool behavior.
|
||||
# @option opts [Symbol] :fallback_policy (`:abort`) the fallback policy
|
||||
#
|
||||
# @raise [ArgumentError] if `fallback_policy` is not a known policy
|
||||
#
|
||||
# @see http://docs.oracle.com/javase/8/docs/api/java/util/concurrent/Executors.html#newCachedThreadPool--
|
||||
def initialize(opts = {})
|
||||
defaults = { idletime: DEFAULT_THREAD_IDLETIMEOUT }
|
||||
overrides = { min_threads: 0,
|
||||
max_threads: DEFAULT_MAX_POOL_SIZE,
|
||||
max_queue: DEFAULT_MAX_QUEUE_SIZE }
|
||||
super(defaults.merge(opts).merge(overrides))
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
# @!macro cached_thread_pool_method_initialize
|
||||
# @!visibility private
|
||||
def ns_initialize(opts)
|
||||
super(opts)
|
||||
if Concurrent.on_jruby?
|
||||
@max_queue = 0
|
||||
@executor = java.util.concurrent.Executors.newCachedThreadPool(
|
||||
DaemonThreadFactory.new(ns_auto_terminate?))
|
||||
@executor.setRejectedExecutionHandler(FALLBACK_POLICY_CLASSES[@fallback_policy].new)
|
||||
@executor.setKeepAliveTime(opts.fetch(:idletime, DEFAULT_THREAD_IDLETIMEOUT), java.util.concurrent.TimeUnit::SECONDS)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
+185
@@ -0,0 +1,185 @@
|
||||
require 'concurrent/concern/logging'
|
||||
|
||||
module Concurrent
|
||||
|
||||
###################################################################
|
||||
|
||||
# @!macro executor_service_method_post
|
||||
#
|
||||
# Submit a task to the executor for asynchronous processing.
|
||||
#
|
||||
# @param [Array] args zero or more arguments to be passed to the task
|
||||
#
|
||||
# @yield the asynchronous task to perform
|
||||
#
|
||||
# @return [Boolean] `true` if the task is queued, `false` if the executor
|
||||
# is not running
|
||||
#
|
||||
# @raise [ArgumentError] if no task is given
|
||||
|
||||
# @!macro executor_service_method_left_shift
|
||||
#
|
||||
# Submit a task to the executor for asynchronous processing.
|
||||
#
|
||||
# @param [Proc] task the asynchronous task to perform
|
||||
#
|
||||
# @return [self] returns itself
|
||||
|
||||
# @!macro executor_service_method_can_overflow_question
|
||||
#
|
||||
# Does the task queue have a maximum size?
|
||||
#
|
||||
# @return [Boolean] True if the task queue has a maximum size else false.
|
||||
|
||||
# @!macro executor_service_method_serialized_question
|
||||
#
|
||||
# Does this executor guarantee serialization of its operations?
|
||||
#
|
||||
# @return [Boolean] True if the executor guarantees that all operations
|
||||
# will be post in the order they are received and no two operations may
|
||||
# occur simultaneously. Else false.
|
||||
|
||||
###################################################################
|
||||
|
||||
# @!macro executor_service_public_api
|
||||
#
|
||||
# @!method post(*args, &task)
|
||||
# @!macro executor_service_method_post
|
||||
#
|
||||
# @!method <<(task)
|
||||
# @!macro executor_service_method_left_shift
|
||||
#
|
||||
# @!method can_overflow?
|
||||
# @!macro executor_service_method_can_overflow_question
|
||||
#
|
||||
# @!method serialized?
|
||||
# @!macro executor_service_method_serialized_question
|
||||
|
||||
###################################################################
|
||||
|
||||
# @!macro executor_service_attr_reader_fallback_policy
|
||||
# @return [Symbol] The fallback policy in effect. Either `:abort`, `:discard`, or `:caller_runs`.
|
||||
|
||||
# @!macro executor_service_method_shutdown
|
||||
#
|
||||
# Begin an orderly shutdown. Tasks already in the queue will be executed,
|
||||
# but no new tasks will be accepted. Has no additional effect if the
|
||||
# thread pool is not running.
|
||||
|
||||
# @!macro executor_service_method_kill
|
||||
#
|
||||
# Begin an immediate shutdown. In-progress tasks will be allowed to
|
||||
# complete but enqueued tasks will be dismissed and no new tasks
|
||||
# will be accepted. Has no additional effect if the thread pool is
|
||||
# not running.
|
||||
|
||||
# @!macro executor_service_method_wait_for_termination
|
||||
#
|
||||
# Block until executor shutdown is complete or until `timeout` seconds have
|
||||
# passed.
|
||||
#
|
||||
# @note Does not initiate shutdown or termination. Either `shutdown` or `kill`
|
||||
# must be called before this method (or on another thread).
|
||||
#
|
||||
# @param [Integer] timeout the maximum number of seconds to wait for shutdown to complete
|
||||
#
|
||||
# @return [Boolean] `true` if shutdown complete or false on `timeout`
|
||||
|
||||
# @!macro executor_service_method_running_question
|
||||
#
|
||||
# Is the executor running?
|
||||
#
|
||||
# @return [Boolean] `true` when running, `false` when shutting down or shutdown
|
||||
|
||||
# @!macro executor_service_method_shuttingdown_question
|
||||
#
|
||||
# Is the executor shuttingdown?
|
||||
#
|
||||
# @return [Boolean] `true` when not running and not shutdown, else `false`
|
||||
|
||||
# @!macro executor_service_method_shutdown_question
|
||||
#
|
||||
# Is the executor shutdown?
|
||||
#
|
||||
# @return [Boolean] `true` when shutdown, `false` when shutting down or running
|
||||
|
||||
# @!macro executor_service_method_auto_terminate_question
|
||||
#
|
||||
# Is the executor auto-terminate when the application exits?
|
||||
#
|
||||
# @return [Boolean] `true` when auto-termination is enabled else `false`.
|
||||
|
||||
# @!macro executor_service_method_auto_terminate_setter
|
||||
#
|
||||
#
|
||||
# Set the auto-terminate behavior for this executor.
|
||||
# @deprecated Has no effect
|
||||
# @param [Boolean] value The new auto-terminate value to set for this executor.
|
||||
# @return [Boolean] `true` when auto-termination is enabled else `false`.
|
||||
|
||||
###################################################################
|
||||
|
||||
# @!macro abstract_executor_service_public_api
|
||||
#
|
||||
# @!macro executor_service_public_api
|
||||
#
|
||||
# @!attribute [r] fallback_policy
|
||||
# @!macro executor_service_attr_reader_fallback_policy
|
||||
#
|
||||
# @!method shutdown
|
||||
# @!macro executor_service_method_shutdown
|
||||
#
|
||||
# @!method kill
|
||||
# @!macro executor_service_method_kill
|
||||
#
|
||||
# @!method wait_for_termination(timeout = nil)
|
||||
# @!macro executor_service_method_wait_for_termination
|
||||
#
|
||||
# @!method running?
|
||||
# @!macro executor_service_method_running_question
|
||||
#
|
||||
# @!method shuttingdown?
|
||||
# @!macro executor_service_method_shuttingdown_question
|
||||
#
|
||||
# @!method shutdown?
|
||||
# @!macro executor_service_method_shutdown_question
|
||||
#
|
||||
# @!method auto_terminate?
|
||||
# @!macro executor_service_method_auto_terminate_question
|
||||
#
|
||||
# @!method auto_terminate=(value)
|
||||
# @!macro executor_service_method_auto_terminate_setter
|
||||
|
||||
###################################################################
|
||||
|
||||
# @!macro executor_service_public_api
|
||||
# @!visibility private
|
||||
module ExecutorService
|
||||
include Concern::Logging
|
||||
|
||||
# @!macro executor_service_method_post
|
||||
def post(*args, &task)
|
||||
raise NotImplementedError
|
||||
end
|
||||
|
||||
# @!macro executor_service_method_left_shift
|
||||
def <<(task)
|
||||
post(&task)
|
||||
self
|
||||
end
|
||||
|
||||
# @!macro executor_service_method_can_overflow_question
|
||||
#
|
||||
# @note Always returns `false`
|
||||
def can_overflow?
|
||||
false
|
||||
end
|
||||
|
||||
# @!macro executor_service_method_serialized_question
|
||||
#
|
||||
# @note Always returns `false`
|
||||
def serialized?
|
||||
false
|
||||
end
|
||||
end
|
||||
end
|
||||
+222
@@ -0,0 +1,222 @@
|
||||
require 'concurrent/utility/engine'
|
||||
require 'concurrent/executor/thread_pool_executor'
|
||||
|
||||
module Concurrent
|
||||
|
||||
# @!macro thread_pool_executor_constant_default_max_pool_size
|
||||
# Default maximum number of threads that will be created in the pool.
|
||||
|
||||
# @!macro thread_pool_executor_constant_default_min_pool_size
|
||||
# Default minimum number of threads that will be retained in the pool.
|
||||
|
||||
# @!macro thread_pool_executor_constant_default_max_queue_size
|
||||
# Default maximum number of tasks that may be added to the task queue.
|
||||
|
||||
# @!macro thread_pool_executor_constant_default_thread_timeout
|
||||
# Default maximum number of seconds a thread in the pool may remain idle
|
||||
# before being reclaimed.
|
||||
|
||||
# @!macro thread_pool_executor_constant_default_synchronous
|
||||
# Default value of the :synchronous option.
|
||||
|
||||
# @!macro thread_pool_executor_attr_reader_max_length
|
||||
# The maximum number of threads that may be created in the pool.
|
||||
# @return [Integer] The maximum number of threads that may be created in the pool.
|
||||
|
||||
# @!macro thread_pool_executor_attr_reader_min_length
|
||||
# The minimum number of threads that may be retained in the pool.
|
||||
# @return [Integer] The minimum number of threads that may be retained in the pool.
|
||||
|
||||
# @!macro thread_pool_executor_attr_reader_largest_length
|
||||
# The largest number of threads that have been created in the pool since construction.
|
||||
# @return [Integer] The largest number of threads that have been created in the pool since construction.
|
||||
|
||||
# @!macro thread_pool_executor_attr_reader_scheduled_task_count
|
||||
# The number of tasks that have been scheduled for execution on the pool since construction.
|
||||
# @return [Integer] The number of tasks that have been scheduled for execution on the pool since construction.
|
||||
|
||||
# @!macro thread_pool_executor_attr_reader_completed_task_count
|
||||
# The number of tasks that have been completed by the pool since construction.
|
||||
# @return [Integer] The number of tasks that have been completed by the pool since construction.
|
||||
|
||||
# @!macro thread_pool_executor_method_active_count
|
||||
# The number of threads that are actively executing tasks.
|
||||
# @return [Integer] The number of threads that are actively executing tasks.
|
||||
|
||||
# @!macro thread_pool_executor_attr_reader_idletime
|
||||
# The number of seconds that a thread may be idle before being reclaimed.
|
||||
# @return [Integer] The number of seconds that a thread may be idle before being reclaimed.
|
||||
|
||||
# @!macro thread_pool_executor_attr_reader_synchronous
|
||||
# Whether or not a value of 0 for :max_queue option means the queue must perform direct hand-off or rather unbounded queue.
|
||||
# @return [true, false]
|
||||
|
||||
# @!macro thread_pool_executor_attr_reader_max_queue
|
||||
# The maximum number of tasks that may be waiting in the work queue at any one time.
|
||||
# When the queue size reaches `max_queue` subsequent tasks will be rejected in
|
||||
# accordance with the configured `fallback_policy`.
|
||||
#
|
||||
# @return [Integer] The maximum number of tasks that may be waiting in the work queue at any one time.
|
||||
# When the queue size reaches `max_queue` subsequent tasks will be rejected in
|
||||
# accordance with the configured `fallback_policy`.
|
||||
|
||||
# @!macro thread_pool_executor_attr_reader_length
|
||||
# The number of threads currently in the pool.
|
||||
# @return [Integer] The number of threads currently in the pool.
|
||||
|
||||
# @!macro thread_pool_executor_attr_reader_queue_length
|
||||
# The number of tasks in the queue awaiting execution.
|
||||
# @return [Integer] The number of tasks in the queue awaiting execution.
|
||||
|
||||
# @!macro thread_pool_executor_attr_reader_remaining_capacity
|
||||
# Number of tasks that may be enqueued before reaching `max_queue` and rejecting
|
||||
# new tasks. A value of -1 indicates that the queue may grow without bound.
|
||||
#
|
||||
# @return [Integer] Number of tasks that may be enqueued before reaching `max_queue` and rejecting
|
||||
# new tasks. A value of -1 indicates that the queue may grow without bound.
|
||||
|
||||
# @!macro thread_pool_executor_method_prune_pool
|
||||
# Prune the thread pool of unneeded threads
|
||||
#
|
||||
# What is being pruned is controlled by the min_threads and idletime
|
||||
# parameters passed at pool creation time
|
||||
#
|
||||
# This is a no-op on all pool implementations as they prune themselves
|
||||
# automatically, and has been deprecated.
|
||||
|
||||
# @!macro thread_pool_executor_public_api
|
||||
#
|
||||
# @!macro abstract_executor_service_public_api
|
||||
#
|
||||
# @!attribute [r] max_length
|
||||
# @!macro thread_pool_executor_attr_reader_max_length
|
||||
#
|
||||
# @!attribute [r] min_length
|
||||
# @!macro thread_pool_executor_attr_reader_min_length
|
||||
#
|
||||
# @!attribute [r] largest_length
|
||||
# @!macro thread_pool_executor_attr_reader_largest_length
|
||||
#
|
||||
# @!attribute [r] scheduled_task_count
|
||||
# @!macro thread_pool_executor_attr_reader_scheduled_task_count
|
||||
#
|
||||
# @!attribute [r] completed_task_count
|
||||
# @!macro thread_pool_executor_attr_reader_completed_task_count
|
||||
#
|
||||
# @!attribute [r] idletime
|
||||
# @!macro thread_pool_executor_attr_reader_idletime
|
||||
#
|
||||
# @!attribute [r] max_queue
|
||||
# @!macro thread_pool_executor_attr_reader_max_queue
|
||||
#
|
||||
# @!attribute [r] length
|
||||
# @!macro thread_pool_executor_attr_reader_length
|
||||
#
|
||||
# @!attribute [r] queue_length
|
||||
# @!macro thread_pool_executor_attr_reader_queue_length
|
||||
#
|
||||
# @!attribute [r] remaining_capacity
|
||||
# @!macro thread_pool_executor_attr_reader_remaining_capacity
|
||||
#
|
||||
# @!method can_overflow?
|
||||
# @!macro executor_service_method_can_overflow_question
|
||||
#
|
||||
# @!method prune_pool
|
||||
# @!macro thread_pool_executor_method_prune_pool
|
||||
|
||||
|
||||
|
||||
|
||||
# @!macro thread_pool_options
|
||||
#
|
||||
# **Thread Pool Options**
|
||||
#
|
||||
# Thread pools support several configuration options:
|
||||
#
|
||||
# * `idletime`: The number of seconds that a thread may be idle before being reclaimed.
|
||||
# * `name`: The name of the executor (optional). Printed in the executor's `#to_s` output and
|
||||
# a `<name>-worker-<id>` name is given to its threads if supported by used Ruby
|
||||
# implementation. `<id>` is uniq for each thread.
|
||||
# * `max_queue`: The maximum number of tasks that may be waiting in the work queue at
|
||||
# any one time. When the queue size reaches `max_queue` and no new threads can be created,
|
||||
# subsequent tasks will be rejected in accordance with the configured `fallback_policy`.
|
||||
# * `auto_terminate`: When true (default), the threads started will be marked as daemon.
|
||||
# * `fallback_policy`: The policy defining how rejected tasks are handled.
|
||||
#
|
||||
# Three fallback policies are supported:
|
||||
#
|
||||
# * `:abort`: Raise a `RejectedExecutionError` exception and discard the task.
|
||||
# * `:discard`: Discard the task and return false.
|
||||
# * `:caller_runs`: Execute the task on the calling thread.
|
||||
#
|
||||
# **Shutting Down Thread Pools**
|
||||
#
|
||||
# Killing a thread pool while tasks are still being processed, either by calling
|
||||
# the `#kill` method or at application exit, will have unpredictable results. There
|
||||
# is no way for the thread pool to know what resources are being used by the
|
||||
# in-progress tasks. When those tasks are killed the impact on those resources
|
||||
# cannot be predicted. The *best* practice is to explicitly shutdown all thread
|
||||
# pools using the provided methods:
|
||||
#
|
||||
# * Call `#shutdown` to initiate an orderly termination of all in-progress tasks
|
||||
# * Call `#wait_for_termination` with an appropriate timeout interval an allow
|
||||
# the orderly shutdown to complete
|
||||
# * Call `#kill` *only when* the thread pool fails to shutdown in the allotted time
|
||||
#
|
||||
# On some runtime platforms (most notably the JVM) the application will not
|
||||
# exit until all thread pools have been shutdown. To prevent applications from
|
||||
# "hanging" on exit, all threads can be marked as daemon according to the
|
||||
# `:auto_terminate` option.
|
||||
#
|
||||
# ```ruby
|
||||
# pool1 = Concurrent::FixedThreadPool.new(5) # threads will be marked as daemon
|
||||
# pool2 = Concurrent::FixedThreadPool.new(5, auto_terminate: false) # mark threads as non-daemon
|
||||
# ```
|
||||
#
|
||||
# @note Failure to properly shutdown a thread pool can lead to unpredictable results.
|
||||
# Please read *Shutting Down Thread Pools* for more information.
|
||||
#
|
||||
# @see http://docs.oracle.com/javase/tutorial/essential/concurrency/pools.html Java Tutorials: Thread Pools
|
||||
# @see http://docs.oracle.com/javase/7/docs/api/java/util/concurrent/Executors.html Java Executors class
|
||||
# @see http://docs.oracle.com/javase/8/docs/api/java/util/concurrent/ExecutorService.html Java ExecutorService interface
|
||||
# @see https://docs.oracle.com/javase/8/docs/api/java/lang/Thread.html#setDaemon-boolean-
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
# @!macro fixed_thread_pool
|
||||
#
|
||||
# A thread pool that reuses a fixed number of threads operating off an unbounded queue.
|
||||
# At any point, at most `num_threads` will be active processing tasks. When all threads are busy new
|
||||
# tasks `#post` to the thread pool are enqueued until a thread becomes available.
|
||||
# Should a thread crash for any reason the thread will immediately be removed
|
||||
# from the pool and replaced.
|
||||
#
|
||||
# The API and behavior of this class are based on Java's `FixedThreadPool`
|
||||
#
|
||||
# @!macro thread_pool_options
|
||||
class FixedThreadPool < ThreadPoolExecutor
|
||||
|
||||
# @!macro fixed_thread_pool_method_initialize
|
||||
#
|
||||
# Create a new thread pool.
|
||||
#
|
||||
# @param [Integer] num_threads the number of threads to allocate
|
||||
# @param [Hash] opts the options defining pool behavior.
|
||||
# @option opts [Symbol] :fallback_policy (`:abort`) the fallback policy
|
||||
#
|
||||
# @raise [ArgumentError] if `num_threads` is less than or equal to zero
|
||||
# @raise [ArgumentError] if `fallback_policy` is not a known policy
|
||||
#
|
||||
# @see http://docs.oracle.com/javase/8/docs/api/java/util/concurrent/Executors.html#newFixedThreadPool-int-
|
||||
def initialize(num_threads, opts = {})
|
||||
raise ArgumentError.new('number of threads must be greater than zero') if num_threads.to_i < 1
|
||||
defaults = { max_queue: DEFAULT_MAX_QUEUE_SIZE,
|
||||
idletime: DEFAULT_THREAD_IDLETIMEOUT }
|
||||
overrides = { min_threads: num_threads,
|
||||
max_threads: num_threads }
|
||||
super(defaults.merge(opts).merge(overrides))
|
||||
end
|
||||
end
|
||||
end
|
||||
+66
@@ -0,0 +1,66 @@
|
||||
require 'concurrent/atomic/event'
|
||||
require 'concurrent/executor/abstract_executor_service'
|
||||
require 'concurrent/executor/serial_executor_service'
|
||||
|
||||
module Concurrent
|
||||
|
||||
# An executor service which runs all operations on the current thread,
|
||||
# blocking as necessary. Operations are performed in the order they are
|
||||
# received and no two operations can be performed simultaneously.
|
||||
#
|
||||
# This executor service exists mainly for testing an debugging. When used
|
||||
# it immediately runs every `#post` operation on the current thread, blocking
|
||||
# that thread until the operation is complete. This can be very beneficial
|
||||
# during testing because it makes all operations deterministic.
|
||||
#
|
||||
# @note Intended for use primarily in testing and debugging.
|
||||
class ImmediateExecutor < AbstractExecutorService
|
||||
include SerialExecutorService
|
||||
|
||||
# Creates a new executor
|
||||
def initialize
|
||||
@stopped = Concurrent::Event.new
|
||||
end
|
||||
|
||||
# @!macro executor_service_method_post
|
||||
def post(*args, &task)
|
||||
raise ArgumentError.new('no block given') unless block_given?
|
||||
return false unless running?
|
||||
task.call(*args)
|
||||
true
|
||||
end
|
||||
|
||||
# @!macro executor_service_method_left_shift
|
||||
def <<(task)
|
||||
post(&task)
|
||||
self
|
||||
end
|
||||
|
||||
# @!macro executor_service_method_running_question
|
||||
def running?
|
||||
! shutdown?
|
||||
end
|
||||
|
||||
# @!macro executor_service_method_shuttingdown_question
|
||||
def shuttingdown?
|
||||
false
|
||||
end
|
||||
|
||||
# @!macro executor_service_method_shutdown_question
|
||||
def shutdown?
|
||||
@stopped.set?
|
||||
end
|
||||
|
||||
# @!macro executor_service_method_shutdown
|
||||
def shutdown
|
||||
@stopped.set
|
||||
true
|
||||
end
|
||||
alias_method :kill, :shutdown
|
||||
|
||||
# @!macro executor_service_method_wait_for_termination
|
||||
def wait_for_termination(timeout = nil)
|
||||
@stopped.wait(timeout)
|
||||
end
|
||||
end
|
||||
end
|
||||
+44
@@ -0,0 +1,44 @@
|
||||
require 'concurrent/executor/immediate_executor'
|
||||
require 'concurrent/executor/simple_executor_service'
|
||||
|
||||
module Concurrent
|
||||
# An executor service which runs all operations on a new thread, blocking
|
||||
# until it completes. Operations are performed in the order they are received
|
||||
# and no two operations can be performed simultaneously.
|
||||
#
|
||||
# This executor service exists mainly for testing an debugging. When used it
|
||||
# immediately runs every `#post` operation on a new thread, blocking the
|
||||
# current thread until the operation is complete. This is similar to how the
|
||||
# ImmediateExecutor works, but the operation has the full stack of the new
|
||||
# thread at its disposal. This can be helpful when the operations will spawn
|
||||
# more operations on the same executor and so on - such a situation might
|
||||
# overflow the single stack in case of an ImmediateExecutor, which is
|
||||
# inconsistent with how it would behave for a threaded executor.
|
||||
#
|
||||
# @note Intended for use primarily in testing and debugging.
|
||||
class IndirectImmediateExecutor < ImmediateExecutor
|
||||
# Creates a new executor
|
||||
def initialize
|
||||
super
|
||||
@internal_executor = SimpleExecutorService.new
|
||||
end
|
||||
|
||||
# @!macro executor_service_method_post
|
||||
def post(*args, &task)
|
||||
raise ArgumentError.new("no block given") unless block_given?
|
||||
return false unless running?
|
||||
|
||||
event = Concurrent::Event.new
|
||||
@internal_executor.post do
|
||||
begin
|
||||
task.call(*args)
|
||||
ensure
|
||||
event.set
|
||||
end
|
||||
end
|
||||
event.wait
|
||||
|
||||
true
|
||||
end
|
||||
end
|
||||
end
|
||||
+101
@@ -0,0 +1,101 @@
|
||||
require 'concurrent/utility/engine'
|
||||
|
||||
if Concurrent.on_jruby?
|
||||
require 'concurrent/errors'
|
||||
require 'concurrent/executor/abstract_executor_service'
|
||||
|
||||
module Concurrent
|
||||
|
||||
# @!macro abstract_executor_service_public_api
|
||||
# @!visibility private
|
||||
class JavaExecutorService < AbstractExecutorService
|
||||
java_import 'java.lang.Runnable'
|
||||
|
||||
FALLBACK_POLICY_CLASSES = {
|
||||
abort: java.util.concurrent.ThreadPoolExecutor::AbortPolicy,
|
||||
discard: java.util.concurrent.ThreadPoolExecutor::DiscardPolicy,
|
||||
caller_runs: java.util.concurrent.ThreadPoolExecutor::CallerRunsPolicy
|
||||
}.freeze
|
||||
private_constant :FALLBACK_POLICY_CLASSES
|
||||
|
||||
def post(*args, &task)
|
||||
raise ArgumentError.new('no block given') unless block_given?
|
||||
return fallback_action(*args, &task).call unless running?
|
||||
@executor.submit Job.new(args, task)
|
||||
true
|
||||
rescue Java::JavaUtilConcurrent::RejectedExecutionException
|
||||
raise RejectedExecutionError
|
||||
end
|
||||
|
||||
def wait_for_termination(timeout = nil)
|
||||
if timeout.nil?
|
||||
ok = @executor.awaitTermination(60, java.util.concurrent.TimeUnit::SECONDS) until ok
|
||||
true
|
||||
else
|
||||
@executor.awaitTermination(1000 * timeout, java.util.concurrent.TimeUnit::MILLISECONDS)
|
||||
end
|
||||
end
|
||||
|
||||
def shutdown
|
||||
synchronize do
|
||||
@executor.shutdown
|
||||
nil
|
||||
end
|
||||
end
|
||||
|
||||
def kill
|
||||
synchronize do
|
||||
@executor.shutdownNow
|
||||
wait_for_termination
|
||||
nil
|
||||
end
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def ns_running?
|
||||
!(ns_shuttingdown? || ns_shutdown?)
|
||||
end
|
||||
|
||||
def ns_shuttingdown?
|
||||
@executor.isShutdown && !@executor.isTerminated
|
||||
end
|
||||
|
||||
def ns_shutdown?
|
||||
@executor.isTerminated
|
||||
end
|
||||
|
||||
class Job
|
||||
include Runnable
|
||||
def initialize(args, block)
|
||||
@args = args
|
||||
@block = block
|
||||
end
|
||||
|
||||
def run
|
||||
@block.call(*@args)
|
||||
end
|
||||
end
|
||||
private_constant :Job
|
||||
end
|
||||
|
||||
class DaemonThreadFactory
|
||||
# hide include from YARD
|
||||
send :include, java.util.concurrent.ThreadFactory
|
||||
|
||||
def initialize(daemonize = true)
|
||||
@daemonize = daemonize
|
||||
@java_thread_factory = java.util.concurrent.Executors.defaultThreadFactory
|
||||
end
|
||||
|
||||
def newThread(runnable)
|
||||
thread = @java_thread_factory.newThread(runnable)
|
||||
thread.setDaemon(@daemonize)
|
||||
return thread
|
||||
end
|
||||
end
|
||||
|
||||
private_constant :DaemonThreadFactory
|
||||
|
||||
end
|
||||
end
|
||||
+30
@@ -0,0 +1,30 @@
|
||||
if Concurrent.on_jruby?
|
||||
|
||||
require 'concurrent/executor/java_executor_service'
|
||||
require 'concurrent/executor/serial_executor_service'
|
||||
|
||||
module Concurrent
|
||||
|
||||
# @!macro single_thread_executor
|
||||
# @!macro abstract_executor_service_public_api
|
||||
# @!visibility private
|
||||
class JavaSingleThreadExecutor < JavaExecutorService
|
||||
include SerialExecutorService
|
||||
|
||||
# @!macro single_thread_executor_method_initialize
|
||||
def initialize(opts = {})
|
||||
super(opts)
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def ns_initialize(opts)
|
||||
@executor = java.util.concurrent.Executors.newSingleThreadExecutor(
|
||||
DaemonThreadFactory.new(ns_auto_terminate?)
|
||||
)
|
||||
@fallback_policy = opts.fetch(:fallback_policy, :discard)
|
||||
raise ArgumentError.new("#{@fallback_policy} is not a valid fallback policy") unless FALLBACK_POLICY_CLASSES.keys.include?(@fallback_policy)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
+147
@@ -0,0 +1,147 @@
|
||||
if Concurrent.on_jruby?
|
||||
|
||||
require 'concurrent/executor/java_executor_service'
|
||||
|
||||
module Concurrent
|
||||
|
||||
# @!macro thread_pool_executor
|
||||
# @!macro thread_pool_options
|
||||
# @!visibility private
|
||||
class JavaThreadPoolExecutor < JavaExecutorService
|
||||
include Concern::Deprecation
|
||||
|
||||
# @!macro thread_pool_executor_constant_default_max_pool_size
|
||||
DEFAULT_MAX_POOL_SIZE = java.lang.Integer::MAX_VALUE # 2147483647
|
||||
|
||||
# @!macro thread_pool_executor_constant_default_min_pool_size
|
||||
DEFAULT_MIN_POOL_SIZE = 0
|
||||
|
||||
# @!macro thread_pool_executor_constant_default_max_queue_size
|
||||
DEFAULT_MAX_QUEUE_SIZE = 0
|
||||
|
||||
# @!macro thread_pool_executor_constant_default_thread_timeout
|
||||
DEFAULT_THREAD_IDLETIMEOUT = 60
|
||||
|
||||
# @!macro thread_pool_executor_constant_default_synchronous
|
||||
DEFAULT_SYNCHRONOUS = false
|
||||
|
||||
# @!macro thread_pool_executor_attr_reader_max_length
|
||||
attr_reader :max_length
|
||||
|
||||
# @!macro thread_pool_executor_attr_reader_max_queue
|
||||
attr_reader :max_queue
|
||||
|
||||
# @!macro thread_pool_executor_attr_reader_synchronous
|
||||
attr_reader :synchronous
|
||||
|
||||
# @!macro thread_pool_executor_method_initialize
|
||||
def initialize(opts = {})
|
||||
super(opts)
|
||||
end
|
||||
|
||||
# @!macro executor_service_method_can_overflow_question
|
||||
def can_overflow?
|
||||
@max_queue != 0
|
||||
end
|
||||
|
||||
# @!macro thread_pool_executor_attr_reader_min_length
|
||||
def min_length
|
||||
@executor.getCorePoolSize
|
||||
end
|
||||
|
||||
# @!macro thread_pool_executor_attr_reader_max_length
|
||||
def max_length
|
||||
@executor.getMaximumPoolSize
|
||||
end
|
||||
|
||||
# @!macro thread_pool_executor_attr_reader_length
|
||||
def length
|
||||
@executor.getPoolSize
|
||||
end
|
||||
|
||||
# @!macro thread_pool_executor_attr_reader_largest_length
|
||||
def largest_length
|
||||
@executor.getLargestPoolSize
|
||||
end
|
||||
|
||||
# @!macro thread_pool_executor_attr_reader_scheduled_task_count
|
||||
def scheduled_task_count
|
||||
@executor.getTaskCount
|
||||
end
|
||||
|
||||
# @!macro thread_pool_executor_attr_reader_completed_task_count
|
||||
def completed_task_count
|
||||
@executor.getCompletedTaskCount
|
||||
end
|
||||
|
||||
# @!macro thread_pool_executor_method_active_count
|
||||
def active_count
|
||||
@executor.getActiveCount
|
||||
end
|
||||
|
||||
# @!macro thread_pool_executor_attr_reader_idletime
|
||||
def idletime
|
||||
@executor.getKeepAliveTime(java.util.concurrent.TimeUnit::SECONDS)
|
||||
end
|
||||
|
||||
# @!macro thread_pool_executor_attr_reader_queue_length
|
||||
def queue_length
|
||||
@executor.getQueue.size
|
||||
end
|
||||
|
||||
# @!macro thread_pool_executor_attr_reader_remaining_capacity
|
||||
def remaining_capacity
|
||||
@max_queue == 0 ? -1 : @executor.getQueue.remainingCapacity
|
||||
end
|
||||
|
||||
# @!macro executor_service_method_running_question
|
||||
def running?
|
||||
super && !@executor.isTerminating
|
||||
end
|
||||
|
||||
# @!macro thread_pool_executor_method_prune_pool
|
||||
def prune_pool
|
||||
deprecated "#prune_pool has no effect and will be removed in the next release."
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def ns_initialize(opts)
|
||||
min_length = opts.fetch(:min_threads, DEFAULT_MIN_POOL_SIZE).to_i
|
||||
max_length = opts.fetch(:max_threads, DEFAULT_MAX_POOL_SIZE).to_i
|
||||
idletime = opts.fetch(:idletime, DEFAULT_THREAD_IDLETIMEOUT).to_i
|
||||
@max_queue = opts.fetch(:max_queue, DEFAULT_MAX_QUEUE_SIZE).to_i
|
||||
@synchronous = opts.fetch(:synchronous, DEFAULT_SYNCHRONOUS)
|
||||
@fallback_policy = opts.fetch(:fallback_policy, :abort)
|
||||
|
||||
raise ArgumentError.new("`synchronous` cannot be set unless `max_queue` is 0") if @synchronous && @max_queue > 0
|
||||
raise ArgumentError.new("`max_threads` cannot be less than #{DEFAULT_MIN_POOL_SIZE}") if max_length < DEFAULT_MIN_POOL_SIZE
|
||||
raise ArgumentError.new("`max_threads` cannot be greater than #{DEFAULT_MAX_POOL_SIZE}") if max_length > DEFAULT_MAX_POOL_SIZE
|
||||
raise ArgumentError.new("`min_threads` cannot be less than #{DEFAULT_MIN_POOL_SIZE}") if min_length < DEFAULT_MIN_POOL_SIZE
|
||||
raise ArgumentError.new("`min_threads` cannot be more than `max_threads`") if min_length > max_length
|
||||
raise ArgumentError.new("#{fallback_policy} is not a valid fallback policy") unless FALLBACK_POLICY_CLASSES.include?(@fallback_policy)
|
||||
|
||||
if @max_queue == 0
|
||||
if @synchronous
|
||||
queue = java.util.concurrent.SynchronousQueue.new
|
||||
else
|
||||
queue = java.util.concurrent.LinkedBlockingQueue.new
|
||||
end
|
||||
else
|
||||
queue = java.util.concurrent.LinkedBlockingQueue.new(@max_queue)
|
||||
end
|
||||
|
||||
@executor = java.util.concurrent.ThreadPoolExecutor.new(
|
||||
min_length,
|
||||
max_length,
|
||||
idletime,
|
||||
java.util.concurrent.TimeUnit::SECONDS,
|
||||
queue,
|
||||
DaemonThreadFactory.new(ns_auto_terminate?),
|
||||
FALLBACK_POLICY_CLASSES[@fallback_policy].new)
|
||||
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
+82
@@ -0,0 +1,82 @@
|
||||
require 'concurrent/executor/abstract_executor_service'
|
||||
require 'concurrent/atomic/event'
|
||||
|
||||
module Concurrent
|
||||
|
||||
# @!macro abstract_executor_service_public_api
|
||||
# @!visibility private
|
||||
class RubyExecutorService < AbstractExecutorService
|
||||
safe_initialization!
|
||||
|
||||
def initialize(*args, &block)
|
||||
super
|
||||
@StopEvent = Event.new
|
||||
@StoppedEvent = Event.new
|
||||
end
|
||||
|
||||
def post(*args, &task)
|
||||
raise ArgumentError.new('no block given') unless block_given?
|
||||
deferred_action = synchronize {
|
||||
if running?
|
||||
ns_execute(*args, &task)
|
||||
else
|
||||
fallback_action(*args, &task)
|
||||
end
|
||||
}
|
||||
if deferred_action
|
||||
deferred_action.call
|
||||
else
|
||||
true
|
||||
end
|
||||
end
|
||||
|
||||
def shutdown
|
||||
synchronize do
|
||||
break unless running?
|
||||
stop_event.set
|
||||
ns_shutdown_execution
|
||||
end
|
||||
true
|
||||
end
|
||||
|
||||
def kill
|
||||
synchronize do
|
||||
break if shutdown?
|
||||
stop_event.set
|
||||
ns_kill_execution
|
||||
stopped_event.set
|
||||
end
|
||||
true
|
||||
end
|
||||
|
||||
def wait_for_termination(timeout = nil)
|
||||
stopped_event.wait(timeout)
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def stop_event
|
||||
@StopEvent
|
||||
end
|
||||
|
||||
def stopped_event
|
||||
@StoppedEvent
|
||||
end
|
||||
|
||||
def ns_shutdown_execution
|
||||
stopped_event.set
|
||||
end
|
||||
|
||||
def ns_running?
|
||||
!stop_event.set?
|
||||
end
|
||||
|
||||
def ns_shuttingdown?
|
||||
!(ns_running? || ns_shutdown?)
|
||||
end
|
||||
|
||||
def ns_shutdown?
|
||||
stopped_event.set?
|
||||
end
|
||||
end
|
||||
end
|
||||
+23
@@ -0,0 +1,23 @@
|
||||
require 'concurrent/executor/ruby_thread_pool_executor'
|
||||
require 'concurrent/executor/serial_executor_service'
|
||||
|
||||
module Concurrent
|
||||
|
||||
# @!macro single_thread_executor
|
||||
# @!macro abstract_executor_service_public_api
|
||||
# @!visibility private
|
||||
class RubySingleThreadExecutor < RubyThreadPoolExecutor
|
||||
include SerialExecutorService
|
||||
|
||||
# @!macro single_thread_executor_method_initialize
|
||||
def initialize(opts = {})
|
||||
super(
|
||||
min_threads: 1,
|
||||
max_threads: 1,
|
||||
max_queue: 0,
|
||||
idletime: DEFAULT_THREAD_IDLETIMEOUT,
|
||||
fallback_policy: opts.fetch(:fallback_policy, :discard),
|
||||
)
|
||||
end
|
||||
end
|
||||
end
|
||||
+396
@@ -0,0 +1,396 @@
|
||||
require 'thread'
|
||||
require 'concurrent/atomic/event'
|
||||
require 'concurrent/concern/logging'
|
||||
require 'concurrent/executor/ruby_executor_service'
|
||||
require 'concurrent/utility/monotonic_time'
|
||||
require 'concurrent/collection/timeout_queue'
|
||||
|
||||
module Concurrent
|
||||
|
||||
# @!macro thread_pool_executor
|
||||
# @!macro thread_pool_options
|
||||
# @!visibility private
|
||||
class RubyThreadPoolExecutor < RubyExecutorService
|
||||
include Concern::Deprecation
|
||||
|
||||
# @!macro thread_pool_executor_constant_default_max_pool_size
|
||||
DEFAULT_MAX_POOL_SIZE = 2_147_483_647 # java.lang.Integer::MAX_VALUE
|
||||
|
||||
# @!macro thread_pool_executor_constant_default_min_pool_size
|
||||
DEFAULT_MIN_POOL_SIZE = 0
|
||||
|
||||
# @!macro thread_pool_executor_constant_default_max_queue_size
|
||||
DEFAULT_MAX_QUEUE_SIZE = 0
|
||||
|
||||
# @!macro thread_pool_executor_constant_default_thread_timeout
|
||||
DEFAULT_THREAD_IDLETIMEOUT = 60
|
||||
|
||||
# @!macro thread_pool_executor_constant_default_synchronous
|
||||
DEFAULT_SYNCHRONOUS = false
|
||||
|
||||
# @!macro thread_pool_executor_attr_reader_max_length
|
||||
attr_reader :max_length
|
||||
|
||||
# @!macro thread_pool_executor_attr_reader_min_length
|
||||
attr_reader :min_length
|
||||
|
||||
# @!macro thread_pool_executor_attr_reader_idletime
|
||||
attr_reader :idletime
|
||||
|
||||
# @!macro thread_pool_executor_attr_reader_max_queue
|
||||
attr_reader :max_queue
|
||||
|
||||
# @!macro thread_pool_executor_attr_reader_synchronous
|
||||
attr_reader :synchronous
|
||||
|
||||
# @!macro thread_pool_executor_method_initialize
|
||||
def initialize(opts = {})
|
||||
super(opts)
|
||||
end
|
||||
|
||||
# @!macro thread_pool_executor_attr_reader_largest_length
|
||||
def largest_length
|
||||
synchronize { @largest_length }
|
||||
end
|
||||
|
||||
# @!macro thread_pool_executor_attr_reader_scheduled_task_count
|
||||
def scheduled_task_count
|
||||
synchronize { @scheduled_task_count }
|
||||
end
|
||||
|
||||
# @!macro thread_pool_executor_attr_reader_completed_task_count
|
||||
def completed_task_count
|
||||
synchronize { @completed_task_count }
|
||||
end
|
||||
|
||||
# @!macro thread_pool_executor_method_active_count
|
||||
def active_count
|
||||
synchronize do
|
||||
@pool.length - @ready.length
|
||||
end
|
||||
end
|
||||
|
||||
# @!macro executor_service_method_can_overflow_question
|
||||
def can_overflow?
|
||||
synchronize { ns_limited_queue? }
|
||||
end
|
||||
|
||||
# @!macro thread_pool_executor_attr_reader_length
|
||||
def length
|
||||
synchronize { @pool.length }
|
||||
end
|
||||
|
||||
# @!macro thread_pool_executor_attr_reader_queue_length
|
||||
def queue_length
|
||||
synchronize { @queue.length }
|
||||
end
|
||||
|
||||
# @!macro thread_pool_executor_attr_reader_remaining_capacity
|
||||
def remaining_capacity
|
||||
synchronize do
|
||||
if ns_limited_queue?
|
||||
@max_queue - @queue.length
|
||||
else
|
||||
-1
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
# removes the worker if it can be pruned
|
||||
#
|
||||
# @return [true, false] if the worker was pruned
|
||||
#
|
||||
# @!visibility private
|
||||
def prune_worker(worker)
|
||||
synchronize do
|
||||
if ns_prunable_capacity > 0
|
||||
remove_worker worker
|
||||
true
|
||||
else
|
||||
false
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
# @!visibility private
|
||||
def remove_worker(worker)
|
||||
synchronize do
|
||||
ns_remove_ready_worker worker
|
||||
ns_remove_busy_worker worker
|
||||
end
|
||||
end
|
||||
|
||||
# @!visibility private
|
||||
def ready_worker(worker, last_message)
|
||||
synchronize { ns_ready_worker worker, last_message }
|
||||
end
|
||||
|
||||
# @!visibility private
|
||||
def worker_died(worker)
|
||||
synchronize { ns_worker_died worker }
|
||||
end
|
||||
|
||||
# @!visibility private
|
||||
def worker_task_completed
|
||||
synchronize { @completed_task_count += 1 }
|
||||
end
|
||||
|
||||
# @!macro thread_pool_executor_method_prune_pool
|
||||
def prune_pool
|
||||
deprecated "#prune_pool has no effect and will be removed in next the release, see https://github.com/ruby-concurrency/concurrent-ruby/pull/1082."
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
# @!visibility private
|
||||
def ns_initialize(opts)
|
||||
@min_length = opts.fetch(:min_threads, DEFAULT_MIN_POOL_SIZE).to_i
|
||||
@max_length = opts.fetch(:max_threads, DEFAULT_MAX_POOL_SIZE).to_i
|
||||
@idletime = opts.fetch(:idletime, DEFAULT_THREAD_IDLETIMEOUT).to_i
|
||||
@max_queue = opts.fetch(:max_queue, DEFAULT_MAX_QUEUE_SIZE).to_i
|
||||
@synchronous = opts.fetch(:synchronous, DEFAULT_SYNCHRONOUS)
|
||||
@fallback_policy = opts.fetch(:fallback_policy, :abort)
|
||||
|
||||
raise ArgumentError.new("`synchronous` cannot be set unless `max_queue` is 0") if @synchronous && @max_queue > 0
|
||||
raise ArgumentError.new("#{@fallback_policy} is not a valid fallback policy") unless FALLBACK_POLICIES.include?(@fallback_policy)
|
||||
raise ArgumentError.new("`max_threads` cannot be less than #{DEFAULT_MIN_POOL_SIZE}") if @max_length < DEFAULT_MIN_POOL_SIZE
|
||||
raise ArgumentError.new("`max_threads` cannot be greater than #{DEFAULT_MAX_POOL_SIZE}") if @max_length > DEFAULT_MAX_POOL_SIZE
|
||||
raise ArgumentError.new("`min_threads` cannot be less than #{DEFAULT_MIN_POOL_SIZE}") if @min_length < DEFAULT_MIN_POOL_SIZE
|
||||
raise ArgumentError.new("`min_threads` cannot be more than `max_threads`") if min_length > max_length
|
||||
|
||||
@pool = [] # all workers
|
||||
@ready = [] # used as a stash (most idle worker is at the start)
|
||||
@queue = [] # used as queue
|
||||
# @ready or @queue is empty at all times
|
||||
@scheduled_task_count = 0
|
||||
@completed_task_count = 0
|
||||
@largest_length = 0
|
||||
@workers_counter = 0
|
||||
@ruby_pid = $$ # detects if Ruby has forked
|
||||
end
|
||||
|
||||
# @!visibility private
|
||||
def ns_limited_queue?
|
||||
@max_queue != 0
|
||||
end
|
||||
|
||||
# @!visibility private
|
||||
def ns_execute(*args, &task)
|
||||
ns_reset_if_forked
|
||||
|
||||
if ns_assign_worker(*args, &task) || ns_enqueue(*args, &task)
|
||||
@scheduled_task_count += 1
|
||||
nil
|
||||
else
|
||||
fallback_action(*args, &task)
|
||||
end
|
||||
end
|
||||
|
||||
# @!visibility private
|
||||
def ns_shutdown_execution
|
||||
ns_reset_if_forked
|
||||
|
||||
if @pool.empty?
|
||||
# nothing to do
|
||||
stopped_event.set
|
||||
end
|
||||
|
||||
if @queue.empty?
|
||||
# no more tasks will be accepted, just stop all workers
|
||||
@pool.each(&:stop)
|
||||
end
|
||||
end
|
||||
|
||||
# @!visibility private
|
||||
def ns_kill_execution
|
||||
# TODO log out unprocessed tasks in queue
|
||||
# TODO try to shutdown first?
|
||||
@pool.each(&:kill)
|
||||
@pool.clear
|
||||
@ready.clear
|
||||
end
|
||||
|
||||
# tries to assign task to a worker, tries to get one from @ready or to create new one
|
||||
# @return [true, false] if task is assigned to a worker
|
||||
#
|
||||
# @!visibility private
|
||||
def ns_assign_worker(*args, &task)
|
||||
# keep growing if the pool is not at the minimum yet
|
||||
worker, _ = (@ready.pop if @pool.size >= @min_length) || ns_add_busy_worker
|
||||
if worker
|
||||
worker << [task, args]
|
||||
true
|
||||
else
|
||||
false
|
||||
end
|
||||
rescue ThreadError
|
||||
# Raised when the operating system refuses to create the new thread
|
||||
return false
|
||||
end
|
||||
|
||||
# tries to enqueue task
|
||||
# @return [true, false] if enqueued
|
||||
#
|
||||
# @!visibility private
|
||||
def ns_enqueue(*args, &task)
|
||||
return false if @synchronous
|
||||
|
||||
if !ns_limited_queue? || @queue.size < @max_queue
|
||||
@queue << [task, args]
|
||||
true
|
||||
else
|
||||
false
|
||||
end
|
||||
end
|
||||
|
||||
# @!visibility private
|
||||
def ns_worker_died(worker)
|
||||
ns_remove_busy_worker worker
|
||||
replacement_worker = ns_add_busy_worker
|
||||
ns_ready_worker replacement_worker, Concurrent.monotonic_time, false if replacement_worker
|
||||
end
|
||||
|
||||
# creates new worker which has to receive work to do after it's added
|
||||
# @return [nil, Worker] nil of max capacity is reached
|
||||
#
|
||||
# @!visibility private
|
||||
def ns_add_busy_worker
|
||||
return if @pool.size >= @max_length
|
||||
|
||||
@workers_counter += 1
|
||||
@pool << (worker = Worker.new(self, @workers_counter))
|
||||
@largest_length = @pool.length if @pool.length > @largest_length
|
||||
worker
|
||||
end
|
||||
|
||||
# handle ready worker, giving it new job or assigning back to @ready
|
||||
#
|
||||
# @!visibility private
|
||||
def ns_ready_worker(worker, last_message, success = true)
|
||||
task_and_args = @queue.shift
|
||||
if task_and_args
|
||||
worker << task_and_args
|
||||
else
|
||||
# stop workers when !running?, do not return them to @ready
|
||||
if running?
|
||||
raise unless last_message
|
||||
@ready.push([worker, last_message])
|
||||
else
|
||||
worker.stop
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
# removes a worker which is not tracked in @ready
|
||||
#
|
||||
# @!visibility private
|
||||
def ns_remove_busy_worker(worker)
|
||||
@pool.delete(worker)
|
||||
stopped_event.set if @pool.empty? && !running?
|
||||
true
|
||||
end
|
||||
|
||||
# @!visibility private
|
||||
def ns_remove_ready_worker(worker)
|
||||
if index = @ready.index { |rw, _| rw == worker }
|
||||
@ready.delete_at(index)
|
||||
end
|
||||
true
|
||||
end
|
||||
|
||||
# @return [Integer] number of excess idle workers which can be removed without
|
||||
# going below min_length, or all workers if not running
|
||||
#
|
||||
# @!visibility private
|
||||
def ns_prunable_capacity
|
||||
if running?
|
||||
[@pool.size - @min_length, @ready.size].min
|
||||
else
|
||||
@pool.size
|
||||
end
|
||||
end
|
||||
|
||||
# @!visibility private
|
||||
def ns_reset_if_forked
|
||||
if $$ != @ruby_pid
|
||||
@queue.clear
|
||||
@ready.clear
|
||||
@pool.clear
|
||||
@scheduled_task_count = 0
|
||||
@completed_task_count = 0
|
||||
@largest_length = 0
|
||||
@workers_counter = 0
|
||||
@ruby_pid = $$
|
||||
end
|
||||
end
|
||||
|
||||
# @!visibility private
|
||||
class Worker
|
||||
include Concern::Logging
|
||||
|
||||
def initialize(pool, id)
|
||||
# instance variables accessed only under pool's lock so no need to sync here again
|
||||
@queue = Collection::TimeoutQueue.new
|
||||
@pool = pool
|
||||
@thread = create_worker @queue, pool, pool.idletime
|
||||
|
||||
if @thread.respond_to?(:name=)
|
||||
@thread.name = [pool.name, 'worker', id].compact.join('-')
|
||||
end
|
||||
end
|
||||
|
||||
def <<(message)
|
||||
@queue << message
|
||||
end
|
||||
|
||||
def stop
|
||||
@queue << :stop
|
||||
end
|
||||
|
||||
def kill
|
||||
@thread.kill
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def create_worker(queue, pool, idletime)
|
||||
Thread.new(queue, pool, idletime) do |my_queue, my_pool, my_idletime|
|
||||
catch(:stop) do
|
||||
prunable = true
|
||||
|
||||
loop do
|
||||
timeout = prunable && my_pool.running? ? my_idletime : nil
|
||||
case message = my_queue.pop(timeout: timeout)
|
||||
when nil
|
||||
throw :stop if my_pool.prune_worker(self)
|
||||
prunable = false
|
||||
when :stop
|
||||
my_pool.remove_worker(self)
|
||||
throw :stop
|
||||
else
|
||||
task, args = message
|
||||
run_task my_pool, task, args
|
||||
my_pool.ready_worker(self, Concurrent.monotonic_time)
|
||||
prunable = true
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def run_task(pool, task, args)
|
||||
task.call(*args)
|
||||
pool.worker_task_completed
|
||||
rescue => ex
|
||||
# let it fail
|
||||
log DEBUG, ex
|
||||
rescue Exception => ex
|
||||
log ERROR, ex
|
||||
pool.worker_died(self)
|
||||
throw :stop
|
||||
end
|
||||
end
|
||||
|
||||
private_constant :Worker
|
||||
end
|
||||
end
|
||||
+35
@@ -0,0 +1,35 @@
|
||||
require 'concurrent/synchronization/lockable_object'
|
||||
|
||||
module Concurrent
|
||||
|
||||
# A simple utility class that executes a callable and returns and array of three elements:
|
||||
# success - indicating if the callable has been executed without errors
|
||||
# value - filled by the callable result if it has been executed without errors, nil otherwise
|
||||
# reason - the error risen by the callable if it has been executed with errors, nil otherwise
|
||||
class SafeTaskExecutor < Synchronization::LockableObject
|
||||
|
||||
def initialize(task, opts = {})
|
||||
@task = task
|
||||
@exception_class = opts.fetch(:rescue_exception, false) ? Exception : StandardError
|
||||
super() # ensures visibility
|
||||
end
|
||||
|
||||
# @return [Array]
|
||||
def execute(*args)
|
||||
success = true
|
||||
value = reason = nil
|
||||
|
||||
synchronize do
|
||||
begin
|
||||
value = @task.call(*args)
|
||||
success = true
|
||||
rescue @exception_class => ex
|
||||
reason = ex
|
||||
success = false
|
||||
end
|
||||
end
|
||||
|
||||
[success, value, reason]
|
||||
end
|
||||
end
|
||||
end
|
||||
+34
@@ -0,0 +1,34 @@
|
||||
require 'concurrent/executor/executor_service'
|
||||
|
||||
module Concurrent
|
||||
|
||||
# Indicates that the including `ExecutorService` guarantees
|
||||
# that all operations will occur in the order they are post and that no
|
||||
# two operations may occur simultaneously. This module provides no
|
||||
# functionality and provides no guarantees. That is the responsibility
|
||||
# of the including class. This module exists solely to allow the including
|
||||
# object to be interrogated for its serialization status.
|
||||
#
|
||||
# @example
|
||||
# class Foo
|
||||
# include Concurrent::SerialExecutor
|
||||
# end
|
||||
#
|
||||
# foo = Foo.new
|
||||
#
|
||||
# foo.is_a? Concurrent::ExecutorService #=> true
|
||||
# foo.is_a? Concurrent::SerialExecutor #=> true
|
||||
# foo.serialized? #=> true
|
||||
#
|
||||
# @!visibility private
|
||||
module SerialExecutorService
|
||||
include ExecutorService
|
||||
|
||||
# @!macro executor_service_method_serialized_question
|
||||
#
|
||||
# @note Always returns `true`
|
||||
def serialized?
|
||||
true
|
||||
end
|
||||
end
|
||||
end
|
||||
+107
@@ -0,0 +1,107 @@
|
||||
require 'concurrent/errors'
|
||||
require 'concurrent/concern/logging'
|
||||
require 'concurrent/synchronization/lockable_object'
|
||||
|
||||
module Concurrent
|
||||
|
||||
# Ensures passed jobs in a serialized order never running at the same time.
|
||||
class SerializedExecution < Synchronization::LockableObject
|
||||
include Concern::Logging
|
||||
|
||||
def initialize()
|
||||
super()
|
||||
synchronize { ns_initialize }
|
||||
end
|
||||
|
||||
Job = Struct.new(:executor, :args, :block) do
|
||||
def call
|
||||
block.call(*args)
|
||||
end
|
||||
end
|
||||
|
||||
# Submit a task to the executor for asynchronous processing.
|
||||
#
|
||||
# @param [Executor] executor to be used for this job
|
||||
#
|
||||
# @param [Array] args zero or more arguments to be passed to the task
|
||||
#
|
||||
# @yield the asynchronous task to perform
|
||||
#
|
||||
# @return [Boolean] `true` if the task is queued, `false` if the executor
|
||||
# is not running
|
||||
#
|
||||
# @raise [ArgumentError] if no task is given
|
||||
def post(executor, *args, &task)
|
||||
posts [[executor, args, task]]
|
||||
true
|
||||
end
|
||||
|
||||
# As {#post} but allows to submit multiple tasks at once, it's guaranteed that they will not
|
||||
# be interleaved by other tasks.
|
||||
#
|
||||
# @param [Array<Array(ExecutorService, Array<Object>, Proc)>] posts array of triplets where
|
||||
# first is a {ExecutorService}, second is array of args for task, third is a task (Proc)
|
||||
def posts(posts)
|
||||
# if can_overflow?
|
||||
# raise ArgumentError, 'SerializedExecution does not support thread-pools which can overflow'
|
||||
# end
|
||||
|
||||
return nil if posts.empty?
|
||||
|
||||
jobs = posts.map { |executor, args, task| Job.new executor, args, task }
|
||||
|
||||
job_to_post = synchronize do
|
||||
if @being_executed
|
||||
@stash.push(*jobs)
|
||||
nil
|
||||
else
|
||||
@being_executed = true
|
||||
@stash.push(*jobs[1..-1])
|
||||
jobs.first
|
||||
end
|
||||
end
|
||||
|
||||
call_job job_to_post if job_to_post
|
||||
true
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def ns_initialize
|
||||
@being_executed = false
|
||||
@stash = []
|
||||
end
|
||||
|
||||
def call_job(job)
|
||||
did_it_run = begin
|
||||
job.executor.post { work(job) }
|
||||
true
|
||||
rescue RejectedExecutionError => ex
|
||||
false
|
||||
end
|
||||
|
||||
# TODO not the best idea to run it myself
|
||||
unless did_it_run
|
||||
begin
|
||||
work job
|
||||
rescue => ex
|
||||
# let it fail
|
||||
log DEBUG, ex
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
# ensures next job is executed if any is stashed
|
||||
def work(job)
|
||||
job.call
|
||||
ensure
|
||||
synchronize do
|
||||
job = @stash.shift || (@being_executed = false)
|
||||
end
|
||||
|
||||
# TODO maybe be able to tell caching pool to just enqueue this job, because the current one end at the end
|
||||
# of this block
|
||||
call_job job if job
|
||||
end
|
||||
end
|
||||
end
|
||||
+28
@@ -0,0 +1,28 @@
|
||||
require 'delegate'
|
||||
require 'concurrent/executor/serial_executor_service'
|
||||
require 'concurrent/executor/serialized_execution'
|
||||
|
||||
module Concurrent
|
||||
|
||||
# A wrapper/delegator for any `ExecutorService` that
|
||||
# guarantees serialized execution of tasks.
|
||||
#
|
||||
# @see [SimpleDelegator](http://www.ruby-doc.org/stdlib-2.1.2/libdoc/delegate/rdoc/SimpleDelegator.html)
|
||||
# @see Concurrent::SerializedExecution
|
||||
class SerializedExecutionDelegator < SimpleDelegator
|
||||
include SerialExecutorService
|
||||
|
||||
def initialize(executor)
|
||||
@executor = executor
|
||||
@serializer = SerializedExecution.new
|
||||
super(executor)
|
||||
end
|
||||
|
||||
# @!macro executor_service_method_post
|
||||
def post(*args, &task)
|
||||
raise ArgumentError.new('no block given') unless block_given?
|
||||
return false unless running?
|
||||
@serializer.post(@executor, *args, &task)
|
||||
end
|
||||
end
|
||||
end
|
||||
+103
@@ -0,0 +1,103 @@
|
||||
require 'concurrent/atomic/atomic_boolean'
|
||||
require 'concurrent/atomic/atomic_fixnum'
|
||||
require 'concurrent/atomic/event'
|
||||
require 'concurrent/executor/executor_service'
|
||||
require 'concurrent/executor/ruby_executor_service'
|
||||
|
||||
module Concurrent
|
||||
|
||||
# An executor service in which every operation spawns a new,
|
||||
# independently operating thread.
|
||||
#
|
||||
# This is perhaps the most inefficient executor service in this
|
||||
# library. It exists mainly for testing an debugging. Thread creation
|
||||
# and management is expensive in Ruby and this executor performs no
|
||||
# resource pooling. This can be very beneficial during testing and
|
||||
# debugging because it decouples the using code from the underlying
|
||||
# executor implementation. In production this executor will likely
|
||||
# lead to suboptimal performance.
|
||||
#
|
||||
# @note Intended for use primarily in testing and debugging.
|
||||
class SimpleExecutorService < RubyExecutorService
|
||||
|
||||
# @!macro executor_service_method_post
|
||||
def self.post(*args)
|
||||
raise ArgumentError.new('no block given') unless block_given?
|
||||
Thread.new(*args) do
|
||||
Thread.current.abort_on_exception = false
|
||||
yield(*args)
|
||||
end
|
||||
true
|
||||
end
|
||||
|
||||
# @!macro executor_service_method_left_shift
|
||||
def self.<<(task)
|
||||
post(&task)
|
||||
self
|
||||
end
|
||||
|
||||
# @!macro executor_service_method_post
|
||||
def post(*args, &task)
|
||||
raise ArgumentError.new('no block given') unless block_given?
|
||||
return false unless running?
|
||||
@count.increment
|
||||
Thread.new(*args) do
|
||||
Thread.current.abort_on_exception = false
|
||||
begin
|
||||
yield(*args)
|
||||
ensure
|
||||
@count.decrement
|
||||
@stopped.set if @running.false? && @count.value == 0
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
# @!macro executor_service_method_left_shift
|
||||
def <<(task)
|
||||
post(&task)
|
||||
self
|
||||
end
|
||||
|
||||
# @!macro executor_service_method_running_question
|
||||
def running?
|
||||
@running.true?
|
||||
end
|
||||
|
||||
# @!macro executor_service_method_shuttingdown_question
|
||||
def shuttingdown?
|
||||
@running.false? && ! @stopped.set?
|
||||
end
|
||||
|
||||
# @!macro executor_service_method_shutdown_question
|
||||
def shutdown?
|
||||
@stopped.set?
|
||||
end
|
||||
|
||||
# @!macro executor_service_method_shutdown
|
||||
def shutdown
|
||||
@running.make_false
|
||||
@stopped.set if @count.value == 0
|
||||
true
|
||||
end
|
||||
|
||||
# @!macro executor_service_method_kill
|
||||
def kill
|
||||
@running.make_false
|
||||
@stopped.set
|
||||
true
|
||||
end
|
||||
|
||||
# @!macro executor_service_method_wait_for_termination
|
||||
def wait_for_termination(timeout = nil)
|
||||
@stopped.wait(timeout)
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def ns_initialize(*args)
|
||||
@running = Concurrent::AtomicBoolean.new(true)
|
||||
@stopped = Concurrent::Event.new
|
||||
@count = Concurrent::AtomicFixnum.new(0)
|
||||
end
|
||||
end
|
||||
end
|
||||
+57
@@ -0,0 +1,57 @@
|
||||
require 'concurrent/utility/engine'
|
||||
require 'concurrent/executor/ruby_single_thread_executor'
|
||||
|
||||
module Concurrent
|
||||
|
||||
if Concurrent.on_jruby?
|
||||
require 'concurrent/executor/java_single_thread_executor'
|
||||
end
|
||||
|
||||
SingleThreadExecutorImplementation = case
|
||||
when Concurrent.on_jruby?
|
||||
JavaSingleThreadExecutor
|
||||
else
|
||||
RubySingleThreadExecutor
|
||||
end
|
||||
private_constant :SingleThreadExecutorImplementation
|
||||
|
||||
# @!macro single_thread_executor
|
||||
#
|
||||
# A thread pool with a single thread an unlimited queue. Should the thread
|
||||
# die for any reason it will be removed and replaced, thus ensuring that
|
||||
# the executor will always remain viable and available to process jobs.
|
||||
#
|
||||
# A common pattern for background processing is to create a single thread
|
||||
# on which an infinite loop is run. The thread's loop blocks on an input
|
||||
# source (perhaps blocking I/O or a queue) and processes each input as it
|
||||
# is received. This pattern has several issues. The thread itself is highly
|
||||
# susceptible to errors during processing. Also, the thread itself must be
|
||||
# constantly monitored and restarted should it die. `SingleThreadExecutor`
|
||||
# encapsulates all these behaviors. The task processor is highly resilient
|
||||
# to errors from within tasks. Also, should the thread die it will
|
||||
# automatically be restarted.
|
||||
#
|
||||
# The API and behavior of this class are based on Java's `SingleThreadExecutor`.
|
||||
#
|
||||
# @!macro abstract_executor_service_public_api
|
||||
class SingleThreadExecutor < SingleThreadExecutorImplementation
|
||||
|
||||
# @!macro single_thread_executor_method_initialize
|
||||
#
|
||||
# Create a new thread pool.
|
||||
#
|
||||
# @option opts [Symbol] :fallback_policy (:discard) the policy for handling new
|
||||
# tasks that are received when the queue size has reached
|
||||
# `max_queue` or the executor has shut down
|
||||
#
|
||||
# @raise [ArgumentError] if `:fallback_policy` is not one of the values specified
|
||||
# in `FALLBACK_POLICIES`
|
||||
#
|
||||
# @see http://docs.oracle.com/javase/tutorial/essential/concurrency/pools.html
|
||||
# @see http://docs.oracle.com/javase/7/docs/api/java/util/concurrent/Executors.html
|
||||
# @see http://docs.oracle.com/javase/8/docs/api/java/util/concurrent/ExecutorService.html
|
||||
|
||||
# @!method initialize(opts = {})
|
||||
# @!macro single_thread_executor_method_initialize
|
||||
end
|
||||
end
|
||||
+88
@@ -0,0 +1,88 @@
|
||||
require 'concurrent/utility/engine'
|
||||
require 'concurrent/executor/ruby_thread_pool_executor'
|
||||
|
||||
module Concurrent
|
||||
|
||||
if Concurrent.on_jruby?
|
||||
require 'concurrent/executor/java_thread_pool_executor'
|
||||
end
|
||||
|
||||
ThreadPoolExecutorImplementation = case
|
||||
when Concurrent.on_jruby?
|
||||
JavaThreadPoolExecutor
|
||||
else
|
||||
RubyThreadPoolExecutor
|
||||
end
|
||||
private_constant :ThreadPoolExecutorImplementation
|
||||
|
||||
# @!macro thread_pool_executor
|
||||
#
|
||||
# An abstraction composed of one or more threads and a task queue. Tasks
|
||||
# (blocks or `proc` objects) are submitted to the pool and added to the queue.
|
||||
# The threads in the pool remove the tasks and execute them in the order
|
||||
# they were received.
|
||||
#
|
||||
# A `ThreadPoolExecutor` will automatically adjust the pool size according
|
||||
# to the bounds set by `min-threads` and `max-threads`. When a new task is
|
||||
# submitted and fewer than `min-threads` threads are running, a new thread
|
||||
# is created to handle the request, even if other worker threads are idle.
|
||||
# If there are more than `min-threads` but less than `max-threads` threads
|
||||
# running, a new thread will be created only if the queue is full.
|
||||
#
|
||||
# Threads that are idle for too long will be garbage collected, down to the
|
||||
# configured minimum options. Should a thread crash it, too, will be garbage collected.
|
||||
#
|
||||
# `ThreadPoolExecutor` is based on the Java class of the same name. From
|
||||
# the official Java documentation;
|
||||
#
|
||||
# > Thread pools address two different problems: they usually provide
|
||||
# > improved performance when executing large numbers of asynchronous tasks,
|
||||
# > due to reduced per-task invocation overhead, and they provide a means
|
||||
# > of bounding and managing the resources, including threads, consumed
|
||||
# > when executing a collection of tasks. Each ThreadPoolExecutor also
|
||||
# > maintains some basic statistics, such as the number of completed tasks.
|
||||
# >
|
||||
# > To be useful across a wide range of contexts, this class provides many
|
||||
# > adjustable parameters and extensibility hooks. However, programmers are
|
||||
# > urged to use the more convenient Executors factory methods
|
||||
# > [CachedThreadPool] (unbounded thread pool, with automatic thread reclamation),
|
||||
# > [FixedThreadPool] (fixed size thread pool) and [SingleThreadExecutor] (single
|
||||
# > background thread), that preconfigure settings for the most common usage
|
||||
# > scenarios.
|
||||
#
|
||||
# @!macro thread_pool_options
|
||||
#
|
||||
# @!macro thread_pool_executor_public_api
|
||||
class ThreadPoolExecutor < ThreadPoolExecutorImplementation
|
||||
|
||||
# @!macro thread_pool_executor_method_initialize
|
||||
#
|
||||
# Create a new thread pool.
|
||||
#
|
||||
# @param [Hash] opts the options which configure the thread pool.
|
||||
#
|
||||
# @option opts [Integer] :max_threads (DEFAULT_MAX_POOL_SIZE) the maximum
|
||||
# number of threads to be created
|
||||
# @option opts [Integer] :min_threads (DEFAULT_MIN_POOL_SIZE) When a new task is submitted
|
||||
# and fewer than `min_threads` are running, a new thread is created
|
||||
# @option opts [Integer] :idletime (DEFAULT_THREAD_IDLETIMEOUT) the maximum
|
||||
# number of seconds a thread may be idle before being reclaimed
|
||||
# @option opts [Integer] :max_queue (DEFAULT_MAX_QUEUE_SIZE) the maximum
|
||||
# number of tasks allowed in the work queue at any one time; a value of
|
||||
# zero means the queue may grow without bound
|
||||
# @option opts [Symbol] :fallback_policy (:abort) the policy for handling new
|
||||
# tasks that are received when the queue size has reached
|
||||
# `max_queue` or the executor has shut down
|
||||
# @option opts [Boolean] :synchronous (DEFAULT_SYNCHRONOUS) whether or not a value of 0
|
||||
# for :max_queue means the queue must perform direct hand-off rather than unbounded.
|
||||
# @raise [ArgumentError] if `:max_threads` is less than one
|
||||
# @raise [ArgumentError] if `:min_threads` is less than zero
|
||||
# @raise [ArgumentError] if `:fallback_policy` is not one of the values specified
|
||||
# in `FALLBACK_POLICIES`
|
||||
#
|
||||
# @see http://docs.oracle.com/javase/7/docs/api/java/util/concurrent/ThreadPoolExecutor.html
|
||||
|
||||
# @!method initialize(opts = {})
|
||||
# @!macro thread_pool_executor_method_initialize
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,179 @@
|
||||
require 'concurrent/scheduled_task'
|
||||
require 'concurrent/atomic/event'
|
||||
require 'concurrent/collection/non_concurrent_priority_queue'
|
||||
require 'concurrent/executor/executor_service'
|
||||
require 'concurrent/executor/single_thread_executor'
|
||||
require 'concurrent/errors'
|
||||
require 'concurrent/options'
|
||||
|
||||
module Concurrent
|
||||
|
||||
# Executes a collection of tasks, each after a given delay. A master task
|
||||
# monitors the set and schedules each task for execution at the appropriate
|
||||
# time. Tasks are run on the global thread pool or on the supplied executor.
|
||||
# Each task is represented as a `ScheduledTask`.
|
||||
#
|
||||
# @see Concurrent::ScheduledTask
|
||||
#
|
||||
# @!macro monotonic_clock_warning
|
||||
class TimerSet < RubyExecutorService
|
||||
|
||||
# Create a new set of timed tasks.
|
||||
#
|
||||
# @!macro executor_options
|
||||
#
|
||||
# @param [Hash] opts the options used to specify the executor on which to perform actions
|
||||
# @option opts [Executor] :executor when set use the given `Executor` instance.
|
||||
# Three special values are also supported: `:task` returns the global task pool,
|
||||
# `:operation` returns the global operation pool, and `:immediate` returns a new
|
||||
# `ImmediateExecutor` object.
|
||||
def initialize(opts = {})
|
||||
super(opts)
|
||||
end
|
||||
|
||||
# Post a task to be execute run after a given delay (in seconds). If the
|
||||
# delay is less than 1/100th of a second the task will be immediately post
|
||||
# to the executor.
|
||||
#
|
||||
# @param [Float] delay the number of seconds to wait for before executing the task.
|
||||
# @param [Array<Object>] args the arguments passed to the task on execution.
|
||||
#
|
||||
# @yield the task to be performed.
|
||||
#
|
||||
# @return [Concurrent::ScheduledTask, false] IVar representing the task if the post
|
||||
# is successful; false after shutdown.
|
||||
#
|
||||
# @raise [ArgumentError] if the intended execution time is not in the future.
|
||||
# @raise [ArgumentError] if no block is given.
|
||||
def post(delay, *args, &task)
|
||||
raise ArgumentError.new('no block given') unless block_given?
|
||||
return false unless running?
|
||||
opts = { executor: @task_executor,
|
||||
args: args,
|
||||
timer_set: self }
|
||||
task = ScheduledTask.execute(delay, opts, &task) # may raise exception
|
||||
task.unscheduled? ? false : task
|
||||
end
|
||||
|
||||
# Begin an immediate shutdown. In-progress tasks will be allowed to
|
||||
# complete but enqueued tasks will be dismissed and no new tasks
|
||||
# will be accepted. Has no additional effect if the thread pool is
|
||||
# not running.
|
||||
def kill
|
||||
shutdown
|
||||
@timer_executor.kill
|
||||
end
|
||||
|
||||
private :<<
|
||||
|
||||
private
|
||||
|
||||
# Initialize the object.
|
||||
#
|
||||
# @param [Hash] opts the options to create the object with.
|
||||
# @!visibility private
|
||||
def ns_initialize(opts)
|
||||
@queue = Collection::NonConcurrentPriorityQueue.new(order: :min)
|
||||
@task_executor = Options.executor_from_options(opts) || Concurrent.global_io_executor
|
||||
@timer_executor = SingleThreadExecutor.new
|
||||
@condition = Event.new
|
||||
@ruby_pid = $$ # detects if Ruby has forked
|
||||
end
|
||||
|
||||
# Post the task to the internal queue.
|
||||
#
|
||||
# @note This is intended as a callback method from ScheduledTask
|
||||
# only. It is not intended to be used directly. Post a task
|
||||
# by using the `SchedulesTask#execute` method.
|
||||
#
|
||||
# @!visibility private
|
||||
def post_task(task)
|
||||
synchronize { ns_post_task(task) }
|
||||
end
|
||||
|
||||
# @!visibility private
|
||||
def ns_post_task(task)
|
||||
return false unless ns_running?
|
||||
ns_reset_if_forked
|
||||
if (task.initial_delay) <= 0.01
|
||||
task.executor.post { task.process_task }
|
||||
else
|
||||
@queue.push(task)
|
||||
# only post the process method when the queue is empty
|
||||
@timer_executor.post(&method(:process_tasks)) if @queue.length == 1
|
||||
@condition.set
|
||||
end
|
||||
true
|
||||
end
|
||||
|
||||
# Remove the given task from the queue.
|
||||
#
|
||||
# @note This is intended as a callback method from `ScheduledTask`
|
||||
# only. It is not intended to be used directly. Cancel a task
|
||||
# by using the `ScheduledTask#cancel` method.
|
||||
#
|
||||
# @!visibility private
|
||||
def remove_task(task)
|
||||
synchronize { @queue.delete(task) }
|
||||
end
|
||||
|
||||
# `ExecutorService` callback called during shutdown.
|
||||
#
|
||||
# @!visibility private
|
||||
def ns_shutdown_execution
|
||||
ns_reset_if_forked
|
||||
@queue.clear
|
||||
@condition.set
|
||||
@condition.reset
|
||||
@timer_executor.shutdown
|
||||
stopped_event.set
|
||||
end
|
||||
|
||||
def ns_reset_if_forked
|
||||
if $$ != @ruby_pid
|
||||
@queue.clear
|
||||
@condition.reset
|
||||
@ruby_pid = $$
|
||||
end
|
||||
end
|
||||
|
||||
# Run a loop and execute tasks in the scheduled order and at the approximate
|
||||
# scheduled time. If no tasks remain the thread will exit gracefully so that
|
||||
# garbage collection can occur. If there are no ready tasks it will sleep
|
||||
# for up to 60 seconds waiting for the next scheduled task.
|
||||
#
|
||||
# @!visibility private
|
||||
def process_tasks
|
||||
loop do
|
||||
task = synchronize { @condition.reset; @queue.peek }
|
||||
break unless task
|
||||
|
||||
now = Concurrent.monotonic_time
|
||||
diff = task.schedule_time - now
|
||||
|
||||
if diff <= 0
|
||||
# We need to remove the task from the queue before passing
|
||||
# it to the executor, to avoid race conditions where we pass
|
||||
# the peek'ed task to the executor and then pop a different
|
||||
# one that's been added in the meantime.
|
||||
#
|
||||
# Note that there's no race condition between the peek and
|
||||
# this pop - this pop could retrieve a different task from
|
||||
# the peek, but that task would be due to fire now anyway
|
||||
# (because @queue is a priority queue, and this thread is
|
||||
# the only reader, so whatever timer is at the head of the
|
||||
# queue now must have the same pop time, or a closer one, as
|
||||
# when we peeked).
|
||||
task = synchronize { @queue.pop }
|
||||
begin
|
||||
task.executor.post { task.process_task }
|
||||
rescue RejectedExecutionError
|
||||
# ignore and continue
|
||||
end
|
||||
else
|
||||
@condition.wait([diff, 60].min)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user