Class: Thread::Task

Inherits:
Object
  • Object
show all
Defined in:
lib/thread/task/base.rb,
lib/thread/task/version.rb

Defined Under Namespace

Classes: Counter, Pool

Constant Summary collapse

VERSION =
"2.0.0"
@@pool =
::Hash.new
@@counter =
::Hash.new{|h,k| h[k] = Counter.new }

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args, pool: nil, count: nil, report_on_exception: false, &block) ⇒ Task

Returns a new instance of Task.

Raises:

  • (::ArgumentError)


32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# 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

Class Method Details

.once(*args, delay: nil, guard: nil, ident: nil, &block) ⇒ Object



116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# 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

Instance Method Details

#cancelObject



83
84
85
86
# File 'lib/thread/task/base.rb', line 83

def cancel
  @thread.kill
  nil
end

#joinObject



75
76
77
# File 'lib/thread/task/base.rb', line 75

def join
  @thread.join
end

#valueObject



79
80
81
# File 'lib/thread/task/base.rb', line 79

def value
  @thread.value
end