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.
Files changed (4) hide show
  1. checksums.yaml +4 -4
  2. data/lib/thread/pool.rb +45 -30
  3. data/thread.gemspec +1 -1
  4. metadata +2 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 6bd6eca648d0974faadfe04e24cbd3a696f386ba
4
- data.tar.gz: f0e27ddf6ed2c9acb872667b3363bb309262aa08
3
+ metadata.gz: 3b410457cd8caaa3227527d9365650d3a02f8203
4
+ data.tar.gz: 0430247c137897d69e59a88a6f57943cd58f46d5
5
5
  SHA512:
6
- metadata.gz: 4d08f22d5c16219d8829efd68ea170d526e8451c9e2b05f9132b4bfba365c72b4aaad07b517ae14080d13dd3c16e954066fa3d528ef2740566876758c7e17617
7
- data.tar.gz: aede26e7afce41766aa3be0031880df21854a02f202cacf1b080754227152e3f6c17342e7374e79052eb2ca6195fe26ac915465fcaaf01c0572d1328dd532422
6
+ metadata.gz: c55851e31ce9cc4897006df956bb3e5cff13418e9a294f0168cfdcf36e1deb4ccdf51b3559c53a513ad8d63d6836c23374c46db4ae26e3f061a1089db55a1754
7
+ data.tar.gz: e91d1f1c0e483f2ed59bbe6966a1fd3604d2b8c082d6356e461d46233094c8d5c1415cf40b506ec22fd08027226af374d11a49621ebe0c74339021a5d7b857cc
@@ -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
- @todo.empty? and @waiting == @spawned
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 wait_done
201
- loop do
202
- @done_mutex.synchronize {
203
- return self if done?
204
- @done.wait @done_mutex
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
- @todo.length < @waiting
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
- private
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
- report_done
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 report_done
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 done? or idle?
467
+ @done.broadcast if _done? or _idle?
453
468
  }
454
469
  end
455
470
  end
@@ -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.6"
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.6
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-07 00:00:00.000000000 Z
11
+ date: 2015-04-08 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rspec