Sha256: 32af5665a5834311e073f8d198dec8697299eea9c06c83eb7536568dc68f9449

Contents?: true

Size: 787 Bytes

Versions: 1

Compression:

Stored size: 787 Bytes

Contents

class WaitingPrioQueue
  def initialize(&block)
    @mutex=Mutex.new
    if block
      @block=block
    else
      @block=lambda {|a,b|a<=>b}
    end
    @buf=[]
  end
  
  def push(value)
    @mutex.synchronize {
      @buf<<value
    }
    @waitThreads.each{|th|th.wakeup}
    @waitThreads.uniq!
    pp "PUSH",@waitThreads
  end
  def <<(value)
    push(value)
  end
    
  def pop
    @waitThreads<<Thread.current
    loop do
      Thread.stop if empty?
      @mutex.synchronize {
        unless empty?
          value=getBest
          @waitThreads.delete(self)
          pp "POP",@waitThreads
          @waitThreads.uniq!
          return value
        end
        
      }
    end
  end
  
  private
  def getBest
    best=@buf.min(&block)
    @buf.delete(best)
    best
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
appswarm-0.0.1 lib/appswarm/tools/waiting_prio_queue.rb