Class: Async::Container::Thread

Inherits:
Channel
  • Object
show all
Defined in:
lib/async/container/thread.rb

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

Returns a new instance of Thread.



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

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



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

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

Instance Method Details

#closeObject



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

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

#interrupt!Object



138
139
140
# File 'lib/async/container/thread.rb', line 138

def interrupt!
  @thread.raise(Interrupt)
end

#nameObject



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

def name
  @thread.name
end

#name=(value) ⇒ Object



119
120
121
# File 'lib/async/container/thread.rb', line 119

def name= value
  @thread.name = value
end

#terminate!Object



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

def terminate!
  @thread.raise(Terminate)
end

#to_sObject



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

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

#waitObject



146
147
148
149
150
151
152
153
# File 'lib/async/container/thread.rb', line 146

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