diff --git a/wpiutil/src/main/native/cpp/EventLoopRunner.cpp b/wpiutil/src/main/native/cpp/EventLoopRunner.cpp index 3b357879df..b885385279 100644 --- a/wpiutil/src/main/native/cpp/EventLoopRunner.cpp +++ b/wpiutil/src/main/native/cpp/EventLoopRunner.cpp @@ -44,11 +44,14 @@ class EventLoopRunner::Thread : public SafeThread { EventLoopRunner::EventLoopRunner() { m_owner.Start(); } -EventLoopRunner::~EventLoopRunner() { +EventLoopRunner::~EventLoopRunner() { Stop(); } + +void EventLoopRunner::Stop() { ExecAsync([](uv::Loop& loop) { // close all handles; this will (eventually) stop the loop loop.Walk([](uv::Handle& h) { h.Close(); }); }); + m_owner.Join(); } void EventLoopRunner::ExecAsync(LoopFunc func) { @@ -60,11 +63,13 @@ void EventLoopRunner::ExecAsync(LoopFunc func) { } void EventLoopRunner::ExecSync(LoopFunc func) { + wpi::future f; if (auto thr = m_owner.GetThread()) { if (auto doExec = thr->m_doExec.lock()) { - doExec->Call(func); + f = doExec->Call(func); } } + if (f.valid()) f.wait(); } std::shared_ptr EventLoopRunner::GetLoop() { diff --git a/wpiutil/src/main/native/include/wpi/EventLoopRunner.h b/wpiutil/src/main/native/include/wpi/EventLoopRunner.h index 71c0f681c7..8150091a6a 100644 --- a/wpiutil/src/main/native/include/wpi/EventLoopRunner.h +++ b/wpiutil/src/main/native/include/wpi/EventLoopRunner.h @@ -26,6 +26,12 @@ class EventLoopRunner { EventLoopRunner(); virtual ~EventLoopRunner(); + /** + * Stop the loop. Once the loop is stopped it cannot be restarted. + * This function does not return until the loop has exited. + */ + void Stop(); + /** * Run a function asynchronously (once) on the loop. * This is safe to call from any thread, but is NOT safe to call from the