class Af::ThreadPool

Attributes

max_size[RW]
thread_class[RW]
workers[R]

Public Class Methods

new(max_size = 10, thread_class = Thread) click to toggle source
# File lib/fiksu-af/thread_pool.rb, line 48
def initialize(max_size = 10, thread_class = Thread)
  @max_size = max_size
  @workers = []
  @mutex = Mutex.new
  @thread_class = thread_class
end

Public Instance Methods

busy?() click to toggle source
# File lib/fiksu-af/thread_pool.rb, line 59
def busy?
  @mutex.synchronize {@workers.any? {|w| w.busy?}}
end
create_worker() click to toggle source
# File lib/fiksu-af/thread_pool.rb, line 95
def create_worker
  return nil if @workers.size >= @max_size
  worker = Worker.new(@thread_class)
  @workers << worker
  worker
end
find_available_worker() click to toggle source
# File lib/fiksu-af/thread_pool.rb, line 87
def find_available_worker
  free_worker || create_worker
end
free_worker() click to toggle source
# File lib/fiksu-af/thread_pool.rb, line 91
def free_worker
  @workers.each {|w| return w unless w.busy?}; nil
end
join() click to toggle source
# File lib/fiksu-af/thread_pool.rb, line 63
def join
  sleep 0.01 while busy?
end
process(&block) click to toggle source
# File lib/fiksu-af/thread_pool.rb, line 67
def process(&block)
  while true
    @mutex.synchronize do
      worker = find_available_worker 
      if worker
        return worker.set_block(block)
      end
    end
    sleep 0.01
  end
end
size() click to toggle source
# File lib/fiksu-af/thread_pool.rb, line 55
def size
  @mutex.synchronize {@workers.size}
end
wait_for_worker() click to toggle source
# File lib/fiksu-af/thread_pool.rb, line 79
def wait_for_worker
  while true
    worker = find_available_worker
    return worker if worker
    sleep 0.01
  end
end