lib/rmega/pool.rb in rmega-0.1.7 vs lib/rmega/pool.rb in rmega-0.2.0

- old
+ new

@@ -1,31 +1,38 @@ -require 'thread' - module Rmega class Pool - MAX = 4 + include Options - def initialize(max = MAX) - Thread.abort_on_exception = true + def initialize + threads_raises_exceptions @mutex = Mutex.new @resource = ConditionVariable.new - @max = max || MAX + @max = options.thread_pool_size @running = [] @queue = [] end + def threads_raises_exceptions + Thread.abort_on_exception = true + end + def defer(&block) synchronize { @queue << block } process_queue end + alias :process :defer + def wait_done + return if done? synchronize { @resource.wait(@mutex) } end + alias :shutdown :wait_done + private def synchronize(&block) @mutex.synchronize(&block) end @@ -45,13 +52,17 @@ def signal_done synchronize { @resource.signal } end + def thread_terminated + synchronize { @running.reject! { |thread| thread == Thread.current } } + end + def thread_proc(&block) Proc.new do block.call - @running.reject! { |thread| thread == Thread.current } + thread_terminated process_queue signal_done if done? end end end