class Async::Container::Thread

Represents a running child thread from the point of view of the parent container.

Public Class Methods

fork(**options) { |for| ... } click to toggle source
# File lib/async/container/thread.rb, line 101
def self.fork(**options)
        self.new(**options) do |thread|
                ::Thread.new do
                        yield Instance.for(thread)
                end
        end
end
new(name: nil) { |self| ... } click to toggle source

Initialize the thread. @parameter name [String] The name to use for the child thread.

Calls superclass method Async::Container::Channel::new
# 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

Public Instance Methods

close() click to toggle source

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

Calls superclass method Async::Container::Channel#close
# File lib/async/container/thread.rb, line 155
def close
        self.terminate!
        self.wait
ensure
        super
end
interrupt!() click to toggle source

Raise {Interrupt} in the child thread.

# File lib/async/container/thread.rb, line 163
def interrupt!
        @thread.raise(Interrupt)
end
name() click to toggle source

Get the name of the thread. @returns [String]

# File lib/async/container/thread.rb, line 144
def name
        @thread.name
end
name=(value) click to toggle source

Set the name of the thread. @parameter value [String] The name to set.

# File lib/async/container/thread.rb, line 138
def name= value
        @thread.name = value
end
terminate!() click to toggle source

Raise {Terminate} in the child thread.

# File lib/async/container/thread.rb, line 168
def terminate!
        @thread.raise(Terminate)
end
to_s() click to toggle source

A human readable representation of the thread. @returns [String]

# File lib/async/container/thread.rb, line 150
def to_s
        "\#<#{self.class} #{@thread.name}>"
end
wait() click to toggle source

Wait for the thread to exit and return he exit status. @returns [Status]

# File lib/async/container/thread.rb, line 174
def wait
        if @waiter
                @waiter.join
                @waiter = nil
        end
        
        return @status
end

Protected Instance Methods

finished(error = nil) click to toggle source

Invoked by the @waiter thread to indicate the outcome of the child thread.

# File lib/async/container/thread.rb, line 206
def finished(error = nil)
        if error
                Console.logger.error(self) {error}
        end
        
        @status = Status.new(error)
        self.close_write
end