Class: Open4::ThreadEnsemble
- Inherits:
-
Object
- Object
- Open4::ThreadEnsemble
- Defined in:
- lib/open4.rb
Instance Attribute Summary collapse
-
#threads ⇒ Object
readonly
Returns the value of attribute threads.
Instance Method Summary collapse
- #add_thread(*a, &b) ⇒ Object
- #all_done ⇒ Object
-
#initialize(cid) ⇒ ThreadEnsemble
constructor
A new instance of ThreadEnsemble.
-
#killall ⇒ Object
take down process more nicely.
- #run ⇒ Object
Constructor Details
#initialize(cid) ⇒ ThreadEnsemble
Returns a new instance of ThreadEnsemble.
159 160 161 162 |
# File 'lib/open4.rb', line 159 def initialize cid @cid, @threads, @argv, @done, @running = cid, [], [], Queue.new, false @killed = false end |
Instance Attribute Details
#threads ⇒ Object (readonly)
Returns the value of attribute threads.
157 158 159 |
# File 'lib/open4.rb', line 157 def threads @threads end |
Instance Method Details
#add_thread(*a, &b) ⇒ Object
164 165 166 |
# File 'lib/open4.rb', line 164 def add_thread *a, &b @running ? raise : (@argv << [a, b]) end |
#all_done ⇒ Object
205 206 207 |
# File 'lib/open4.rb', line 205 def all_done @threads.size.times{ @done.pop } end |
#killall ⇒ Object
take down process more nicely
171 172 173 174 175 176 177 178 179 |
# File 'lib/open4.rb', line 171 def killall c = Thread.critical return nil if @killed Thread.critical = true (@threads - [Thread.current]).each{|t| t.kill rescue nil} @killed = true ensure Thread.critical = c end |
#run ⇒ Object
181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 |
# File 'lib/open4.rb', line 181 def run @running = true begin @argv.each do |a, b| @threads << Thread.new(*a) do |*_a| begin b[*_a] ensure killall rescue nil if $! @done.push Thread.current end end end rescue killall raise ensure all_done end @threads.map{|t| t.value} end |