Sha256: afb99618dc0c0af242f84e10b3fae6ae9c93127f6245525b080095086693fd1d

Contents?: true

Size: 805 Bytes

Versions: 1

Compression:

Stored size: 805 Bytes

Contents

module SauceTunnel
  # A really basic message queue, which unlike the queue in stdlib makes it
  # possible to specify a timeout for `pop`.
  class SimpleQueue
    def initialize
      @mutex = Mutex.new
      @queue = []
      @received = ConditionVariable.new
    end

    def push(value)
      @mutex.synchronize do
        @queue << value
        @received.signal
      end
    end

    def pop(timeout = nil)
      @mutex.synchronize do
        start_time = Time.now
        loop do
          if @queue.empty?
            elapsed_time = Time.now - start_time

            raise ThreadError, "queue empty" if elapsed_time >= timeout

            @received.wait(@mutex, timeout - elapsed_time)
          else
            break
          end
        end
        @queue.shift
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
sauce_tunnel-0.1.0 lib/sauce_tunnel/simple_queue.rb