Parent

Class/Module Index [+]

Quicksearch

Celluloid::InternalPool

Maintain a thread pool FOR SPEED!!

Attributes

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

Public Class Methods

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

  reset
end

Public 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 73
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 54
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
  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 22
def get(&block)
  @mutex.synchronize do
    begin
      if @pool.empty?
        thread = create
      else
        thread = @pool.shift
        @idle_size -= 1
      end
    end until thread.status # handle crashed threads

    @busy_size += 1
    thread[:celluloid_queue] << block
    thread
  end
end
put(thread) click to toggle source

Return a thread to the pool

# File lib/celluloid/internal_pool.rb, line 40
def put(thread)
  @mutex.synchronize do
    if @pool.size >= @max_idle
      thread[:celluloid_queue] << nil
    else
      clean_thread_locals(thread)
      @pool << thread
      @idle_size += 1
      @busy_size -= 1
    end
  end
end
reset() click to toggle source
# File lib/celluloid/internal_pool.rb, line 16
def reset
  # TODO: should really adjust this based on usage
  @max_idle = 16
end
shutdown() click to toggle source
# File lib/celluloid/internal_pool.rb, line 82
def shutdown
  @mutex.synchronize do
    @max_idle = 0
    @pool.each do |thread|
      thread[:celluloid_queue] << nil
    end
  end
end

[Validate]

Generated with the Darkfish Rdoc Generator 2.