Class: God::DriverEventQueue
- Inherits:
-
Object
- Object
- God::DriverEventQueue
- 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
-
#clear ⇒ Object
Clear the queue.
-
#empty? ⇒ Boolean
Returns true if the queue is empty, false if not.
-
#initialize ⇒ DriverEventQueue
constructor
Initialize a DriverEventQueue.
-
#length ⇒ Object
(also: #size)
Returns the Integer length of the queue.
-
#pop ⇒ Object
Wait until the queue has something due, pop it off the queue, and return it.
-
#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.
-
#shutdown ⇒ Object
Wake any sleeping threads after setting the sentinel.
Constructor Details
#initialize ⇒ DriverEventQueue
Initialize a DriverEventQueue.
92 93 94 95 96 97 |
# File 'lib/god/driver.rb', line 92 def initialize @shutdown = false @events = [] @monitor = Monitor.new @resource = @monitor.new_cond end |
Instance Method Details
#clear ⇒ Object
Clear the queue.
Returns nothing.
151 152 153 |
# File 'lib/god/driver.rb', line 151 def clear @events.clear end |
#empty? ⇒ Boolean
Returns true if the queue is empty, false if not.
144 145 146 |
# File 'lib/god/driver.rb', line 144 def empty? @events.empty? end |
#length ⇒ Object Also known as: size
Returns the Integer length of the queue.
156 157 158 |
# File 'lib/god/driver.rb', line 156 def length @events.length end |
#pop ⇒ Object
Wait until the queue has something due, pop it off the queue, and return it.
Returns the popped event.
113 114 115 116 117 118 119 120 121 122 123 124 125 126 |
# File 'lib/god/driver.rb', line 113 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.
132 133 134 135 136 137 138 139 140 141 |
# File 'lib/god/driver.rb', line 132 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 |
#shutdown ⇒ Object
Wake any sleeping threads after setting the sentinel.
Returns nothing.
102 103 104 105 106 107 |
# File 'lib/god/driver.rb', line 102 def shutdown @shutdown = true @monitor.synchronize do @resource.broadcast end end |