Sha256: f203cd989570449af345171eade6d6b6e6ba1dac094d1029a5d43628a171bf1e

Contents?: true

Size: 785 Bytes

Versions: 9

Compression:

Stored size: 785 Bytes

Contents

require 'thread'
require 'timeout'

class TimedQueue
  def initialize
    @que = []
    @waiting = []
    @mutex = Mutex.new
    @resource = ConditionVariable.new
  end

  def push(obj)
    @mutex.synchronize do
      @que.push obj
      @resource.signal
    end
  end
  alias << push

  def timed_pop(timeout=0.5)
    while true
      @mutex.synchronize do
        @waiting.delete(Thread.current)
        if @que.empty?
          @waiting.push Thread.current
          @resource.wait(@mutex, timeout)
          raise TimeoutError if @que.empty?
        else
          retval = @que.shift
          @resource.signal
          return retval
        end
      end
    end
  end

  def empty?
    @que.empty?
  end

  def clear
    @que.clear
  end

  def length
    @que.length
  end
end

Version data entries

9 entries across 6 versions & 2 rubygems

Version Path
girl_friday-0.9.4 lib/girl_friday/timed_queue.rb
girl_friday-0.9.4 test/timed_queue.rb
girl_friday-0.9.3 lib/girl_friday/timed_queue.rb
girl_friday-0.9.3 test/timed_queue.rb
girl_friday-0.9.2 lib/girl_friday/timed_queue.rb
girl_friday-0.9.2 test/timed_queue.rb
connection_pool-0.0.1 lib/connection_pool/timed_queue.rb
girl_friday-0.9.1 test/timed_queue.rb
girl_friday-0.9.0 test/timed_queue.rb