Sha256: 22782633f46a609e81b811bb275c58c7ec8328fa59e231d0462764552aebdfd6

Contents?: true

Size: 1001 Bytes

Versions: 2

Compression:

Stored size: 1001 Bytes

Contents

class Proco
module MT
# @private
class Pool
  include Proco::Logger

  def initialize size, logger = nil
    @logger = logger
    @workers = size.times.map { |i|
      Worker.new @logger
    }
    @num_workers = @workers.length
    if @num_workers > 1
      self.instance_eval do
        alias assign assign_try
      end
    else
      self.instance_eval do
        alias assign assign_wait
      end
    end
  end

  def assign_wait &block
    # Optimistic randomized assignment to avoid mutex contention
    @workers.sample.assign(&block)
  end

  def assign_try &block
    @num_workers.times do |i|
      ret = @workers.sample.try_assign(&block)
      return ret if ret
    end
    # phew. blocking assignment
    # debug "Failed immediate thread allocation in the 1st round (#@num_workers)"
    assign_wait(&block)
  end

  def exit
    @workers.each(&:exit)
  end

  def kill
    @workers.each(&:kill)
  end

  def counter
    @workers.map(&:counter).inject(:+)
  end
end#Pool
end#MT
end#Proco

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
proco-0.0.2 lib/proco/mt/pool.rb
proco-0.0.1 lib/proco/mt/pool.rb