Class: Celluloid::Internals::ThreadHandle
- Inherits:
-
Object
- Object
- Celluloid::Internals::ThreadHandle
- Defined in:
- lib/celluloid/internals/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
-
#alive? ⇒ Boolean
Is the thread running?.
-
#backtrace ⇒ Object
Obtain the backtrace for this thread.
-
#initialize(actor_system, role = nil) ⇒ ThreadHandle
constructor
A new instance of ThreadHandle.
-
#join(limit = nil) ⇒ Object
Join to a running thread, blocking until it terminates.
-
#kill ⇒ Object
Forcibly kill the thread.
Constructor Details
#initialize(actor_system, role = nil) ⇒ ThreadHandle
Returns a new instance of ThreadHandle.
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
# File 'lib/celluloid/internals/thread_handle.rb', line 7 def initialize(actor_system, role = nil) @mutex = Mutex.new @join = ConditionVariable.new @thread = actor_system.get_thread do Thread.current.role = role begin yield ensure @mutex.synchronize do @thread = nil @join.broadcast end end end end |
Instance Method Details
#alive? ⇒ Boolean
Is the thread running?
25 26 27 |
# File 'lib/celluloid/internals/thread_handle.rb', line 25 def alive? @mutex.synchronize { @thread && @thread.alive? } end |
#backtrace ⇒ Object
Obtain the backtrace for this thread
43 44 45 46 47 48 49 |
# File 'lib/celluloid/internals/thread_handle.rb', line 43 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 |
#join(limit = nil) ⇒ Object
Join to a running thread, blocking until it terminates
36 37 38 39 40 |
# File 'lib/celluloid/internals/thread_handle.rb', line 36 def join(limit = nil) raise ThreadError, "Target thread must not be current thread" if @thread == Thread.current @mutex.synchronize { @join.wait(@mutex, limit) if @thread } self end |
#kill ⇒ Object
Forcibly kill the thread
30 31 32 33 |
# File 'lib/celluloid/internals/thread_handle.rb', line 30 def kill @mutex.synchronize { @thread && @thread.kill } self end |