Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 19 additions & 19 deletions lib/open4.rb
Original file line number Diff line number Diff line change
Expand Up @@ -179,9 +179,9 @@ def run

begin
@argv.each do |a, b|
@threads << Thread.new(*a) do |*a|
@threads << Thread.new(*a) do |*th_a|
begin
b[*a]
b[*th_a]
ensure
killall rescue nil if $!
@done.push Thread.current
Expand Down Expand Up @@ -210,9 +210,9 @@ def to timeout = nil

def new_thread *a, &b
cur = Thread.current
Thread.new(*a) do |*a|
Thread.new(*a) do |*th_a|
begin
b[*a]
b[*th_a]
rescue Exception => e
cur.raise e
end
Expand All @@ -222,11 +222,11 @@ def new_thread *a, &b

def getopts opts = {}
lambda do |*args|
keys, default, ignored = args
keys, _, _ = args
catch(:opt) do
[keys].flatten.each do |key|
[key, key.to_s, key.to_s.intern].each do |key|
throw :opt, opts[key] if opts.has_key?(key)
[key, key.to_s, key.to_s.intern].each do |th_key|
throw :opt, opts[th_key] if opts.has_key?(th_key)
end
end
default
Expand Down Expand Up @@ -255,8 +255,8 @@ def relay src, dst = nil, t = nil
q = Queue.new
th = nil

timer_set = lambda do |t|
th = new_thread{ to(t){ q.pop } }
timer_set = lambda do |tt|
th = new_thread{ to(tt){ q.pop } }
end

timer_cancel = lambda do |t|
Expand All @@ -265,9 +265,9 @@ def relay src, dst = nil, t = nil

timer_set[t]
begin
src.each do |buf|
src.each do |inner_buf|
timer_cancel[t]
send_dst[buf]
send_dst[inner_buf]
timer_set[t]
end
ensure
Expand Down Expand Up @@ -338,17 +338,17 @@ def spawn arg, *argv

te = ThreadEnsemble.new c

te.add_thread(i, stdin) do |i, stdin|
relay stdin, i, stdin_timeout
i.close rescue nil
te.add_thread(i, stdin) do |th_i, th_stdin|
relay stdin, th_i, stdin_timeout
th_i.close rescue nil
end

te.add_thread(o, stdout) do |o, stdout|
relay o, stdout, stdout_timeout
te.add_thread(o, stdout) do |th_o, th_stdout|
relay th_o, th_stdout, stdout_timeout
end

te.add_thread(e, stderr) do |o, stderr|
relay e, stderr, stderr_timeout
te.add_thread(e, stderr) do |th_o, th_stderr|
relay th_e, th_stderr, stderr_timeout
end

te.run
Expand Down Expand Up @@ -382,7 +382,7 @@ def background arg, *argv
else
argv.push opts
end
thread = Thread.new(arg, argv){|arg, argv| spawn arg, *argv}
thread = Thread.new(arg, argv){|th_arg, th_argv| spawn th_arg, *th_argv}
sc = class << thread; self; end
sc.module_eval {
define_method(:pid){ @pid ||= q.pop }
Expand Down