mirror of
https://github.com/wpilibsuite/allwpilib
synced 2026-06-19 00:41:43 +00:00
wpiutil: Add Stop() to EventLoopRunner
This allows stopping the event loop without having to destroy the object. Also fix ExecSync() to actually wait for the call to complete.
This commit is contained in:
@@ -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<void> 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<uv::Loop> EventLoopRunner::GetLoop() {
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user