thread 0.1.6 → 0.1.7
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.
- checksums.yaml +4 -4
- data/lib/thread/pool.rb +45 -30
- data/thread.gemspec +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 3b410457cd8caaa3227527d9365650d3a02f8203
|
4
|
+
data.tar.gz: 0430247c137897d69e59a88a6f57943cd58f46d5
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: c55851e31ce9cc4897006df956bb3e5cff13418e9a294f0168cfdcf36e1deb4ccdf51b3559c53a513ad8d63d6836c23374c46db4ae26e3f061a1089db55a1754
|
7
|
+
data.tar.gz: e91d1f1c0e483f2ed59bbe6966a1fd3604d2b8c082d6356e461d46233094c8d5c1415cf40b506ec22fd08027226af374d11a49621ebe0c74339021a5d7b857cc
|
data/lib/thread/pool.rb
CHANGED
@@ -189,46 +189,45 @@ class Thread::Pool
|
|
189
189
|
}
|
190
190
|
end
|
191
191
|
|
192
|
-
# Are all tasks consumed
|
192
|
+
# Are all tasks consumed?
|
193
193
|
def done?
|
194
194
|
@mutex.synchronize {
|
195
|
-
|
195
|
+
_done?
|
196
196
|
}
|
197
197
|
end
|
198
198
|
|
199
199
|
# Wait until all tasks are consumed. The caller will be blocked until then.
|
200
|
-
def
|
201
|
-
|
202
|
-
|
203
|
-
|
204
|
-
@
|
205
|
-
|
200
|
+
def wait(what = :idle)
|
201
|
+
case what
|
202
|
+
when :done
|
203
|
+
loop do
|
204
|
+
@done_mutex.synchronize {
|
205
|
+
return self if _done?
|
206
|
+
|
207
|
+
@done.wait @done_mutex
|
208
|
+
}
|
209
|
+
end
|
210
|
+
|
211
|
+
when :idle
|
212
|
+
until idle?
|
213
|
+
@done_mutex.synchronize {
|
214
|
+
break if _idle?
|
215
|
+
|
216
|
+
@done.wait @done_mutex
|
217
|
+
}
|
218
|
+
end
|
206
219
|
end
|
220
|
+
|
221
|
+
self
|
207
222
|
end
|
208
223
|
|
209
224
|
# Check if there are idle workers.
|
210
225
|
def idle?
|
211
226
|
@mutex.synchronize {
|
212
|
-
|
227
|
+
_idle?
|
213
228
|
}
|
214
229
|
end
|
215
230
|
|
216
|
-
# Process Block when there is a idle worker if not block its returns
|
217
|
-
def idle (*args, &block)
|
218
|
-
while !idle?
|
219
|
-
@done_mutex.synchronize {
|
220
|
-
break if idle?
|
221
|
-
@done.wait @done_mutex
|
222
|
-
}
|
223
|
-
end
|
224
|
-
|
225
|
-
unless block
|
226
|
-
return
|
227
|
-
end
|
228
|
-
|
229
|
-
process *args, &block
|
230
|
-
end
|
231
|
-
|
232
231
|
# Add a task to the pool which will execute the block with the given
|
233
232
|
# argument.
|
234
233
|
#
|
@@ -239,6 +238,14 @@ class Thread::Pool
|
|
239
238
|
raise ArgumentError, 'you must pass a block'
|
240
239
|
end
|
241
240
|
|
241
|
+
wait.process!(*args, &block)
|
242
|
+
end
|
243
|
+
|
244
|
+
def process! (*args, &block)
|
245
|
+
unless block || @block
|
246
|
+
raise ArgumentError, 'you must pass a block'
|
247
|
+
end
|
248
|
+
|
242
249
|
task = Task.new(self, *args, &(block || @block))
|
243
250
|
|
244
251
|
@mutex.synchronize {
|
@@ -256,7 +263,7 @@ class Thread::Pool
|
|
256
263
|
task
|
257
264
|
end
|
258
265
|
|
259
|
-
alias << process
|
266
|
+
alias << process!
|
260
267
|
|
261
268
|
# Trim the unused threads, if forced threads will be trimmed even if there
|
262
269
|
# are tasks waiting.
|
@@ -350,7 +357,7 @@ class Thread::Pool
|
|
350
357
|
attr_accessor :abort_on_exception
|
351
358
|
end
|
352
359
|
|
353
|
-
|
360
|
+
private
|
354
361
|
def wake_up_timeout
|
355
362
|
if defined? @pipes
|
356
363
|
@pipes.last.write_nonblock 'x' rescue nil
|
@@ -375,7 +382,7 @@ class Thread::Pool
|
|
375
382
|
|
376
383
|
@waiting += 1
|
377
384
|
|
378
|
-
|
385
|
+
done!
|
379
386
|
|
380
387
|
if @idle_trim and @spawned > @min
|
381
388
|
check_time = Time.now + @idle_trim
|
@@ -447,9 +454,17 @@ class Thread::Pool
|
|
447
454
|
}
|
448
455
|
end
|
449
456
|
|
450
|
-
def
|
457
|
+
def _done?
|
458
|
+
@todo.empty? and @waiting == @spawned
|
459
|
+
end
|
460
|
+
|
461
|
+
def _idle?
|
462
|
+
@todo.length < @waiting
|
463
|
+
end
|
464
|
+
|
465
|
+
def done!
|
451
466
|
@done_mutex.synchronize {
|
452
|
-
@done.broadcast if
|
467
|
+
@done.broadcast if _done? or _idle?
|
453
468
|
}
|
454
469
|
end
|
455
470
|
end
|
data/thread.gemspec
CHANGED
@@ -4,7 +4,7 @@ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
|
4
4
|
|
5
5
|
Gem::Specification.new do |spec|
|
6
6
|
spec.name = "thread"
|
7
|
-
spec.version = "0.1.
|
7
|
+
spec.version = "0.1.7"
|
8
8
|
spec.authors = ["meh."]
|
9
9
|
spec.email = ["meh@schizofreni.co"]
|
10
10
|
spec.summary = %q{Various extensions to the base thread library.}
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: thread
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.7
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- meh.
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-04-
|
11
|
+
date: 2015-04-08 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rspec
|