Class: Rack::Utils::QueueWithTimeout

Inherits:
Object
  • Object
show all
Defined in:
lib/utils.rb

Overview

Instance Method Summary collapse

Constructor Details

#initializeQueueWithTimeout

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

#lengthObject



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