Sha256: 54d53187c57c3dbe6c5718e317408a85d385a07cadaf18408d11f2f6620108e9
Contents?: true
Size: 1.2 KB
Versions: 3
Compression:
Stored size: 1.2 KB
Contents
require 'thread' module Celluloid # Maintain a thread pool FOR SPEED!! module ThreadPool @pool = [] @mutex = Mutex.new # TODO: should really adjust this based on usage @max_idle = 16 class << self attr_accessor :max_idle # Get a thread from the pool, running the given block def get(&block) @mutex.synchronize do if @pool.empty? thread = create else thread = @pool.shift end thread[:queue] << block thread end end # Return a thread to the pool def put(thread) @mutex.synchronize do if @pool.size >= @max_idle thread[:queue] << nil else @pool << thread end end end # Create a new thread with an associated queue of procs to run 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[:queue] = queue thread end end end end
Version data entries
3 entries across 3 versions & 1 rubygems
Version | Path |
---|---|
celluloid-0.10.0 | lib/celluloid/thread_pool.rb |
celluloid-0.9.1 | lib/celluloid/thread_pool.rb |
celluloid-0.9.0 | lib/celluloid/thread_pool.rb |