Class: EasyBunnyRPC::TimedQueue
- Inherits:
-
Object
- Object
- EasyBunnyRPC::TimedQueue
- Defined in:
- lib/easy_bunny_rpc/timed_queue.rb
Instance Method Summary collapse
-
#initialize ⇒ TimedQueue
constructor
A new instance of TimedQueue.
- #pop_with_timeout(timeout = 0.5) ⇒ Object
- #push(item) ⇒ Object
Constructor Details
#initialize ⇒ TimedQueue
Returns a new instance of TimedQueue.
6 7 8 9 10 |
# File 'lib/easy_bunny_rpc/timed_queue.rb', line 6 def initialize @array = Array.new @mutex = Mutex.new @cv = ConditionVariable.new end |
Instance Method Details
#pop_with_timeout(timeout = 0.5) ⇒ Object
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/easy_bunny_rpc/timed_queue.rb', line 19 def pop_with_timeout(timeout=0.5) timeout_at = Time.now + timeout @mutex.synchronize do loop do if @array.empty? remaining = timeout_at - Time.now raise(Timeout::Error, "Waited #{timeout} seconds to pop") if(remaining <= 0) # unlocks mutex, waits for remaining seconds, locks mutex on wake-up @cv.wait(@mutex, remaining) else return @array.pop end end end end |
#push(item) ⇒ Object
12 13 14 15 16 17 |
# File 'lib/easy_bunny_rpc/timed_queue.rb', line 12 def push(item) @mutex.synchronize do @array.push(item) @cv.signal end end |