diff options
author | Eric Wong <normalperson@yhbt.net> | 2010-05-14 15:52:58 -0700 |
---|---|---|
committer | Eric Wong <normalperson@yhbt.net> | 2010-05-14 18:17:08 -0700 |
commit | 01ee5327c018f64fa4b6aa767c0328c56f699170 (patch) | |
tree | e38767d95236443391ae7480bdba0804764423c2 /lib | |
parent | 5f854e876ba55180936ebfc82002df9bd882a4ae (diff) | |
download | rainbows-01ee5327c018f64fa4b6aa767c0328c56f699170.tar.gz |
It'll be useful later on for a variety of things!
Diffstat (limited to 'lib')
-rw-r--r-- | lib/rainbows.rb | 1 | ||||
-rw-r--r-- | lib/rainbows/queue_pool.rb | 32 |
2 files changed, 33 insertions, 0 deletions
diff --git a/lib/rainbows.rb b/lib/rainbows.rb index b203c93..16e2662 100644 --- a/lib/rainbows.rb +++ b/lib/rainbows.rb @@ -35,6 +35,7 @@ module Rainbows autoload :AppPool, 'rainbows/app_pool' autoload :DevFdResponse, 'rainbows/dev_fd_response' autoload :MaxBody, 'rainbows/max_body' + autoload :QueuePool, 'rainbows/queue_pool' class << self diff --git a/lib/rainbows/queue_pool.rb b/lib/rainbows/queue_pool.rb new file mode 100644 index 0000000..806bbee --- /dev/null +++ b/lib/rainbows/queue_pool.rb @@ -0,0 +1,32 @@ +# -*- encoding: binary -*- +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 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 + end + 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 + end +end |