Class: SyncQueue
- Inherits:
-
Object
- Object
- SyncQueue
- Defined in:
- lib/devp2p/sync_queue.rb
Overview
A naive synchronized queue for Celluloid actors.
Instance Method Summary collapse
- #clear ⇒ Object
- #deq(non_block = false) ⇒ Object
- #empty? ⇒ Boolean
- #enq(obj) ⇒ Object (also: #<<)
-
#initialize ⇒ SyncQueue
constructor
A new instance of SyncQueue.
- #length ⇒ Object (also: #size)
-
#num_waiting ⇒ Object
Returns the number of threads waiting on the queue.
-
#peek(non_block = false) ⇒ Object
Same as pop except it will not remove the element from queue, just peek.
Constructor Details
#initialize ⇒ SyncQueue
Returns a new instance of SyncQueue.
8 9 10 11 12 |
# File 'lib/devp2p/sync_queue.rb', line 8 def initialize @queue = [] @num_waiting = 0 @cond = Celluloid::Condition.new end |
Instance Method Details
#clear ⇒ Object
63 64 65 |
# File 'lib/devp2p/sync_queue.rb', line 63 def clear @queue.clear end |
#deq(non_block = false) ⇒ Object
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/devp2p/sync_queue.rb', line 20 def deq(non_block=false) loop do if @queue.empty? if non_block raise ThreadError, 'queue empty' else begin @num_waiting += 1 @cond.wait ensure @num_waiting -= 1 end end else return @queue.shift end end end |
#empty? ⇒ Boolean
59 60 61 |
# File 'lib/devp2p/sync_queue.rb', line 59 def empty? @queue.empty? end |
#enq(obj) ⇒ Object Also known as: <<
14 15 16 17 |
# File 'lib/devp2p/sync_queue.rb', line 14 def enq(obj) @queue.push obj @cond.signal end |
#length ⇒ Object Also known as: size
67 68 69 |
# File 'lib/devp2p/sync_queue.rb', line 67 def length @queue.length end |
#num_waiting ⇒ Object
Returns the number of threads waiting on the queue.
73 74 75 |
# File 'lib/devp2p/sync_queue.rb', line 73 def num_waiting @num_waiting end |
#peek(non_block = false) ⇒ Object
Same as pop except it will not remove the element from queue, just peek.
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/devp2p/sync_queue.rb', line 40 def peek(non_block=false) loop do if @queue.empty? if non_block raise ThreadError, 'queue empty' else begin @num_waiting += 1 @cond.wait ensure @num_waiting -= 1 end end else return @queue[0] end end end |