Class: Gem::MiniMirror::Pool

Inherits:
Object
  • Object
show all
Defined in:
lib/rubygems/mini_mirror/pool.rb

Instance Method Summary collapse

Constructor Details

#initialize(size) ⇒ Pool

Returns a new instance of Pool.



3
4
5
6
# File 'lib/rubygems/mini_mirror/pool.rb', line 3

def initialize(size)
  @size = size
  @queue = Queue.new
end

Instance Method Details

#job(&blk) ⇒ Object



8
9
10
# File 'lib/rubygems/mini_mirror/pool.rb', line 8

def job(&blk)
  @queue << blk
end

#run_til_doneObject



12
13
14
15
16
17
18
19
20
# File 'lib/rubygems/mini_mirror/pool.rb', line 12

def run_til_done
  threads = Array.new(@size) do
    Thread.new { @queue.pop.call while true }
  end
  until @queue.empty? && @queue.num_waiting == @size
    threads.each { |t| t.join(0.1) }
  end
  threads.each { |t| t.kill }
end