Sha256: ce13df2db0b7dfb67d1aa7008185b93fd7c2b99b010818ce1e7b878b1b2b854c

Contents?: true

Size: 708 Bytes

Versions: 20

Compression:

Stored size: 708 Bytes

Contents

# http://spin.atomicobject.com/2014/07/07/ruby-queue-pop-timeout/
class QueueWithTimeout
  def initialize
    @mutex = Mutex.new
    @queue = []
    @recieved = ConditionVariable.new
  end

  def push(x)
    @mutex.synchronize do
      @queue << x
      @recieved.signal
    end
  end

  def <<(x)
    push(x)
  end

  def pop(non_block = false)
    pop_with_timeout(non_block ? 0 : nil)
  end

  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
        fail ThreadError, 'queue empty' if @queue.empty?
      end
      @queue.shift
    end
  end
end

Version data entries

20 entries across 20 versions & 2 rubygems

Version Path
slack-ruby-client-bhe-0.5.5 spec/support/queue_with_timeout.rb
slack-ruby-client-bhe-0.5.4 spec/support/queue_with_timeout.rb
slack-ruby-client-0.10.0 spec/support/queue_with_timeout.rb
slack-ruby-client-0.9.1 spec/support/queue_with_timeout.rb
slack-ruby-client-0.9.0 spec/support/queue_with_timeout.rb
slack-ruby-client-0.8.1 spec/support/queue_with_timeout.rb
slack-ruby-client-0.8.0 spec/support/queue_with_timeout.rb
slack-ruby-client-0.7.9 spec/support/queue_with_timeout.rb
slack-ruby-client-0.7.8 spec/support/queue_with_timeout.rb
slack-ruby-client-0.7.7 spec/support/queue_with_timeout.rb
slack-ruby-client-0.7.6 spec/support/queue_with_timeout.rb
slack-ruby-client-0.7.5 spec/support/queue_with_timeout.rb
slack-ruby-client-0.7.4 spec/support/queue_with_timeout.rb
slack-ruby-client-0.7.3 spec/support/queue_with_timeout.rb
slack-ruby-client-0.7.2 spec/support/queue_with_timeout.rb
slack-ruby-client-0.7.1 spec/support/queue_with_timeout.rb
slack-ruby-client-0.7.0 spec/support/queue_with_timeout.rb
slack-ruby-client-0.6.0 spec/support/queue_with_timeout.rb
slack-ruby-client-0.5.4 spec/support/queue_with_timeout.rb
slack-ruby-client-0.5.3 spec/support/queue_with_timeout.rb