Sha256: 5f73dddc4ee749cd517044f405db697ce15ed0579d87316e4c0085f6fc48b3fc

Contents?: true

Size: 907 Bytes

Versions: 4

Compression:

Stored size: 907 Bytes

Contents

require "thread"

module Stud
  class Task
    def initialize(*args, &block)
      # A queue to receive the result of the block
      # TODO(sissel): Don't use a queue, just store it in an instance variable.
      @queue = Queue.new

      @thread = Thread.new(@queue, *args) do |queue, *args|
        begin
          result = block.call(*args)
          queue << [:return, result]
        rescue => e
          queue << [:exception, e]
        end
      end # thread
    end # def initialize

    def wait
      @thread.join
      reason, result = @queue.pop

      if reason == :exception
        #raise StandardError.new(result)
        raise result
      else
        return result
      end
    end # def wait

    def stop!
      Thread.current[:stud_task_interrupted] = true
    end

    def self.interrupted?
      Thread.current[:stud_task_interrupted]
    end
  end # class Task
end # module Stud

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
stud-0.0.21 lib/stud/task.rb
stud-0.0.20 lib/stud/task.rb
stud-0.0.19 lib/stud/task.rb
stud-0.0.18 lib/stud/task.rb