Class: Pwrake::FiberQueue

Inherits:
Object
  • Object
show all
Defined in:
lib/pwrake/branch/fiber_queue.rb

Instance Method Summary collapse

Constructor Details

#initialize(log = nil) ⇒ FiberQueue

Returns a new instance of FiberQueue.



10
11
12
13
14
15
# File 'lib/pwrake/branch/fiber_queue.rb', line 10

def initialize(log=nil)
  @q = []
  @waiter = []
  @finished = false
  @log = log
end

Instance Method Details

#deqObject



26
27
28
29
30
31
32
33
# File 'lib/pwrake/branch/fiber_queue.rb', line 26

def deq
  while @q.empty?
    return nil if @finished
    @waiter.push(Fiber.current)
    Fiber.yield
  end
  return @q.shift
end

#deq_nonblockObject



35
36
37
# File 'lib/pwrake/branch/fiber_queue.rb', line 35

def deq_nonblock
  @q.shift
end

#enq(x) ⇒ Object



17
18
19
20
21
22
23
24
# File 'lib/pwrake/branch/fiber_queue.rb', line 17

def enq(x)
  if @finished
    raise FiberQueueError,"cannot enq to already finished queue"
  end
  @q.push(x)
  f = @waiter.shift
  f.resume if f
end

#finishObject



39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/pwrake/branch/fiber_queue.rb', line 39

def finish
  @finished = true
  while f = @waiter.shift
    begin
      f.resume
    rescue => exc
      if @log
        @log.error(([exc.to_s]+exc.backtrace).join("\n"))
      end
    end
  end
end