class Empathy::EM::Queue
A Empathy
equivalent to ::Queue from thread.rb
Public Class Methods
new()
click to toggle source
Creates a new queue
# File lib/empathy/em/queue.rb, line 8 def initialize @mutex = Mutex.new() @cv = ConditionVariable.new() @q = [] @waiting = 0 end
Public Instance Methods
clear()
click to toggle source
Removes all objects from the queue @return [void]
# File lib/empathy/em/queue.rb, line 54 def clear @q.clear end
empty?()
click to toggle source
@return [true] if the queue is empty @return [false] otherwise
# File lib/empathy/em/queue.rb, line 48 def empty? @q.empty? end
length()
click to toggle source
@return [Fixnum] the length of the queue
# File lib/empathy/em/queue.rb, line 41 def length @q.length end
Also aliased as: size
num_waiting()
click to toggle source
@return [Fixnum] the number of fibers waiting on the queue
# File lib/empathy/em/queue.rb, line 59 def num_waiting @waiting end
pop(non_block=false)
click to toggle source
Retrieves data from the queue. @param [Boolean] non_block @raise FiberError if non_block is true and the queue is empty
# File lib/empathy/em/queue.rb, line 27 def pop(non_block=false) raise FiberError, "queue empty" if non_block && empty? if empty? @waiting += 1 @mutex.synchronize { @cv.wait(@mutex) if empty? } @waiting -= 1 end # array.pop is like a stack, we're a FIFO @q.shift end
push(obj)
click to toggle source
@param [Object] obj @return [void]
# File lib/empathy/em/queue.rb, line 17 def push(obj) @q << obj @mutex.synchronize { @cv.signal } end