Sha256: 8ae963d885a0fb69da7770429c5ee6e1085c0c3d558e857e39b91cd288d8ad95

Contents?: true

Size: 1.19 KB

Versions: 1

Compression:

Stored size: 1.19 KB

Contents

require 'thread'

module Celluloid
  # Maintain a thread pool FOR SPEED!!
  module ThreadPool
    @pool = []
    @lock = 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)
        @lock.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)
        @lock.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
          begin
            while func = queue.pop
              func.call
            end
          rescue Exception => ex
            Logger.crash("#{self} internal failure", ex)
          end
        end
        thread[:queue] = queue
        thread
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
celluloid-0.8.0 lib/celluloid/thread_pool.rb