Class: Async::Container::Thread

Inherits:
Channel
  • Object
show all
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

#in, #out

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Channel

#close_read, #close_write, #receive

Constructor Details

#initialize(name: nil) ⇒ Thread

Initialize the thread.



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/async/container/thread.rb', line 95

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

.fork(**options) ⇒ Object



85
86
87
88
89
90
91
# File 'lib/async/container/thread.rb', line 85

def self.fork(**options)
	self.new(**options) do |thread|
		::Thread.new do
			yield Instance.for(thread)
		end
	end
end

Instance Method Details

#closeObject

Invoke #terminate! and then #wait for the child thread to exit.



139
140
141
142
143
144
# File 'lib/async/container/thread.rb', line 139

def close
	self.terminate!
	self.wait
ensure
	super
end

#interrupt!Object

Raise Interrupt in the child thread.



147
148
149
# File 'lib/async/container/thread.rb', line 147

def interrupt!
	@thread.raise(Interrupt)
end

#nameObject

Get the name of the thread.



128
129
130
# File 'lib/async/container/thread.rb', line 128

def name
	@thread.name
end

#name=(value) ⇒ Object

Set the name of the thread.



122
123
124
# File 'lib/async/container/thread.rb', line 122

def name= value
	@thread.name = value
end

#terminate!Object

Raise Async::Container::Terminate in the child thread.



152
153
154
# File 'lib/async/container/thread.rb', line 152

def terminate!
	@thread.raise(Terminate)
end

#to_sObject

A human readable representation of the thread.



134
135
136
# File 'lib/async/container/thread.rb', line 134

def to_s
	"\#<#{self.class} #{@thread.name}>"
end

#waitObject

Wait for the thread to exit and return he exit status.



158
159
160
161
162
163
164
165
# File 'lib/async/container/thread.rb', line 158

def wait
	if @waiter
		@waiter.join
		@waiter = nil
	end
	
	return @status
end