pipa-threadpool 0.2.1 → 0.2.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. data/README.rdoc +3 -4
  2. data/lib/threadpool.rb +15 -13
  3. metadata +2 -2
data/README.rdoc CHANGED
@@ -2,7 +2,7 @@
2
2
  O HAI! My name is Pool, Thread Pool. Now in Ruby.
3
3
 
4
4
  === Installation
5
- gem install pipa-threadpool -s gems.github.com
5
+ gem install pipa-threadpool -s http://gems.github.com
6
6
 
7
7
  === Usage
8
8
  pool = ThreadPool.new
@@ -20,9 +20,8 @@ http://rdoc.info/projects/pipa/threadpool
20
20
  Please report via Github issue tracking.
21
21
 
22
22
  === See also
23
- http://github.com/pipa/monkeyjob -- Background job runner
24
- http://github.com/pipa/xmlnuts -- Ruby <-> XML mapping
25
- http://github.com/pipa/statelogic -- A simple state machine for ActiveRecord
23
+ * http://github.com/pipa/xmlnuts -- Ruby <-> XML mapping
24
+ * http://github.com/pipa/statelogic -- A simple state machine for ActiveRecord
26
25
 
27
26
 
28
27
  Free hint: If you liek mudkipz^W^Wfeel like generous today you can tip me at http://tipjoy.com/u/pisuka
data/lib/threadpool.rb CHANGED
@@ -56,14 +56,8 @@ class ThreadPool
56
56
  @keep_alive_time = (args[2] || options[:keep_alive] || DEFAULT_KEEP_ALIVE_TIME).to_f
57
57
  raise ArgumentError, "keep_alive_time must be a non-negative real number" if @keep_alive_time < 0
58
58
 
59
- @workers, @jobs = ThreadGroup.new, Queue.new
59
+ @workers, @jobs = [], Queue.new
60
60
 
61
- @worker_routine = proc do
62
- while job = @jobs.pop
63
- job.run rescue nil
64
- end
65
- end
66
-
67
61
  @controller = Thread.new do
68
62
  loop do
69
63
  sleep(@keep_alive_time)
@@ -128,9 +122,9 @@ class ThreadPool
128
122
  _sync do
129
123
  @dead = true
130
124
  @controller.run
131
- stop_workers(@workers.list.size)
125
+ stop_workers(@workers.size)
132
126
  end
133
- ThreadsWait.all_waits(@controller, *@workers.list)
127
+ ThreadsWait.all_waits(@controller, *@workers)
134
128
  self
135
129
  end
136
130
 
@@ -141,7 +135,7 @@ class ThreadPool
141
135
  _sync do
142
136
  @dead = true
143
137
  @controller.run
144
- @workers.list.each {|w| w.kill }
138
+ @workers.each {|w| w.kill }
145
139
  end
146
140
  self
147
141
  end
@@ -152,7 +146,7 @@ class ThreadPool
152
146
  raise ArgumentError, 'block must be provided' unless block_given?
153
147
  _sync do
154
148
  if @jobs.num_waiting == 0
155
- if @workers.list.size < @max_workers
149
+ if @workers.size < @max_workers
156
150
  create_worker
157
151
  else
158
152
  return nil unless enqueue
@@ -162,7 +156,7 @@ class ThreadPool
162
156
  end
163
157
  self
164
158
  end
165
-
159
+
166
160
  def _sync #:nodoc:
167
161
  synchronize do
168
162
  check_state
@@ -175,7 +169,7 @@ class ThreadPool
175
169
  end
176
170
 
177
171
  def create_worker #:nodoc:
178
- @workers.add(Thread.new(&@worker_routine))
172
+ @workers << Thread.new(&method(:worker_routine))
179
173
  end
180
174
 
181
175
  def create_workers(n) #:nodoc:
@@ -185,5 +179,13 @@ class ThreadPool
185
179
  def stop_workers(n) #:nodoc:
186
180
  n.times { @jobs << nil }
187
181
  end
182
+
183
+ def worker_routine #:nodoc:
184
+ while job = @jobs.pop
185
+ job.run rescue nil
186
+ end
187
+ ensure
188
+ synchronize { @workers.delete(Thread.current) }
189
+ end
188
190
  end
189
191
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: pipa-threadpool
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.1
4
+ version: 0.2.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Igor Gunko
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-06-06 00:00:00 -07:00
12
+ date: 2009-06-10 00:00:00 -07:00
13
13
  default_executable:
14
14
  dependencies: []
15
15