RobotCore 1.0
WPILib port to raspberry Pi
Loading...
Searching...
No Matches
EventLoop.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.event;
6
7import java.util.ConcurrentModificationException;
8import java.util.LinkedHashSet;
9import org.wpilib.util.container.WeakLinkedHashSet;
10
14public final class EventLoop {
15 // Keep all bindings in one insertion-ordered collection for polling.
16 private final WeakLinkedHashSet<Runnable> m_bindings = new WeakLinkedHashSet<>();
17 // Keep strong references for strongly bound actions to prevent garbage collection.
18 private final LinkedHashSet<Runnable> m_strongBindings = new LinkedHashSet<>();
19 private boolean m_running;
20
22 public EventLoop() {}
23
31 public void bind(Runnable action) {
32 if (m_running) {
33 throw new ConcurrentModificationException("Cannot bind EventLoop while it is running");
34 }
35 m_bindings.add(action);
36 m_strongBindings.add(action);
37 }
38
45 public void bindWeak(Runnable action) {
46 if (m_running) {
47 throw new ConcurrentModificationException("Cannot bind EventLoop while it is running");
48 }
49 m_bindings.add(action);
50 }
51
58 public boolean isBound(Runnable action) {
59 return m_bindings.contains(action);
60 }
61
68 public void unbind(Runnable action) {
69 if (m_running) {
70 throw new ConcurrentModificationException("Cannot unbind EventLoop while it is running");
71 }
72 m_bindings.remove(action);
73 m_strongBindings.remove(action);
74 }
75
77 @SuppressWarnings("PMD.UnusedAssignment")
78 public void poll() {
79 try {
80 m_running = true;
81 m_bindings.forEach(Runnable::run);
82 } finally {
83 m_running = false;
84 }
85 }
86
88 public void clear() {
89 if (m_running) {
90 throw new ConcurrentModificationException("Cannot clear EventLoop while it is running");
91 }
92 m_bindings.clear();
93 m_strongBindings.clear();
94 }
95}
void bind(Runnable action)
void bindWeak(Runnable action)
boolean isBound(Runnable action)
void unbind(Runnable action)