Class: ConcurrentQueue::Listener

Inherits:
Object
  • Object
show all
Defined in:
lib/concurrent_queue/listener.rb

Instance Method Summary collapse

Constructor Details

#initializeListener

Returns a new instance of Listener.



4
5
6
7
8
9
# File 'lib/concurrent_queue/listener.rb', line 4

def initialize
  @condition_variable = ConditionVariable.new
  @mutex = Mutex.new
  @popped = false
  @item = nil
end

Instance Method Details

#add_queue(queue) ⇒ Object



24
25
26
27
28
# File 'lib/concurrent_queue/listener.rb', line 24

def add_queue(queue)
  raise unless queue.is_a?(ConcurrentQueue)
  @queues << queue
  nil
end

#pop(queues) ⇒ Object



30
31
32
33
34
35
36
37
# File 'lib/concurrent_queue/listener.rb', line 30

def pop(queues)
  queues.each { |queue| queue.add_listener(self) }
  @mutex.synchronize do
    @condition_variable.wait(@mutex) unless @popped
  end
  queues.each { |queue| queue.remove_listener(self) }
  @item
end

#send(item) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/concurrent_queue/listener.rb', line 11

def send(item)
  @mutex.synchronize do
    if @popped
      return false
    else
      @item = item
      @popped = true
      @condition_variable.signal
      return true
    end
  end
end