class Tpool::Block

Attributes

res[RW]

Public Class Methods

new(args) click to toggle source

Constructor. Should not be called manually.

# File lib/tpool_block.rb, line 5
def initialize(args)
  @args = args
  
  @tpool = @args[:tpool]
  @running = false
  @done = false
  @error = nil
end

Public Instance Methods

done?() click to toggle source

Returns true if the asynced job is done running.

# File lib/tpool_block.rb, line 41
def done?
  return @done
end
error!() click to toggle source

Raises error if one has happened in the asynced job.

# 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
join() click to toggle source

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

# 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
kill() click to toggle source

Kills the current running job.

# 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) click to toggle source
# 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
run() click to toggle source

Starts running whatever block it is holding.

# 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 if thread.alive?
    end
  end
  
  return self
end
running?() click to toggle source

Returns true if the asynced job is still running.

# File lib/tpool_block.rb, line 46
def running?
  return @running
end
waiting?() click to toggle source

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

# File lib/tpool_block.rb, line 51
def waiting?
  return true if !@done and !@running and !@error
  return false
end