Class: Celluloid::ThreadHandle

Inherits:
Object
  • Object
show all
Defined in:
lib/celluloid/thread_handle.rb

Overview

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

Instance Method Summary collapse

Constructor Details

#initializeThreadHandle

Returns a new instance of ThreadHandle.



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/celluloid/thread_handle.rb', line 6

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

Instance Method Details

#alive?Boolean

Returns:

  • (Boolean)


22
23
24
# File 'lib/celluloid/thread_handle.rb', line 22

def alive?
  @mutex.synchronize { @thread.alive? if @thread }
end

#joinObject



30
31
32
33
# File 'lib/celluloid/thread_handle.rb', line 30

def join
  @mutex.synchronize { @join.wait(@mutex) if @thread }
  nil
end

#killObject



26
27
28
# File 'lib/celluloid/thread_handle.rb', line 26

def kill
  !!@mutex.synchronize { @thread.kill if @thread }
end