Class: Rack::Utils::QueueWithTimeout
- Inherits:
-
Object
- Object
- Rack::Utils::QueueWithTimeout
- Defined in:
- lib/utils.rb
Overview
Instance Method Summary collapse
- #<<(x) ⇒ Object
-
#initialize ⇒ QueueWithTimeout
constructor
A new instance of QueueWithTimeout.
- #length ⇒ Object
- #pop(non_block = false) ⇒ Object
- #pop_with_timeout(timeout = nil) ⇒ Object
Constructor Details
#initialize ⇒ QueueWithTimeout
Returns a new instance of QueueWithTimeout.
4 5 6 7 8 |
# File 'lib/utils.rb', line 4 def initialize @mutex = Mutex.new @queue = [] @recieved = ConditionVariable.new end |
Instance Method Details
#<<(x) ⇒ Object
10 11 12 13 14 15 |
# File 'lib/utils.rb', line 10 def <<(x) @mutex.synchronize do @queue << x @recieved.signal end end |
#length ⇒ Object
17 18 19 |
# File 'lib/utils.rb', line 17 def length @queue.length end |
#pop(non_block = false) ⇒ Object
21 22 23 |
# File 'lib/utils.rb', line 21 def pop(non_block = false) pop_with_timeout(non_block ? 0 : nil) end |
#pop_with_timeout(timeout = nil) ⇒ Object
25 26 27 28 29 30 31 32 33 34 |
# File 'lib/utils.rb', line 25 def pop_with_timeout(timeout = nil) @mutex.synchronize do if @queue.empty? @recieved.wait(@mutex, timeout) if timeout != 0 #if we're still empty after the timeout, raise exception raise ThreadError, "queue empty" if @queue.empty? end @queue.shift end end |