Class: Garcon::Stash::Queue

Inherits:
Object show all
Defined in:
lib/garcon/stash/queue.rb

Direct Known Subclasses

Journal

Instance Method Summary collapse

Constructor Details

#initializeQueue

Returns a new instance of Queue.



23
24
25
26
27
28
# File 'lib/garcon/stash/queue.rb', line 23

def initialize
  @queue, @full, @empty = [], [], []
  @stop = false
  @heartbeat = Thread.new(&method(:heartbeat))
  @heartbeat.priority = -9
end

Instance Method Details

#<<(x) ⇒ Object



30
31
32
33
34
# File 'lib/garcon/stash/queue.rb', line 30

def <<(x)
  @queue << x
  thread = @full.first
  thread.wakeup if thread
end

#closeObject



67
68
69
70
# File 'lib/garcon/stash/queue.rb', line 67

def close
  @stop = true
  @heartbeat.join
end

#firstObject



44
45
46
47
48
49
50
51
52
53
54
# File 'lib/garcon/stash/queue.rb', line 44

def first
  while @queue.empty?
    begin
      @full << Thread.current
      Thread.stop while @queue.empty?
    ensure
      @full.delete(Thread.current)
    end
  end
  @queue.first
end

#flushObject



56
57
58
59
60
61
62
63
64
65
# File 'lib/garcon/stash/queue.rb', line 56

def flush
  until @queue.empty?
    begin
      @empty << Thread.current
      Thread.stop until @queue.empty?
    ensure
      @empty.delete(Thread.current)
    end
  end
end

#popObject



36
37
38
39
40
41
42
# File 'lib/garcon/stash/queue.rb', line 36

def pop
  @queue.shift
  if @queue.empty?
    thread = @empty.first
    thread.wakeup if thread
  end
end