Parent

Class/Module Index [+]

Quicksearch

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() 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

[Validate]

Generated with the Darkfish Rdoc Generator 2.