Sha256: fbcf9234825e4c9b465a5bd5c16c98ddfadd7bee192a67beb2f21dd5ad8c2660

Contents?: true

Size: 539 Bytes

Versions: 4

Compression:

Stored size: 539 Bytes

Contents

require 'thread'

module PromisePool
  class Queue
    def initialize
      @queue = []
      @condv = ConditionVariable.new
    end

    def size
      @queue.size
    end

    def << task
      queue << task
      condv.signal
    end

    def pop mutex, timeout=60
      if queue.empty?
        condv.wait(mutex, timeout)
        queue.shift || lambda{ |_| false } # shutdown idle workers
      else
        queue.shift
      end
    end

    def clear
      queue.clear
    end

    protected
    attr_reader :queue, :condv
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
promise_pool-0.9.2 lib/promise_pool/queue.rb
promise_pool-0.9.1 lib/promise_pool/queue.rb
promise_pool-0.9.0 lib/promise_pool/queue.rb
promise_pool-0.1.0 lib/promise_pool/queue.rb