Class: CopyTunerClient::QueueWithTimeout
- Inherits:
-
Object
- Object
- CopyTunerClient::QueueWithTimeout
- Defined in:
- lib/copy_tuner_client/queue_with_timeout.rb
Overview
Instance Method Summary collapse
- #<<(item) ⇒ Object
-
#initialize ⇒ QueueWithTimeout
constructor
A new instance of QueueWithTimeout.
- #pop(non_block = false) ⇒ Object
- #pop_with_timeout(timeout = nil) ⇒ Object
- #uniq_push(item) ⇒ Object
Constructor Details
#initialize ⇒ QueueWithTimeout
Returns a new instance of QueueWithTimeout.
4 5 6 7 8 |
# File 'lib/copy_tuner_client/queue_with_timeout.rb', line 4 def initialize @mutex = Mutex.new @queue = [] @received = ConditionVariable.new end |
Instance Method Details
#<<(item) ⇒ Object
10 11 12 13 14 15 |
# File 'lib/copy_tuner_client/queue_with_timeout.rb', line 10 def <<(item) @mutex.synchronize do @queue << item @received.signal end end |
#pop(non_block = false) ⇒ Object
26 27 28 |
# File 'lib/copy_tuner_client/queue_with_timeout.rb', line 26 def pop(non_block = false) pop_with_timeout(non_block ? 0 : nil) end |
#pop_with_timeout(timeout = nil) ⇒ Object
30 31 32 33 34 35 36 37 38 39 |
# File 'lib/copy_tuner_client/queue_with_timeout.rb', line 30 def pop_with_timeout(timeout = nil) @mutex.synchronize do wait_for_item(timeout) # if we're still empty after the timeout, raise exception raise ThreadError, 'queue empty' if @queue.empty? @queue.shift end end |
#uniq_push(item) ⇒ Object
17 18 19 20 21 22 23 24 |
# File 'lib/copy_tuner_client/queue_with_timeout.rb', line 17 def uniq_push(item) @mutex.synchronize do unless @queue.member?(item) @queue << item @received.signal end end end |