Sha256: 834eb35a7406e012ebef91b688a86b903f0f4f4612a142d33becb80b7c8696b2

Contents?: true

Size: 655 Bytes

Versions: 1

Compression:

Stored size: 655 Bytes

Contents

require 'thread'
require 'timeout'

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

  def push(obj)
    @mutex.synchronize do
      @que.push obj
      @resource.broadcast
    end
  end
  alias_method :<<, :push

  def timed_pop(timeout=0.5)
    deadline = Time.now + timeout
    @mutex.synchronize do
      loop do
        return @que.shift unless @que.empty?
        raise Timeout::Error if Time.now > deadline
        @resource.wait(@mutex, timeout)
      end
    end
  end

  def empty?
    @que.empty?
  end

  def clear
    @que.clear
  end

  def length
    @que.length
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
connection_pool-0.0.3 lib/connection_pool/timed_queue.rb