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.



64
65
66
67
68
69
70
71
# File 'lib/god/driver.rb', line 64

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

Instance Method Details

#clearObject



125
126
127
# File 'lib/god/driver.rb', line 125

def clear
  @events.clear
end

#empty?Boolean

Returns:

  • (Boolean)


121
122
123
# File 'lib/god/driver.rb', line 121

def empty?
  @events.empty?
end

#lengthObject Also known as: size



129
130
131
# File 'lib/god/driver.rb', line 129

def length
  @events.length
end

#popObject Also known as: shift, deq

Sleep until the queue has something due



86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/god/driver.rb', line 86

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

    @events.shift
  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



107
108
109
110
111
112
113
114
115
116
# File 'lib/god/driver.rb', line 107

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



76
77
78
79
80
81
# File 'lib/god/driver.rb', line 76

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