Sha256: 3d31f5d6f278c8d48a0375b48c1ba5a90c3df69fe3e548f4988301aca509b27e

Contents?: true

Size: 994 Bytes

Versions: 7

Compression:

Stored size: 994 Bytes

Contents

require 'forwardable'

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
    extend Forwardable
    def_delegators :@thread, :backtrace

    def initialize
      @mutex = Mutex.new
      @join  = ConditionVariable.new

      @thread = InternalPool.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
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
celluloid-0.12.4 lib/celluloid/thread_handle.rb
celluloid-0.12.4.pre2 lib/celluloid/thread_handle.rb
celluloid-0.12.4.pre lib/celluloid/thread_handle.rb
celluloid-0.12.3 lib/celluloid/thread_handle.rb
celluloid-0.12.2 lib/celluloid/thread_handle.rb
celluloid-0.12.1 lib/celluloid/thread_handle.rb
celluloid-0.12.1.pre2 lib/celluloid/thread_handle.rb