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



135
136
137
# File 'lib/god/driver.rb', line 135

def clear
  @events.clear
end

#empty?Boolean

Returns:

  • (Boolean)


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

def empty?
  @que.empty?
end

#lengthObject Also known as: size



139
140
141
# File 'lib/god/driver.rb', line 139

def length
  @events.length
end

#num_waitingObject



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

def num_waiting
  @waiting.size
end

#popObject 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
# 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
        sleep @events.first.at - Time.now
        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



110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/god/driver.rb', line 110

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

#shutdownObject

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