Class: Async::Container::Thread
- Defined in:
- lib/async/container/thread.rb
Overview
Represents a running child thread from the point of view of the parent container.
Defined Under Namespace
Classes: Exit, Instance, Status
Instance Attribute Summary
Attributes inherited from Channel
Class Method Summary collapse
Instance Method Summary collapse
-
#close ⇒ Object
Invoke #terminate! and then #wait for the child thread to exit.
-
#initialize(name: nil) ⇒ Thread
constructor
Initialize the thread.
-
#interrupt! ⇒ Object
Raise Interrupt in the child thread.
-
#name ⇒ Object
Get the name of the thread.
-
#name=(value) ⇒ Object
Set the name of the thread.
-
#terminate! ⇒ Object
Raise Terminate in the child thread.
-
#to_s ⇒ Object
A human readable representation of the thread.
-
#wait ⇒ Object
Wait for the thread to exit and return he exit status.
Methods inherited from Channel
#close_read, #close_write, #receive
Constructor Details
#initialize(name: nil) ⇒ Thread
Initialize the thread.
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 |
# File 'lib/async/container/thread.rb', line 111 def initialize(name: nil) super() @status = nil @thread = yield(self) @thread.report_on_exception = false @thread.name = name @waiter = ::Thread.new do begin @thread.join rescue Exit => exit finished(exit.error) rescue Interrupt # Graceful shutdown. finished rescue Exception => error finished(error) else finished end end end |
Class Method Details
Instance Method Details
#close ⇒ Object
Invoke #terminate! and then #wait for the child thread to exit.
155 156 157 158 159 160 |
# File 'lib/async/container/thread.rb', line 155 def close self.terminate! self.wait ensure super end |
#interrupt! ⇒ Object
Raise Interrupt in the child thread.
163 164 165 |
# File 'lib/async/container/thread.rb', line 163 def interrupt! @thread.raise(Interrupt) end |
#name ⇒ Object
Get the name of the thread.
144 145 146 |
# File 'lib/async/container/thread.rb', line 144 def name @thread.name end |
#name=(value) ⇒ Object
Set the name of the thread.
138 139 140 |
# File 'lib/async/container/thread.rb', line 138 def name= value @thread.name = value end |
#terminate! ⇒ Object
Raise Async::Container::Terminate in the child thread.
168 169 170 |
# File 'lib/async/container/thread.rb', line 168 def terminate! @thread.raise(Terminate) end |
#to_s ⇒ Object
A human readable representation of the thread.
150 151 152 |
# File 'lib/async/container/thread.rb', line 150 def to_s "\#<#{self.class} #{@thread.name}>" end |
#wait ⇒ Object
Wait for the thread to exit and return he exit status.
174 175 176 177 178 179 180 181 |
# File 'lib/async/container/thread.rb', line 174 def wait if @waiter @waiter.join @waiter = nil end return @status end |