[wpilib] Throw early when EventLoop is modified while running (#6115)

This commit is contained in:
Thad House
2023-12-31 22:45:10 -08:00
committed by GitHub
parent c16946c0ec
commit f9aabc5ab2
5 changed files with 91 additions and 1 deletions

View File

@@ -4,20 +4,41 @@
#include "frc/event/EventLoop.h"
#include "frc/Errors.h"
using namespace frc;
namespace {
struct RunningSetter {
bool& m_running;
explicit RunningSetter(bool& running) noexcept : m_running{running} {
m_running = true;
}
~RunningSetter() noexcept { m_running = false; }
};
} // namespace
EventLoop::EventLoop() {}
void EventLoop::Bind(wpi::unique_function<void()> action) {
if (m_running) {
throw FRC_MakeError(err::Error,
"Cannot bind EventLoop while it is running");
}
m_bindings.emplace_back(std::move(action));
}
void EventLoop::Poll() {
RunningSetter runSetter{m_running};
for (wpi::unique_function<void()>& action : m_bindings) {
action();
}
}
void EventLoop::Clear() {
if (m_running) {
throw FRC_MakeError(err::Error,
"Cannot clear EventLoop while it is running");
}
m_bindings.clear();
}

View File

@@ -38,5 +38,6 @@ class EventLoop {
private:
std::vector<wpi::unique_function<void()>> m_bindings;
bool m_running{false};
};
} // namespace frc

View File

@@ -0,0 +1,24 @@
// Copyright (c) FIRST and other WPILib contributors.
// Open Source Software; you can modify and/or share it under the terms of
// the WPILib BSD license file in the root directory of this project.
#include <gtest/gtest.h>
#include "frc/Errors.h"
#include "frc/event/EventLoop.h"
using namespace frc;
TEST(EventLoopTest, ConcurrentModification) {
EventLoop loop;
loop.Bind([&loop] { ASSERT_THROW(loop.Bind([] {}), frc::RuntimeError); });
loop.Poll();
loop.Clear();
loop.Bind([&loop] { ASSERT_THROW(loop.Clear(), frc::RuntimeError); });
loop.Poll();
}