Class: Proco::MT::Pool

Inherits:
Object
  • Object
show all
Includes:
Logger
Defined in:
lib/proco/mt/pool.rb

Instance Method Summary collapse

Methods included from Logger

#logger

Constructor Details

#initialize(size, logger = nil) ⇒ Pool

Returns a new instance of Pool.



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/proco/mt/pool.rb', line 7

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

Instance Method Details

#assign_try(&block) ⇒ Object



29
30
31
32
33
34
35
36
37
# File 'lib/proco/mt/pool.rb', line 29

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

#assign_wait(&block) ⇒ Object



24
25
26
27
# File 'lib/proco/mt/pool.rb', line 24

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

#counterObject



47
48
49
# File 'lib/proco/mt/pool.rb', line 47

def counter
  @workers.map(&:counter).inject(:+)
end

#exitObject



39
40
41
# File 'lib/proco/mt/pool.rb', line 39

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

#killObject



43
44
45
# File 'lib/proco/mt/pool.rb', line 43

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