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

<<(obj)
Alias for: push
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
deq(non_block=false)
Alias for: pop
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
enq(obj)
Alias for: push
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
Also aliased as: shift, deq
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
Also aliased as: <<, enq
shift(non_block=false)
Alias for: pop
size()
Alias for: length