class Pwrake::FiberPool
Public Class Methods
new(max_fiber=2,&block)
click to toggle source
# File lib/pwrake/master/fiber_pool.rb, line 5 def initialize(max_fiber=2,&block) @new_fiber_block = block @max_fiber = max_fiber @count = 0 @fibers = [] @idle_fiber = [] @q = [] @new_fiber_start_time = Pwrake.clock-10 end
Public Instance Methods
count_down()
click to toggle source
# File lib/pwrake/master/fiber_pool.rb, line 36 def count_down @count -= 1 end
deq()
click to toggle source
# File lib/pwrake/master/fiber_pool.rb, line 27 def deq while @q.empty? return nil if @finished @idle_fiber.push(Fiber.current) Fiber.yield end @q.shift end
empty?()
click to toggle source
# File lib/pwrake/master/fiber_pool.rb, line 40 def empty? @count == 0 end
enq(x)
click to toggle source
# File lib/pwrake/master/fiber_pool.rb, line 15 def enq(x) @q.push(x) @count += 1 if @idle_fiber.empty? and @fibers.size < @max_fiber and Pwrake.clock - @new_fiber_start_time > 0.1 @idle_fiber << new_fiber end f = @idle_fiber.shift f.resume if f @finished end
finish()
click to toggle source
# File lib/pwrake/master/fiber_pool.rb, line 44 def finish @finished = true run while f = @fibers.shift if f.alive? $stderr.puts "FiberPool#finish: fiber is still alive." end end end
new_fiber()
click to toggle source
# File lib/pwrake/master/fiber_pool.rb, line 62 def new_fiber @fibers.push(fb = @new_fiber_block.call(self)) fb end
run()
click to toggle source
# File lib/pwrake/master/fiber_pool.rb, line 54 def run cond = !@idle_fiber.empty? while f = @idle_fiber.shift f.resume end cond end