Class: God::DriverEventQueue

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

Overview

The DriverEventQueue is a simple queue that holds TimedEvent instances in order to maintain the schedule of upcoming events.

Instance Method Summary collapse

Constructor Details

#initializeDriverEventQueue

Initialize a DriverEventQueue.



95
96
97
98
99
100
# File 'lib/god/driver.rb', line 95

def initialize
  @shutdown = false
  @events = []
  @monitor = Monitor.new
  @resource = @monitor.new_cond
end

Instance Method Details

#clearObject

Clear the queue.

Returns nothing.



153
154
155
# File 'lib/god/driver.rb', line 153

def clear
  @events.clear
end

#empty?Boolean

Returns true if the queue is empty, false if not.

Returns:

  • (Boolean)


146
147
148
# File 'lib/god/driver.rb', line 146

def empty?
  @events.empty?
end

#lengthObject Also known as: size

Returns the Integer length of the queue.



158
159
160
# File 'lib/god/driver.rb', line 158

def length
  @events.length
end

#popObject

Wait until the queue has something due, pop it off the queue, and return it.

Returns the popped event.



116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/god/driver.rb', line 116

def pop
  @monitor.synchronize do
    if @events.empty?
      raise ThreadError, "queue empty" if @shutdown
      @resource.wait
    else
      delay = @events.first.at - Time.now
      @resource.wait(delay) if delay > 0
    end

    @events.shift
  end
end

#push(event) ⇒ Object

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

Returns nothing.



134
135
136
137
138
139
140
141
142
143
# File 'lib/god/driver.rb', line 134

def push(event)
  @monitor.synchronize do
    @events << event
    @events.sort!

    # If we've sorted the events and found the one we're adding is at
    # the front, it will likely need to run before the next due date.
    @resource.signal if @events.first == event
  end
end

#shutdownObject

Wake any sleeping threads after setting the sentinel.

Returns nothing.



105
106
107
108
109
110
# File 'lib/god/driver.rb', line 105

def shutdown
  @shutdown = true
  @monitor.synchronize do
    @resource.broadcast
  end
end