Class: Open4::ThreadEnsemble

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(cid) ⇒ ThreadEnsemble

Returns a new instance of ThreadEnsemble.



155
156
157
158
# File 'lib/vendor/open4/lib/open4.rb', line 155

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.



153
154
155
# File 'lib/vendor/open4/lib/open4.rb', line 153

def threads
  @threads
end

Instance Method Details

#add_thread(*a, &b) ⇒ Object



160
161
162
# File 'lib/vendor/open4/lib/open4.rb', line 160

def add_thread *a, &b
  @running ? raise : (@argv << [a, b])
end

#all_doneObject



201
202
203
# File 'lib/vendor/open4/lib/open4.rb', line 201

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

#killallObject

take down process more nicely



167
168
169
170
171
172
173
174
175
# File 'lib/vendor/open4/lib/open4.rb', line 167

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



177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
# File 'lib/vendor/open4/lib/open4.rb', line 177

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