Class: LightIO::Library::Queue
- Inherits:
-
Object
- Object
- LightIO::Library::Queue
- Defined in:
- lib/lightio/library/queue.rb
Direct Known Subclasses
Instance Method Summary collapse
-
#clear ⇒ Object
Removes all objects from the queue.
- #close ⇒ Object
-
#closed? ⇒ Boolean
closed?.
-
#empty? ⇒ Boolean
empty?.
-
#initialize ⇒ Queue
constructor
A new instance of Queue.
-
#length ⇒ Object
(also: #size)
length size.
-
#num_waiting ⇒ Object
Returns the number of threads waiting on the queue.
-
#pop(non_block = false) ⇒ Object
(also: #deq, #shift)
pop(non_block=false) deq(non_block=false) shift(non_block=false).
-
#push(object) ⇒ Object
(also: #enq, #<<)
push(object) enq(object) <<(object).
Constructor Details
#initialize ⇒ Queue
Returns a new instance of Queue.
3 4 5 6 7 |
# File 'lib/lightio/library/queue.rb', line 3 def initialize @queue = [] @waiters = [] @close = false end |
Instance Method Details
#clear ⇒ Object
Removes all objects from the queue.
81 82 83 84 |
# File 'lib/lightio/library/queue.rb', line 81 def clear() @queue.clear self end |
#close ⇒ Object
9 10 11 12 13 14 |
# File 'lib/lightio/library/queue.rb', line 9 def close() #This is a stub, used for indexing @close = true @waiters.each {|w| w.transfer nil} self end |
#closed? ⇒ Boolean
closed?
Returns true if the queue is closed.
19 20 21 |
# File 'lib/lightio/library/queue.rb', line 19 def closed?() @close end |
#empty? ⇒ Boolean
empty?
Returns true if the queue is empty.
76 77 78 |
# File 'lib/lightio/library/queue.rb', line 76 def empty?() @queue.empty? end |
#length ⇒ Object Also known as: size
length size
Returns the length of the queue.
90 91 92 |
# File 'lib/lightio/library/queue.rb', line 90 def length() @queue.size end |
#num_waiting ⇒ Object
Returns the number of threads waiting on the queue.
96 97 98 |
# File 'lib/lightio/library/queue.rb', line 96 def num_waiting() @waiters.size end |
#pop(non_block = false) ⇒ Object Also known as: deq, shift
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.
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/lightio/library/queue.rb', line 54 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 |
#push(object) ⇒ Object Also known as: enq, <<
push(object) enq(object) <<(object)
Pushes the given object to the queue.
28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/lightio/library/queue.rb', line 28 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 |