class Celluloid::TaskThread - celluloid-0.15.2 Documentation

# class Celluloid::TaskThread

Tasks with a Thread backend

### Public Class Methods

new(type, meta) click to toggle source

Run the given block within a task

Calls superclass method Celluloid::Task.new

    # File lib/celluloid/tasks/task_thread.rb, line 5
    def initialize(type, meta)
      @resume_queue = Queue.new
      @exception_queue = Queue.new
      @yield_mutex  = Mutex.new
      @yield_cond   = ConditionVariable.new

      super
    end

### Public Instance Methods

backtrace() click to toggle source

    # File lib/celluloid/tasks/task_thread.rb, line 48
    def backtrace
      @thread.backtrace
    end

create() { || ... } click to toggle source

    # File lib/celluloid/tasks/task_thread.rb, line 14
    def create
      @thread = Celluloid::ThreadHandle.new(:task) do
        begin
          ex = @resume_queue.pop
          raise ex if ex.is_a?(Task::TerminatedError)

          yield
        rescue Exception => ex
          @exception_queue << ex
        ensure
          @yield_cond.signal
        end
      end
    end

deliver(value) click to toggle source

    # File lib/celluloid/tasks/task_thread.rb, line 34
    def deliver(value)
      raise DeadTaskError, "cannot resume a dead task" unless @thread.alive?

      @yield_mutex.synchronize do
        @resume_queue.push(value)
        @yield_cond.wait(@yield_mutex)
        while @exception_queue.size > 0
          raise @exception_queue.pop
        end
      end
    rescue ThreadError
      raise DeadTaskError, "cannot resume a dead task"
    end

signal() click to toggle source

    # File lib/celluloid/tasks/task_thread.rb, line 29
    def signal
      @yield_cond.signal
      @resume_queue.pop
    end