module Concurrent::Executor

def <<(task)

Returns:
  • (self) - returns itself

Parameters:
  • task (Proc) -- the asynchronous task to perform
def <<(task)
  post(&task)
  self
end

def execute(*args, &task)

def execute(*args, &task)
  raise NotImplementedError
end

def init_executor

def init_executor
  @mutex = Mutex.new
  @stop_event = Event.new
  @stopped_event = Event.new
end

def kill

not running.
will be accepted. Has no additional effect if the thread pool is
complete but enqueued tasks will be dismissed and no new tasks
Begin an immediate shutdown. In-progress tasks will be allowed to
def kill
  mutex.synchronize do
    break if shutdown?
    stop_event.set
    kill_execution
    stopped_event.set
  end
  true
end

def kill_execution

def kill_execution
  # do nothing
end

def post(*args, &task)

Raises:
  • (ArgumentError) - if no task is given

Returns:
  • (Boolean) - `true` if the task is queued, `false` if the executor

Other tags:
    Yield: - the asynchronous task to perform

Parameters:
  • args (Array) -- zero or more arguments to be passed to the task
def post(*args, &task)
  raise ArgumentError.new('no block given') unless block_given?
  mutex.synchronize do
    return false unless running?
    execute(*args, &task)
    true
  end
end

def running?

Returns:
  • (Boolean) - `true` when running, `false` when shutting down or shutdown
def running?
  ! stop_event.set?
end

def shutdown

thread pool is not running.
but no new tasks will be accepted. Has no additional effect if the
Begin an orderly shutdown. Tasks already in the queue will be executed,
def shutdown
  mutex.synchronize do
    break unless running?
    stop_event.set
    shutdown_execution
  end
  true
end

def shutdown?

Returns:
  • (Boolean) - `true` when shutdown, `false` when shutting down or running
def shutdown?
  stopped_event.set?
end

def shutdown_execution

def shutdown_execution
  stopped_event.set
end

def shuttingdown?

Returns:
  • (Boolean) - `true` when not running and not shutdown, else `false`
def shuttingdown?
  ! (running? || shutdown?)
end

def wait_for_termination(timeout = nil)

Returns:
  • (Boolean) - `true` if shutdown complete or false on `timeout`

Parameters:
  • timeout (Integer) -- the maximum number of seconds to wait for shutdown to complete

Other tags:
    Note: - Does not initiate shutdown or termination. Either `shutdown` or `kill`
def wait_for_termination(timeout = nil)
  stopped_event.wait(timeout)
end