class Celluloid::InternalPool

Maintain a thread pool FOR SPEED!!

Attributes

max_idle[RW]

Public Class Methods

new() click to toggle source
# File lib/celluloid/internal_pool.rb, line 8
def initialize
  @mutex = Mutex.new
  @idle_threads = []
  @all_threads  = []
  @busy_size = 0
  @idle_size = 0

  # TODO: should really adjust this based on usage
  @max_idle = 16
  @running = true
end

Public Instance Methods

active?() click to toggle source
# File lib/celluloid/internal_pool.rb, line 47
def active?
  busy_size + idle_size > 0
end
assert_inactive() click to toggle source
# File lib/celluloid/internal_pool.rb, line 32
def assert_inactive
  if active?
    message = "Thread pool is still active"
    if defined?(JRUBY_VERSION)
      Celluloid.logger.warn message
    else
      raise Error, message
    end
  end
end
assert_running() click to toggle source
# File lib/celluloid/internal_pool.rb, line 28
def assert_running
  raise Error, "Thread pool is not running" unless running?
end
busy_size() click to toggle source
# File lib/celluloid/internal_pool.rb, line 20
def busy_size
  @busy_size
end
each() { |thread| ... } click to toggle source
# File lib/celluloid/internal_pool.rb, line 51
def each
  to_a.each {|thread| yield thread }
end
get(&block) click to toggle source

Get a thread from the pool, running the given block

# File lib/celluloid/internal_pool.rb, line 60
def get(&block)
  @mutex.synchronize do
    assert_running

    begin
      if @idle_threads.empty?
        thread = create
      else
        thread = @idle_threads.pop
        @idle_size = @idle_threads.length
      end
    end until thread.status # handle crashed threads

    thread.busy = true
    @busy_size += 1
    thread[:celluloid_queue] << block
    thread
  end
end
idle_size() click to toggle source
# File lib/celluloid/internal_pool.rb, line 24
def idle_size
  @idle_size
end
kill() click to toggle source
# File lib/celluloid/internal_pool.rb, line 110
def kill
  @mutex.synchronize do
    finalize
    @running = false

    @all_threads.shift.kill until @all_threads.empty?
    @idle_threads.clear
    @busy_size = 0
    @idle_size = 0
  end
end
put(thread) click to toggle source

Return a thread to the pool

# File lib/celluloid/internal_pool.rb, line 81
def put(thread)
  @mutex.synchronize do
    thread.busy = false
    if idle_size + 1 >= @max_idle
      thread[:celluloid_queue] << nil
      @busy_size -= 1
      @all_threads.delete(thread)
    else
      @idle_threads.push thread
      @busy_size -= 1
      @idle_size = @idle_threads.length
      clean_thread_locals(thread)
    end
  end
end
running?() click to toggle source
# File lib/celluloid/internal_pool.rb, line 43
def running?
  @running
end
shutdown() click to toggle source
# File lib/celluloid/internal_pool.rb, line 97
def shutdown
  @mutex.synchronize do
    finalize
    @all_threads.each do |thread|
      thread[:celluloid_queue] << nil
    end
    @all_threads.clear
    @idle_threads.clear
    @busy_size = 0
    @idle_size = 0
  end
end
to_a() click to toggle source
# File lib/celluloid/internal_pool.rb, line 55
def to_a
  @mutex.synchronize { @all_threads.dup }
end

Private Instance Methods

clean_thread_locals(thread) click to toggle source

Clean the thread locals of an incoming thread

# File lib/celluloid/internal_pool.rb, line 146
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() click to toggle source

Create a new thread with an associated queue of procs to run

# File lib/celluloid/internal_pool.rb, line 125
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
  # @idle_threads << thread
  @all_threads << thread
  thread
end
finalize() click to toggle source
# File lib/celluloid/internal_pool.rb, line 155
def finalize
  @max_idle = 0
end