open4 0.5.0 → 0.5.1

Sign up to get free protection for your applications and to get access to all the features.
data/a.rb ADDED
@@ -0,0 +1,64 @@
1
+ require 'thread'
2
+
3
+ class ThreadEnsemble
4
+ #--{{{
5
+ attr 'threads'
6
+
7
+ def initialize
8
+ @threads, @argv, @done, @running = [], [], Queue.new, false
9
+ end
10
+
11
+ def add_thread *a, &b
12
+ @running ? raise : (@argv << [a, b])
13
+ end
14
+
15
+ def killall
16
+ (@threads - [Thread.current]).each{|t| t.kill rescue nil} if @running
17
+ end
18
+
19
+ def run
20
+ @running = true
21
+
22
+ begin
23
+ @argv.each do |a, b|
24
+ @threads << Thread.new(*a) do |*a|
25
+ begin
26
+ b[*a]
27
+ ensure
28
+ killall rescue nil if $!
29
+ @done.push Thread.current
30
+ end
31
+ end
32
+ end
33
+ rescue
34
+ killall
35
+ raise
36
+ ensure
37
+ all_done
38
+ end
39
+
40
+ @threads.map{|t| t.value}
41
+ end
42
+
43
+ def all_done
44
+ @threads.size.times{ @done.pop }
45
+ end
46
+ #--}}}
47
+ end
48
+
49
+ loop do
50
+ te = ThreadEnsemble.new
51
+
52
+ 1000.times do
53
+ te.add_thread do
54
+ raise
55
+ end
56
+ end
57
+
58
+ begin
59
+ te.run
60
+ rescue
61
+ running = te.threads.select{|t| t.status}
62
+ abort running.inspect if running.size > 0
63
+ end
64
+ end
@@ -4,7 +4,7 @@ require 'thread'
4
4
 
5
5
  module Open4
6
6
  #--{{{
7
- VERSION = '0.5.0'
7
+ VERSION = '0.5.1'
8
8
  def self.version() VERSION end
9
9
 
10
10
  class Error < ::StandardError; end
@@ -101,45 +101,46 @@ module Open4
101
101
 
102
102
  class ThreadEnsemble
103
103
  #--{{{
104
+ attr 'threads'
105
+
104
106
  def initialize
105
- @group = ThreadGroup.new
106
- @kill = Queue.new
107
- @killer = Thread.new{ k = @kill.pop and others.map{|t| t.kill unless t == k rescue next}}
108
- @running = false
107
+ @threads, @argv, @done, @running = [], [], Queue.new, false
109
108
  end
110
109
 
111
110
  def add_thread *a, &b
112
- raise if @running
113
- @group.add(Thread.new(*a) do |*a|
114
- Thread.stop
115
- begin
116
- b[*a]
117
- ensure
118
- kill if $!
119
- end
120
- end)
111
+ @running ? raise : (@argv << [a, b])
121
112
  end
122
113
 
123
- def kill
124
- @kill.push Thread.current
114
+ def killall
115
+ (@threads - [Thread.current]).each{|t| t.kill rescue nil} if @running
125
116
  end
126
117
 
127
- def others
128
- threads - [Thread.current]
129
- end
118
+ def run
119
+ @running = true
130
120
 
131
- def threads
132
- @group.list
133
- end
121
+ begin
122
+ @argv.each do |a, b|
123
+ @threads << Thread.new(*a) do |*a|
124
+ begin
125
+ b[*a]
126
+ ensure
127
+ killall rescue nil if $!
128
+ @done.push Thread.current
129
+ end
130
+ end
131
+ end
132
+ rescue
133
+ killall
134
+ raise
135
+ ensure
136
+ all_done
137
+ end
134
138
 
135
- def value
136
- others.map{|t| t.value}
139
+ @threads.map{|t| t.value}
137
140
  end
138
141
 
139
- def run
140
- @running = true
141
- others.map{|t| t.wakeup}
142
- value
142
+ def all_done
143
+ @threads.size.times{ @done.pop }
143
144
  end
144
145
  #--}}}
145
146
  end
@@ -4,7 +4,7 @@ require 'thread'
4
4
 
5
5
  module Open4
6
6
  #--{{{
7
- VERSION = '0.5.0'
7
+ VERSION = '0.5.1'
8
8
  def self.version() VERSION end
9
9
 
10
10
  class Error < ::StandardError; end
@@ -101,45 +101,46 @@ module Open4
101
101
 
102
102
  class ThreadEnsemble
103
103
  #--{{{
104
+ attr 'threads'
105
+
104
106
  def initialize
105
- @group = ThreadGroup.new
106
- @kill = Queue.new
107
- @killer = Thread.new{ k = @kill.pop and others.map{|t| t.kill unless t == k rescue next}}
108
- @running = false
107
+ @threads, @argv, @done, @running = [], [], Queue.new, false
109
108
  end
110
109
 
111
110
  def add_thread *a, &b
112
- raise if @running
113
- @group.add(Thread.new(*a) do |*a|
114
- Thread.stop
115
- begin
116
- b[*a]
117
- ensure
118
- kill if $!
119
- end
120
- end)
111
+ @running ? raise : (@argv << [a, b])
121
112
  end
122
113
 
123
- def kill
124
- @kill.push Thread.current
114
+ def killall
115
+ (@threads - [Thread.current]).each{|t| t.kill rescue nil} if @running
125
116
  end
126
117
 
127
- def others
128
- threads - [Thread.current]
129
- end
118
+ def run
119
+ @running = true
130
120
 
131
- def threads
132
- @group.list
133
- end
121
+ begin
122
+ @argv.each do |a, b|
123
+ @threads << Thread.new(*a) do |*a|
124
+ begin
125
+ b[*a]
126
+ ensure
127
+ killall rescue nil if $!
128
+ @done.push Thread.current
129
+ end
130
+ end
131
+ end
132
+ rescue
133
+ killall
134
+ raise
135
+ ensure
136
+ all_done
137
+ end
134
138
 
135
- def value
136
- others.map{|t| t.value}
139
+ @threads.map{|t| t.value}
137
140
  end
138
141
 
139
- def run
140
- @running = true
141
- others.map{|t| t.wakeup}
142
- value
142
+ def all_done
143
+ @threads.size.times{ @done.pop }
143
144
  end
144
145
  #--}}}
145
146
  end
File without changes
metadata CHANGED
@@ -3,8 +3,8 @@ rubygems_version: 0.8.11
3
3
  specification_version: 1
4
4
  name: open4
5
5
  version: !ruby/object:Gem::Version
6
- version: 0.5.0
7
- date: 2006-08-07 00:00:00.000000 -06:00
6
+ version: 0.5.1
7
+ date: 2006-08-22 00:00:00.000000 -06:00
8
8
  summary: open4
9
9
  require_paths:
10
10
  - lib
@@ -36,6 +36,8 @@ files:
36
36
  - build
37
37
  - install
38
38
  - gemspec.rb
39
+ - a.rb
40
+ - open4-0.5.1.gem
39
41
  - sample/block.rb
40
42
  - sample/simple.rb
41
43
  - sample/exception.rb
@@ -44,7 +46,7 @@ files:
44
46
  - sample/timeout.rb
45
47
  - sample/stdin_timeout.rb
46
48
  - lib/open4.rb
47
- - lib/open4-0.5.0.rb
49
+ - lib/open4-0.5.1.rb
48
50
  test_files: []
49
51
  rdoc_options: []
50
52
  extra_rdoc_files: []