Sha256: ba67a0f6207257ae61b01cc94730ff7526fc47129b5058eeb7b2cf210437e50c

Contents?: true

Size: 1.14 KB

Versions: 6

Compression:

Stored size: 1.14 KB

Contents

module Celluloid
  # Tasks with a Thread backend
  class TaskThread < Task
    # Run the given block within a task
    def initialize(type, meta)
      @resume_queue = Queue.new
      @exception_queue = Queue.new
      @yield_mutex  = Mutex.new
      @yield_cond   = ConditionVariable.new

      super
    end

    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

    def signal
      @yield_cond.signal
      @resume_queue.pop
    end

    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

    def backtrace
      @thread.backtrace
    end
  end
end

Version data entries

6 entries across 6 versions & 2 rubygems

Version Path
vagrant-tiktalik-0.0.3 vendor/bundle/ruby/2.0.0/gems/celluloid-0.15.2/lib/celluloid/tasks/task_thread.rb
celluloid-0.15.2 lib/celluloid/tasks/task_thread.rb
celluloid-0.15.1 lib/celluloid/tasks/task_thread.rb
celluloid-0.15.0 lib/celluloid/tasks/task_thread.rb
celluloid-0.15.0.pre2 lib/celluloid/tasks/task_thread.rb
celluloid-0.15.0.pre lib/celluloid/tasks/task_thread.rb