Class: Open4::ThreadEnsemble

Inherits:
Object
  • Object
show all
Defined in:
lib/open4.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#threadsObject (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_doneObject



205
206
207
# File 'lib/open4.rb', line 205

def all_done
  @threads.size.times{ @done.pop }
end

#killallObject

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

#runObject



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