Class: TimeoutQueue
- Inherits:
-
Object
- Object
- TimeoutQueue
- Defined in:
- lib/timeout_queue.rb,
lib/timeout_queue/version.rb
Constant Summary collapse
- VERSION =
"0.1.0"
Instance Method Summary collapse
- #clear ⇒ Object
-
#delete(object) ⇒ Object
delete an object from the queue before it can be popped.
- #empty? ⇒ Boolean
-
#initialize(**opt) ⇒ TimeoutQueue
constructor
A new instance of TimeoutQueue.
-
#pop(non_block = false, **opts) ⇒ Object
(also: #shift, #deq)
retrieve next object from queue.
-
#push(object, **opt) ⇒ Object
(also: #<<, #enq)
push object into end of queue.
- #size ⇒ Object (also: #length)
-
#unshift(object) ⇒ Object
push object into front of the queue.
Constructor Details
#initialize(**opt) ⇒ TimeoutQueue
Returns a new instance of TimeoutQueue.
3 4 5 6 7 8 9 |
# File 'lib/timeout_queue.rb', line 3 def initialize(**opt) @queue = [] @mutex = Mutex.new @received = ConditionVariable.new end |
Instance Method Details
#clear ⇒ Object
101 102 103 104 105 106 |
# File 'lib/timeout_queue.rb', line 101 def clear with_mutex do @queue.clear end self end |
#delete(object) ⇒ Object
delete an object from the queue before it can be popped
38 39 40 41 42 43 44 45 46 |
# File 'lib/timeout_queue.rb', line 38 def delete(object) with_mutex do @queue.delete(object) end object end |
#empty? ⇒ Boolean
93 94 95 |
# File 'lib/timeout_queue.rb', line 93 def empty? @queue.send __method__ end |
#pop(non_block = false, **opts) ⇒ Object Also known as: shift, deq
retrieve next object from queue
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/timeout_queue.rb', line 57 def pop(non_block=false, **opts) timeout = opts[:timeout] if timeout end_time = Time.now + timeout.to_f end with_mutex do while @queue.empty? and not(non_block) if timeout break unless ((time_now = Time.now) < end_time) @received.wait(@mutex, end_time - time_now) else @received.wait(@mutex) end end raise ThreadError unless not @queue.empty? @queue.shift end end |
#push(object, **opt) ⇒ Object Also known as: <<, enq
push object into end of queue
16 17 18 19 20 |
# File 'lib/timeout_queue.rb', line 16 def push(object, **opt) __push do @queue.send(__method__, object) end end |
#size ⇒ Object Also known as: length
97 98 99 |
# File 'lib/timeout_queue.rb', line 97 def size @queue.send __method__ end |
#unshift(object) ⇒ Object
push object into front of the queue
27 28 29 30 31 |
# File 'lib/timeout_queue.rb', line 27 def unshift(object) __push do @queue.send(__method__, object) end end |