Sha256: a2e876149eb1380ee98e02eca0c5a0caf059ecbf22d87cf379c161b22ad1d7eb

Contents?: true

Size: 1.29 KB

Versions: 14

Compression:

Stored size: 1.29 KB

Contents

require 'thread'

module Celluloid
  # Maintain a thread pool FOR SPEED!!
  module InternalPool
    @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
          begin
            if @pool.empty?
              thread = create
            else
              thread = @pool.shift
            end
          end until thread.status # handle crashed threads

          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

14 entries across 14 versions & 2 rubygems

Version Path
celluloid-0.12.4 lib/celluloid/internal_pool.rb
celluloid-0.12.4.pre2 lib/celluloid/internal_pool.rb
celluloid-0.12.3 lib/celluloid/internal_pool.rb
celluloid-0.12.2 lib/celluloid/internal_pool.rb
celluloid-0.12.1 lib/celluloid/internal_pool.rb
celluloid-0.12.1.pre2 lib/celluloid/internal_pool.rb
celluloid-0.12.1.pre lib/celluloid/internal_pool.rb
celluloid-0.12.0 lib/celluloid/internal_pool.rb
celluloid-0.12.0.pre3 lib/celluloid/internal_pool.rb
celluloid-0.12.0.pre2 lib/celluloid/internal_pool.rb
celluloid-0.12.0.pre lib/celluloid/internal_pool.rb
celluloid-0.11.1 lib/celluloid/internal_pool.rb
celluloid-0.11.0 lib/celluloid/internal_pool.rb
kulesa-celluloid-0.10.2 lib/celluloid/internal_pool.rb