Class: Open4::ThreadEnsemble

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(cid) ⇒ ThreadEnsemble

Returns a new instance of ThreadEnsemble.



109
110
111
112
# File 'lib/rvm/open4.rb', line 109

def initialize cid
  @cid, @threads, @argv, @done, @running = cid, [], [], Queue.new, false
  @killed = false
end

Instance Attribute Details

#threadsObject (readonly)

–{{{



107
108
109
# File 'lib/rvm/open4.rb', line 107

def threads
  @threads
end

Instance Method Details

#add_thread(*a, &b) ⇒ Object



114
115
116
# File 'lib/rvm/open4.rb', line 114

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

#all_doneObject



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

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

#killallObject

take down process more nicely



121
122
123
124
125
126
127
128
129
# File 'lib/rvm/open4.rb', line 121

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



131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# File 'lib/rvm/open4.rb', line 131

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