diff options
author | Eric Wong <normalperson@yhbt.net> | 2010-10-22 16:21:03 -0700 |
---|---|---|
committer | Eric Wong <normalperson@yhbt.net> | 2010-10-22 16:21:03 -0700 |
commit | 180485d49ea858f83ef2a28a9e07224aa514edc7 (patch) | |
tree | b4c649d2118c0010bf3876a49dadfe3e4cbc3f86 /lib/rainbows/queue_pool.rb | |
parent | 41145ed4d335718ac43aec9313b7571a12fe96ee (diff) | |
download | rainbows-180485d49ea858f83ef2a28a9e07224aa514edc7.tar.gz |
This simplifies and disambiguates most constant resolution issues as well as lowering our identation level. Hopefully this makes code easier to understand.
Diffstat (limited to 'lib/rainbows/queue_pool.rb')
-rw-r--r-- | lib/rainbows/queue_pool.rb | 41 |
1 files changed, 19 insertions, 22 deletions
diff --git a/lib/rainbows/queue_pool.rb b/lib/rainbows/queue_pool.rb index 3ae899c..4a2ab8c 100644 --- a/lib/rainbows/queue_pool.rb +++ b/lib/rainbows/queue_pool.rb @@ -2,32 +2,29 @@ # :enddoc: require 'thread' -module Rainbows +# Thread pool class based on pulling off a single Ruby Queue. +# This is NOT used for the ThreadPool class, since that class does not +# need a userspace Queue. +class Rainbows::QueuePool < Struct.new(:queue, :threads) + G = Rainbows::G - # Thread pool class based on pulling off a single Ruby Queue. - # This is NOT used for the ThreadPool class, since that class does not - # need a userspace Queue. - class QueuePool < Struct.new(:queue, :threads) - G = Rainbows::G - - def initialize(size = 20, &block) - q = Queue.new - self.threads = (1..size).map do - Thread.new do - while job = q.shift - block.call(job) - end + def initialize(size = 20, &block) + q = Queue.new + self.threads = (1..size).map do + Thread.new do + while job = q.shift + block.call(job) end end - self.queue = q end + self.queue = q + end - def quit! - threads.each { |_| queue << nil } - threads.delete_if do |t| - G.tick - t.alive? ? t.join(0.01) : true - end until threads.empty? - end + def quit! + threads.each { |_| queue << nil } + threads.delete_if do |t| + G.tick + t.alive? ? t.join(0.01) : true + end until threads.empty? end end |