class Thread::Task

Constants

VERSION

Public Class Methods

new( *args, pool: nil, count: nil, report_on_exception: false, &block ) click to toggle source
# File lib/thread/task/base.rb, line 32
def initialize( *args, pool: nil, count: nil, report_on_exception: false, &block )
  raise  ::ArgumentError, "block required."    if block.nil?

  if  pool.nil? and count.nil?
    @thread  =  ::Thread.start( args ) do |args_|
      ::Thread.current.report_on_exception  =  report_on_exception
      block.call( *args_ )
    end

  elsif  pool  and pool.is_a?(::Thread::Task::Pool)
    @thread  =  ::Thread.start( args ) do |args_|
      ::Thread.current.report_on_exception  =  report_on_exception
      pool.acquire
      begin
        block.call( *args_ )
      ensure
        pool.release
      end
    end

  elsif  count  and  count.is_a? ::Integer  and  count > 0
    key  =  caller[1]
    unless ( pool = @@pool[key] )
      pool  =  ::Thread::Task::Pool.new( count )
      @@pool[key]  =  pool
    end

    @thread  =  ::Thread.start( args ) do |args_|
      ::Thread.current.report_on_exception  =  report_on_exception
      pool.acquire
      begin
        block.call( *args_ )
      ensure
        pool.release
      end
    end

  else
    raise  ::ArgumentError, "Nil or size of Thread::Task::Pool object expected."

  end
end
once( *args, delay: nil, guard: nil, ident: nil, &block ) click to toggle source
# File lib/thread/task/base.rb, line 116
def Task.once( *args, delay: nil, guard: nil, ident: nil, &block )
  key  =  ( ident || caller[0] ).to_s
  if  delay  and  delay.is_a? ::Numeric  and  delay > 0
    @@counter[key].incr
    ::Thread::Task.new( key ) do |key_|
      ::Kernel.sleep  delay
      count  =  @@counter[key_].decr
      block.call( *args )    if count == 0
    end
  elsif  guard  and  guard.is_a? ::Numeric  and  guard > 0
    count  =  @@counter[key].incr
    block.call( *args )    if count == 1
    ::Thread::Task.new( key ) do |key_|
      ::Kernel.sleep  guard
      @@counter[key_].decr
    end
  else
    raise  ::ArgumentError, "delay or guard time expected."
  end
end

Public Instance Methods

cancel() click to toggle source
# File lib/thread/task/base.rb, line 83
def cancel
  @thread.kill
  nil
end
join() click to toggle source
# File lib/thread/task/base.rb, line 75
def join
  @thread.join
end
value() click to toggle source
# File lib/thread/task/base.rb, line 79
def value
  @thread.value
end