Class: Open4::ThreadEnsemble
- Defined in:
- lib/alib-0.5.1/open4.rb,
lib/alib-0.5.1/open4-0.9.1.rb
Instance Attribute Summary collapse
-
#threads ⇒ Object
readonly
–{{{.
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.
104 105 106 107 |
# File 'lib/alib-0.5.1/open4.rb', line 104 def initialize cid @cid, @threads, @argv, @done, @running = cid, [], [], Queue.new, false @killed = false end |
Instance Attribute Details
#threads ⇒ Object (readonly)
–{{{
102 103 104 |
# File 'lib/alib-0.5.1/open4.rb', line 102 def threads @threads end |
Instance Method Details
#add_thread(*a, &b) ⇒ Object
109 110 111 |
# File 'lib/alib-0.5.1/open4.rb', line 109 def add_thread *a, &b @running ? raise : (@argv << [a, b]) end |
#all_done ⇒ Object
150 151 152 |
# File 'lib/alib-0.5.1/open4.rb', line 150 def all_done @threads.size.times{ @done.pop } end |
#killall ⇒ Object
take down process more nicely
116 117 118 119 120 121 122 123 124 |
# File 'lib/alib-0.5.1/open4.rb', line 116 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
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 |
# File 'lib/alib-0.5.1/open4.rb', line 126 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 |