RobotCore 1.0
WPILib port to raspberry Pi
Loading...
Searching...
No Matches
PeriodicPriorityQueue.java
1// Copyright (c) FIRST and other WPILib contributors.
2// Open Source Software; you can modify and/or share it under the terms of
3// the WPILib BSD license file in the root directory of this project.
4
5package org.wpilib.internal;
6
7import java.util.Collection;
8import java.util.PriorityQueue;
9import java.util.concurrent.atomic.AtomicLong;
10import org.wpilib.hardware.hal.NotifierJNI;
11import org.wpilib.system.RobotController;
12import org.wpilib.util.WPIUtilJNI;
13
26 private final PriorityQueue<Callback> m_queue;
27
28 private long m_loopStartTimeNanos;
29
32 m_queue = new PriorityQueue<>();
33 }
34
45 public Callback add(Runnable func, long timestamp, double periodSeconds, double offsetSeconds) {
46 Callback callback = new Callback(func, timestamp, periodSeconds, offsetSeconds);
47 add(callback);
48 return callback;
49 }
50
60 public Callback add(Runnable func, long timestamp, double periodSeconds) {
61 Callback callback = new Callback(func, timestamp, periodSeconds);
62 m_queue.add(callback);
63 return callback;
64 }
65
74 public Callback add(Runnable func, double periodSeconds, double offsetSeconds) {
75 return add(func, RobotController.getMonotonicTime(), periodSeconds, offsetSeconds);
76 }
77
85 public Callback add(Runnable func, double periodSeconds) {
86 return add(func, RobotController.getMonotonicTime(), periodSeconds);
87 }
88
94 public void add(Callback callback) {
95 m_queue.add(callback);
96 }
97
103 public void addAll(Collection<Callback> callbacks) {
104 m_queue.addAll(callbacks);
105 }
106
112 public void remove(Runnable func) {
113 m_queue.removeIf(callback -> callback.func.equals(func));
114 }
115
121 public void remove(Callback callback) {
122 m_queue.remove(callback);
123 }
124
130 public void removeAll(Collection<Callback> callbacks) {
131 m_queue.removeAll(callbacks);
132 }
133
135 public void clear() {
136 m_queue.clear();
137 }
138
160 public boolean runCallbacks(int notifier) {
161 var callback = m_queue.poll();
162 if (callback == null) {
163 throw new IllegalStateException(
164 "No callbacks to run! Did you make sure to call add() first?");
165 }
166
167 NotifierJNI.setNotifierAlarm(notifier, callback.expirationTime, 0, true, true);
168
169 try {
170 WPIUtilJNI.waitForObject(notifier);
171 } catch (InterruptedException ex) {
172 return false;
173 }
174
175 m_loopStartTimeNanos = RobotController.getMonotonicTime();
176
177 callback.func.run();
178
179 // Increment the expiration time by the number of full periods it's behind
180 // plus one to avoid rapid repeat fires from a large loop overrun. We
181 // assume m_loopStartTime >= expirationTime rather than checking for it since
182 // the callback wouldn't be running otherwise.
183 callback.expirationTime +=
184 callback.period
185 + (m_loopStartTimeNanos - callback.expirationTime) / callback.period * callback.period;
186 m_queue.add(callback);
187
188 // Process all other callbacks that are ready to run
189 while (m_queue.peek().expirationTime <= m_loopStartTimeNanos) {
190 callback = m_queue.poll();
191
192 callback.func.run();
193
194 callback.expirationTime +=
195 callback.period
196 + (m_loopStartTimeNanos - callback.expirationTime)
197 / callback.period
198 * callback.period;
199 m_queue.add(callback);
200 }
201
202 return true;
203 }
204
212 public long getLoopStartTime() {
213 return m_loopStartTimeNanos;
214 }
215
223 public static class Callback implements Comparable<Callback> {
225 private static final AtomicLong s_nextId = new AtomicLong(1);
226
228 public final Runnable func;
229
231 public final long period;
232
234 public long expirationTime;
235
240 public final long id;
241
250 public Callback(Runnable func, long startTime, long period, long offset) {
251 this.func = func;
252 this.period = period;
253 this.expirationTime =
254 startTime
255 + offset
256 + (1 + (RobotController.getMonotonicTime() - startTime - offset) / this.period)
257 * this.period;
258 this.id = s_nextId.getAndIncrement();
259 }
260
269 public Callback(Runnable func, long timestamp, double periodSeconds, double offsetSeconds) {
270 this(func, timestamp, (long) (periodSeconds * 1e9), (long) (offsetSeconds * 1e9));
271 }
272
280 public Callback(Runnable func, long timestamp, double periodSeconds) {
281 this(func, timestamp, (long) (periodSeconds * 1e9), 0);
282 }
283
290 @Override
291 public boolean equals(Object rhs) {
292 return rhs instanceof Callback callback && id == callback.id;
293 }
294
300 @Override
301 public int hashCode() {
302 return Long.hashCode(id);
303 }
304
314 @Override
315 public int compareTo(Callback rhs) {
316 // Elements with sooner expiration times are sorted as lesser. The head of
317 // Java's PriorityQueue is the least element.
318 int expTimeDiff = Long.compare(expirationTime, rhs.expirationTime);
319 return expTimeDiff != 0 ? expTimeDiff : Long.compare(id, rhs.id);
320 }
321 }
322}
Callback add(Runnable func, long timestamp, double periodSeconds, double offsetSeconds)
void addAll(Collection< Callback > callbacks)
Callback add(Runnable func, long timestamp, double periodSeconds)
Callback add(Runnable func, double periodSeconds, double offsetSeconds)
void removeAll(Collection< Callback > callbacks)
Callback add(Runnable func, double periodSeconds)