Class: IO::Event::Timers
- Inherits:
-
Object
- Object
- IO::Event::Timers
- Defined in:
- lib/io/event/timers.rb
Overview
An efficient sorted set of timers.
Defined Under Namespace
Classes: Handle
Constant Summary collapse
- COMPACT_MINIMUM_COUNT =
128
Instance Method Summary collapse
-
#after(offset, &block) ⇒ Object
Schedule a block to be called after a specific time offset, relative to the current time as returned by #now.
-
#cancelled!(handle) ⇒ Object
Track cancelled timers that are still retained in the heap.
-
#fire(now = self.now) ⇒ Object
Fire all timers that are ready to fire.
-
#initialize ⇒ Timers
constructor
Initialize the timers.
- #now ⇒ Object
-
#schedule(time, block) ⇒ Object
Schedule a block to be called at a specific time in the future.
- #size ⇒ Object
-
#wait_interval(now = self.now) ⇒ Object
Compute the time interval until the next timer fires.
Constructor Details
#initialize ⇒ Timers
Initialize the timers.
82 83 84 85 86 |
# File 'lib/io/event/timers.rb', line 82 def initialize @heap = PriorityHeap.new @scheduled = [] @cancelled = 0 end |
Instance Method Details
#after(offset, &block) ⇒ Object
Schedule a block to be called after a specific time offset, relative to the current time as returned by #now.
111 112 113 |
# File 'lib/io/event/timers.rb', line 111 def after(offset, &block) schedule(self.now + offset.to_f, block) end |
#cancelled!(handle) ⇒ Object
Track cancelled timers that are still retained in the heap.
211 212 213 |
# File 'lib/io/event/timers.rb', line 211 def cancelled!(handle) @cancelled += 1 end |
#fire(now = self.now) ⇒ Object
Fire all timers that are ready to fire.
141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 |
# File 'lib/io/event/timers.rb', line 141 def fire(now = self.now) # Flush scheduled timers into the heap: flush! # Get the earliest timer: while handle = @heap.peek if handle.cancelled? @heap.pop handle.removed! @cancelled -= 1 if @cancelled > 0 elsif handle.time <= now # Remove the earliest timer from the heap: @heap.pop handle.removed! # Call the block: handle.call(now) else break end end end |
#now ⇒ Object
134 135 136 |
# File 'lib/io/event/timers.rb', line 134 def now ::Process.clock_gettime(::Process::CLOCK_MONOTONIC) end |
#schedule(time, block) ⇒ Object
Schedule a block to be called at a specific time in the future.
99 100 101 102 103 104 105 |
# File 'lib/io/event/timers.rb', line 99 def schedule(time, block) handle = Handle.new(time, block) @scheduled << handle return handle end |
#size ⇒ Object
89 90 91 92 93 |
# File 'lib/io/event/timers.rb', line 89 def size flush! return @heap.size end |
#wait_interval(now = self.now) ⇒ Object
Compute the time interval until the next timer fires.
119 120 121 122 123 124 125 126 127 128 129 130 131 |
# File 'lib/io/event/timers.rb', line 119 def wait_interval(now = self.now) flush! while handle = @heap.peek if handle.cancelled? @heap.pop handle.removed! @cancelled -= 1 if @cancelled > 0 else return handle.time - now end end end |