Class: God::DriverEventQueue
- Inherits:
-
Object
- Object
- God::DriverEventQueue
- Defined in:
- lib/god/driver.rb
Instance Method Summary collapse
- #clear ⇒ Object
- #empty? ⇒ Boolean
-
#initialize ⇒ DriverEventQueue
constructor
A new instance of DriverEventQueue.
- #length ⇒ Object (also: #size)
- #num_waiting ⇒ Object
-
#pop ⇒ Object
(also: #shift, #deq)
Sleep until the queue has something due.
-
#push(event) ⇒ Object
(also: #<<, #enq)
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
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
#clear ⇒ Object
136 137 138 |
# File 'lib/god/driver.rb', line 136 def clear @events.clear end |
#empty? ⇒ Boolean
132 133 134 |
# File 'lib/god/driver.rb', line 132 def empty? @que.empty? end |
#length ⇒ Object Also known as: size
140 141 142 |
# File 'lib/god/driver.rb', line 140 def length @events.length end |
#num_waiting ⇒ Object
146 147 148 |
# File 'lib/god/driver.rb', line 146 def num_waiting @waiting.size end |
#pop ⇒ Object Also known as: shift, deq
Sleep until the queue has something due
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 |
# File 'lib/god/driver.rb', line 84 def pop begin while (Thread.critical = true; @events.empty? or !@events.first.due?) @waiting.push Thread.current if @events.empty? raise ThreadError, "queue empty" if @shutdown Thread.stop else Thread.critical = false delay = @events.first.at - Time.now sleep delay if delay > 0 Thread.critical = true end end @events.shift ensure Thread.critical = false end 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
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 |
# File 'lib/god/driver.rb', line 111 def push(event) Thread.critical = true @events << event @events.sort! begin t = @waiting.shift if @events.first == event t.wakeup if t rescue ThreadError retry ensure Thread.critical = false end begin t.run if t rescue ThreadError end end |
#shutdown ⇒ Object
Wake any sleeping threads after setting the sentinel
69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/god/driver.rb', line 69 def shutdown @shutdown = true begin Thread.critical = true @waiting.each do |t| t.run end ensure Thread.critical = false end end |