class LightIO::Library::Queue

Private Class Methods

new() click to toggle source
# File lib/lightio/library/queue.rb, line 6
def initialize
  @queue = []
  @waiters = []
  @close = false
end

Private Instance Methods

<<(object)
Alias for: push
clear() click to toggle source

Removes all objects from the queue.

# File lib/lightio/library/queue.rb, line 84
def clear()
  @queue.clear
  self
end
close() click to toggle source
# File lib/lightio/library/queue.rb, line 12
def close()
  #This is a stub, used for indexing
  @close = true
  @waiters.each {|w| w.transfer nil}
  self
end
closed?() click to toggle source

closed?

Returns true if the queue is closed.

# File lib/lightio/library/queue.rb, line 22
def closed?()
  @close
end
deq(non_block=false)
Alias for: pop
empty?() click to toggle source

empty?

Returns true if the queue is empty.

# File lib/lightio/library/queue.rb, line 79
def empty?()
  @queue.empty?
end
enq(object)
Alias for: push
length() click to toggle source

length size

Returns the length of the queue.

# File lib/lightio/library/queue.rb, line 93
def length()
  @queue.size
end
Also aliased as: size
num_waiting() click to toggle source

Returns the number of threads waiting on the queue.

# File lib/lightio/library/queue.rb, line 99
def num_waiting()
  @waiters.size
end
pop(non_block=false) click to toggle source

pop(non_block=false) deq(non_block=false) shift(non_block=false)

Retrieves data from the queue.

If the queue is empty, the calling thread is suspended until data is pushed onto the queue. If non_block is true, the thread isn't suspended, and an exception is raised.

# File lib/lightio/library/queue.rb, line 57
def pop(non_block=false)
  if @close
    return empty? ? nil : @queue.pop
  end
  if empty?
    if non_block
      raise ThreadError, 'queue empty'
    else
      future = LightIO::Future.new
      @waiters << future
      future.value
    end
  else
    @queue.pop
  end
end
Also aliased as: deq, shift
push(object) click to toggle source

push(object) enq(object) <<(object)

Pushes the given object to the queue.

# File lib/lightio/library/queue.rb, line 31
def push(object)
  raise ClosedQueueError, "queue closed" if @close
  if (waiter = @waiters.shift)
    future = LightIO::Future.new
    LightIO::IOloop.current.add_callback {
      waiter.transfer(object)
      future.transfer
    }
    future.value
  else
    @queue << object
  end
  self
end
Also aliased as: enq, <<
shift(non_block=false)
Alias for: pop
size()
Alias for: length