Class: Tpool::Block

Inherits:
Object
  • Object
show all
Defined in:
lib/tpool_block.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args) ⇒ Block

Constructor. Should not be called manually.



5
6
7
8
9
10
11
12
# File 'lib/tpool_block.rb', line 5

def initialize(args)
  @args = args
  
  @tpool = @args[:tpool]
  @running = false
  @done = false
  @error = nil
end

Instance Attribute Details

#resObject

Returns the value of attribute res.



2
3
4
# File 'lib/tpool_block.rb', line 2

def res
  @res
end

Instance Method Details

#done?Boolean

Returns true if the asynced job is done running.

Returns:

  • (Boolean)


41
42
43
# File 'lib/tpool_block.rb', line 41

def done?
  return @done
end

#error!Object

Raises error if one has happened in the asynced job.

Raises:

  • (@error)


57
58
59
60
61
62
63
# File 'lib/tpool_block.rb', line 57

def error!
  #Wait for error to get set if any.
  self.join
  
  #Raise it if it got set.
  raise @error if @error
end

#joinObject

Sleeps until the asynced job is done. If an error occurred in the job, that error will be raised when calling the method.



66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/tpool_block.rb', line 66

def join
  if !@done
    @args[:thread_starts] << Thread.current
    
    begin
      Thread.stop
    rescue Exception
      sleep 0.1 while !@done
    end
  end
  
  return self
end

#killObject

Kills the current running job.



81
82
83
84
# File 'lib/tpool_block.rb', line 81

def kill
  Thread.pass while !self.done? and !self.running?
  @thread_running.raise Exception, "Should kill itself." if !self.done? and self.running?
end

#result(args = nil) ⇒ Object



86
87
88
89
90
91
# File 'lib/tpool_block.rb', line 86

def result(args = nil)
  self.join if args and args[:wait]
  raise "Not done yet." unless self.done?
  self.error!
  return @res
end

#runObject

Starts running whatever block it is holding.



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/tpool_block.rb', line 15

def run
  @thread_running = Thread.current
  @thread_running.priority = @tpool.args[:priority] if @tpool.args.key?(:priority)
  @running = true
  
  begin
    @res = @args[:blk].call(*@args[:args], &@args[:blk])
  rescue Exception => e
    @error = e
    @args[:tpool].on_error_call(:error => e, :block => self)
  ensure
    @running = false
    @done = true
    @thread_running = nil
  end
  
  if @args[:thread_starts]
    @args[:thread_starts].each do |thread|
      thread.wakeup
    end
  end
  
  return self
end

#running?Boolean

Returns true if the asynced job is still running.

Returns:

  • (Boolean)


46
47
48
# File 'lib/tpool_block.rb', line 46

def running?
  return @running
end

#waiting?Boolean

Returns true if the asynced job is still waiting to run.

Returns:

  • (Boolean)


51
52
53
54
# File 'lib/tpool_block.rb', line 51

def waiting?
  return true if !@done and !@running and !@error
  return false
end