Sha256: 1f7bbe079f2c6d1fcf2d5fa24fc80e13a3d2f13c7805d932d597fd2b1ac7e4b4

Contents?: true

Size: 1.16 KB

Versions: 3

Compression:

Stored size: 1.16 KB

Contents

module Celluloid
  # An abstraction around threads from the InternalPool which ensures we don't
  # accidentally do things to threads which have been returned to the pool,
  # such as, say, killing them
  class ThreadHandle
    def initialize
      @mutex = Mutex.new
      @join  = ConditionVariable.new

      @thread = Celluloid.internal_pool.get do
        begin
          yield
        ensure
          @mutex.synchronize do
            @thread = nil
            @join.broadcast
          end
        end
      end
    end

    # Is the thread running?
    def alive?
      @mutex.synchronize { @thread.alive? if @thread }
    end

    # Forcibly kill the thread
    def kill
      !!@mutex.synchronize { @thread.kill if @thread }
      self
    end

    # Join to a running thread, blocking until it terminates
    def join
      @mutex.synchronize { @join.wait(@mutex) if @thread }
      self
    end

    # Obtain the backtrace for this thread
    def backtrace
      @thread.backtrace
    rescue NoMethodError
      # undefined method `backtrace' for nil:NilClass
      # Swallow this in case this ThreadHandle was terminated and @thread was
      # set to nil
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
celluloid-0.13.0 lib/celluloid/thread_handle.rb
celluloid-0.13.0.pre2 lib/celluloid/thread_handle.rb
celluloid-0.13.0.pre lib/celluloid/thread_handle.rb