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



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

def clear
  @events.clear
end

#empty?Boolean

Returns:

  • (Boolean)


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

def empty?
  @que.empty?
end

#lengthObject Also known as: size



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

def length
  @events.length
end

#num_waitingObject



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

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
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

#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