Class: God::DriverEventQueue

Inherits:
Object
  • Object
show all
Defined in:
lib/god/driver.rb

Instance Method Summary collapse

Constructor Details

#initializeDriverEventQueue

Returns a new instance of DriverEventQueue.



57
58
59
60
61
62
63
64
# File 'lib/god/driver.rb', line 57

def initialize
  @shutdown = false
  @waiting = []
  @events = []
  @waiting.taint
  @events.taint
  self.taint
end

Instance Method Details

#clearObject



121
122
123
# File 'lib/god/driver.rb', line 121

def clear
  @events.clear
end

#empty?Boolean

Returns:

  • (Boolean)


117
118
119
# File 'lib/god/driver.rb', line 117

def empty?
  @que.empty?
end

#lengthObject Also known as: size



125
126
127
# File 'lib/god/driver.rb', line 125

def length
  @events.length
end

#num_waitingObject



131
132
133
# File 'lib/god/driver.rb', line 131

def num_waiting
  @waiting.size
end

#popObject Also known as: shift, deq

Sleep until the queue has something due



79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/god/driver.rb', line 79

def pop
  while (@events.empty? or !@events.first.due?)
    @waiting.push Thread.current
    if @events.empty?
      raise ThreadError, "queue empty" if @shutdown
      Thread.stop
    else
      sleep @events.first.at - Time.now
    end
  end
  @events.shift
end

#push(event) ⇒ Object Also known as: <<, enq

Add an event to the queue, wake any waiters if what we added needs to happen sooner than the next pending event



99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/god/driver.rb', line 99

def push(event)
  @events << event
  @events.sort!
  begin
    t = @waiting.shift if @events.first == event
    t.wakeup if t
  rescue ThreadError
    retry
  end
  begin
    t.run if t
  rescue ThreadError
  end
end

#shutdownObject

Wake any sleeping threads after setting the sentinel



69
70
71
72
73
74
# File 'lib/god/driver.rb', line 69

def shutdown
  @shutdown = true
  @waiting.each do |t|
    t.run
  end
end