Class: Pwrake::FiberPool

Inherits:
Object
  • Object
show all
Defined in:
lib/pwrake/master/fiber_pool.rb

Instance Method Summary collapse

Constructor Details

#initialize(max_fiber = 2, &block) ⇒ FiberPool

Returns a new instance of FiberPool.



5
6
7
8
9
10
11
12
13
# 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

Instance Method Details

#count_downObject



36
37
38
# File 'lib/pwrake/master/fiber_pool.rb', line 36

def count_down
  @count -= 1
end

#deqObject



27
28
29
30
31
32
33
34
# 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?Boolean

Returns:

  • (Boolean)


40
41
42
# File 'lib/pwrake/master/fiber_pool.rb', line 40

def empty?
  @count == 0
end

#enq(x) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
# 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

#finishObject



44
45
46
47
48
49
50
51
52
# 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_fiberObject



62
63
64
65
# File 'lib/pwrake/master/fiber_pool.rb', line 62

def new_fiber
  @fibers.push(fb = @new_fiber_block.call(self))
  fb
end

#runObject



54
55
56
57
58
59
60
# 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