Class: ThreadStorm::Queue

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

Overview

:nodoc:

Instance Method Summary collapse

Constructor Details

#initializeQueue

Returns a new instance of Queue.



6
7
8
9
10
11
# File 'lib/thread_storm/queue.rb', line 6

def initialize
  @lock  = Mutex.new
  @cond  = ConditionVariable.new
  @die   = false
  @queue = []
end

Instance Method Details

#die!Object

Clears the queue. Any calls to #pop will immediately return with nil.



30
31
32
33
34
35
36
# File 'lib/thread_storm/queue.rb', line 30

def die!
  @lock.synchronize do
    @die = true
    @queue.clear
    @cond.broadcast # Wake up any threads waiting on #pop.
  end
end

#popObject

Pops a value of the queue. Blocks if the queue is empty.



22
23
24
25
26
27
# File 'lib/thread_storm/queue.rb', line 22

def pop
  @lock.synchronize do
    @cond.wait(@lock) if @queue.empty? and not die?
    @queue.pop
  end
end

#push(value) ⇒ Object

Pushes a value on the queue and wakes up the next thread waiting on #pop.



14
15
16
17
18
19
# File 'lib/thread_storm/queue.rb', line 14

def push(value)
  @lock.synchronize do 
    @queue.push(value)
    @cond.signal # Wake up next thread waiting on #pop.
  end
end