Class: WaitingQueue

Inherits:
Queue
  • Object
show all
Defined in:
lib/appswarm/tools/waiting_queue.rb

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ WaitingQueue

Returns a new instance of WaitingQueue.



2
3
4
5
6
# File 'lib/appswarm/tools/waiting_queue.rb', line 2

def initialize(*args)
  super
  @mutex=Mutex.new
  @waitThreads=[]
end

Instance Method Details

#<<(value) ⇒ Object



15
16
17
# File 'lib/appswarm/tools/waiting_queue.rb', line 15

def <<(value)
  push(value)
end

#popObject



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/appswarm/tools/waiting_queue.rb', line 19

def pop
  @waitThreads<<Thread.current
  loop do
    Thread.stop if empty?
    @mutex.synchronize {
      unless empty?
        value=super
        @waitThreads.delete(self)
        @waitThreads.uniq!
        return value
      end
      
    }
  end
end

#push(value) ⇒ Object



8
9
10
11
12
13
14
# File 'lib/appswarm/tools/waiting_queue.rb', line 8

def push(value)
  @mutex.synchronize {
    super
  }
  @waitThreads.each{|th|th.wakeup}
  @waitThreads.uniq!
end