class RuneRb::Misc::ThreadPool

Attributes

queue_limit[RW]

Public Class Methods

new(count, queue_limit = 0) click to toggle source

Initialize with number of threads to run

# File app/core/util.rb, line 337
def initialize(count, queue_limit = 0)
  @mutex = Mutex.new
  @executors = []
  @queue = []
  @queue_limit = queue_limit
  @count = count
  count.times { @executors << Executor.new(@queue, @mutex) }
end

Public Instance Methods

busy() click to toggle source
# File app/core/util.rb, line 385
def busy
  !(@queue.empty? && @executors.all?{|e| !e.active})
end
close() click to toggle source

Kills all threads

# File app/core/util.rb, line 376
def close
  @executors.each {|e| e.close }
end
execute(*args, &block) click to toggle source

Runs the block at some time in the near future

# File app/core/util.rb, line 347
def execute(*args, &block)
  init_completable(block)

  if @queue_limit > 0
    sleep 0.01 until @queue.size < @queue_limit
  end

  @mutex.synchronize do
    @queue << [args, block]
  end
end
join() click to toggle source

Sleeps and blocks until the task queue is finished executing

# File app/core/util.rb, line 381
def join
  sleep 0.01 until @queue.empty? && @executors.all?{|e| !e.active}
end
size() click to toggle source

Size of the thread pool

# File app/core/util.rb, line 371
def size
  @count
end
synchronous_execute(*args, &block) click to toggle source

Runs the block at some time in the near future, and blocks until complete

# File app/core/util.rb, line 360
def synchronous_execute(*args, &block)
  execute(*args, &block)
  sleep 0.01 until block.complete?
end
waiting() click to toggle source

Size of the task queue

# File app/core/util.rb, line 366
def waiting
  @queue.size
end

Protected Instance Methods

init_completable(block) click to toggle source
# File app/core/util.rb, line 391
def init_completable(block)
  block.extend(Completable)
  block.complete = false
end