Maintain a thread pool FOR SPEED!!
# File lib/celluloid/internal_pool.rb, line 8 def initialize @pool = [] @mutex = Mutex.new @busy_size = @idle_size = 0 reset end
Clean the thread locals of an incoming thread
# File lib/celluloid/internal_pool.rb, line 73 def clean_thread_locals(thread) thread.keys.each do |key| next if key == :celluloid_queue # Ruby seems to lack an API for deleting thread locals. WTF, Ruby? thread[key] = nil end end
Create a new thread with an associated queue of procs to run
# File lib/celluloid/internal_pool.rb, line 54 def create queue = Queue.new thread = Thread.new do while proc = queue.pop begin proc.call rescue => ex Logger.crash("thread crashed", ex) end put thread end end thread[:celluloid_queue] = queue thread end
Get a thread from the pool, running the given block
# File lib/celluloid/internal_pool.rb, line 22 def get(&block) @mutex.synchronize do begin if @pool.empty? thread = create else thread = @pool.shift @idle_size -= 1 end end until thread.status # handle crashed threads @busy_size += 1 thread[:celluloid_queue] << block thread end end
Return a thread to the pool
# File lib/celluloid/internal_pool.rb, line 40 def put(thread) @mutex.synchronize do if @pool.size >= @max_idle thread[:celluloid_queue] << nil else clean_thread_locals(thread) @pool << thread @idle_size += 1 @busy_size -= 1 end end end
# File lib/celluloid/internal_pool.rb, line 16 def reset # TODO: should really adjust this based on usage @max_idle = 16 end
# File lib/celluloid/internal_pool.rb, line 82 def shutdown @mutex.synchronize do @max_idle = 0 @pool.each do |thread| thread[:celluloid_queue] << nil end end end