mirror of
https://github.com/wpilibsuite/allwpilib
synced 2026-06-22 01:11:42 +00:00
[wpilib] Throw early when EventLoop is modified while running (#6115)
This commit is contained in:
@@ -5,6 +5,7 @@
|
||||
package edu.wpi.first.wpilibj.event;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.ConcurrentModificationException;
|
||||
import java.util.LinkedHashSet;
|
||||
|
||||
/**
|
||||
@@ -12,6 +13,7 @@ import java.util.LinkedHashSet;
|
||||
*/
|
||||
public final class EventLoop {
|
||||
private final Collection<Runnable> m_bindings = new LinkedHashSet<>();
|
||||
private boolean m_running;
|
||||
|
||||
/**
|
||||
* Bind a new action to run when the loop is polled.
|
||||
@@ -19,16 +21,27 @@ public final class EventLoop {
|
||||
* @param action the action to run.
|
||||
*/
|
||||
public void bind(Runnable action) {
|
||||
if (m_running) {
|
||||
throw new ConcurrentModificationException("Cannot bind EventLoop while it is running");
|
||||
}
|
||||
m_bindings.add(action);
|
||||
}
|
||||
|
||||
/** Poll all bindings. */
|
||||
public void poll() {
|
||||
m_bindings.forEach(Runnable::run);
|
||||
try {
|
||||
m_running = true;
|
||||
m_bindings.forEach(Runnable::run);
|
||||
} finally {
|
||||
m_running = false;
|
||||
}
|
||||
}
|
||||
|
||||
/** Clear all bindings. */
|
||||
public void clear() {
|
||||
if (m_running) {
|
||||
throw new ConcurrentModificationException("Cannot clear EventLoop while it is running");
|
||||
}
|
||||
m_bindings.clear();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user