class Celluloid::Group::Pool

Attributes

busy_size[R]
idle_size[R]
max_idle[RW]

You do not want to use this. Truly, you do not. There is no scenario when you will. But. If you somehow do.. `Celluloid.group_class = Celluloid::Group::Pool` and weep.

Public Class Methods

new() click to toggle source
Calls superclass method Celluloid::Group::new
# File lib/celluloid/group/pool.rb, line 9
def initialize
  super
  @mutex = Mutex.new
  @idle_threads = []
  @group = []
  @busy_size = 0
  @idle_size = 0

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

Public Instance Methods

busy?() click to toggle source
# File lib/celluloid/group/pool.rb, line 25
def busy?
  busy_size.count > 0
end
get(&block) click to toggle source

Get a thread from the pool, running the given block

# File lib/celluloid/group/pool.rb, line 34
def get(&block)
  @mutex.synchronize do
    assert_active

    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?() click to toggle source
# File lib/celluloid/group/pool.rb, line 21
def idle?
  busy_size.count == 0
end
put(thread) click to toggle source

Return a thread to the pool

# File lib/celluloid/group/pool.rb, line 55
def put(thread)
  @mutex.synchronize do
    thread.busy = false
    if idle_size + 1 >= @max_idle
      thread[:celluloid_queue] << nil
      @busy_size -= 1
      @group.delete(thread)
    else
      @idle_threads.push thread
      @busy_size -= 1
      @idle_size = @idle_threads.length
      clean_thread_locals(thread)
    end
  end
end
shutdown() click to toggle source
# File lib/celluloid/group/pool.rb, line 71
def shutdown
  @running = false
  @mutex.synchronize do
    finalize
    @group.each do |thread|
      thread[:celluloid_queue] << nil
    end
    @group.shift.kill until @group.empty?
    @idle_threads.clear
    @busy_size = 0
    @idle_size = 0
  end
end

Private Instance Methods

clean_thread_locals(thread) click to toggle source

Clean the thread locals of an incoming thread

# File lib/celluloid/group/pool.rb, line 109
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/group/pool.rb, line 88
def create
  queue = Queue.new
  thread = Thread.new do
    while proc = queue.pop
      begin
        proc.call
      rescue ::Exception => ex
        Internals::Logger.crash("thread crashed", ex)
      ensure
        put thread
      end
    end
  end

  thread[:celluloid_queue] = queue
  # @idle_threads << thread
  @group << thread
  thread
end
finalize() click to toggle source
# File lib/celluloid/group/pool.rb, line 118
def finalize
  @max_idle = 0
end