From f04407f1833d0bbba3e73d62f90cc779641ee793 Mon Sep 17 00:00:00 2001 From: Jeff Becker Date: Fri, 15 May 2020 08:36:38 -0400 Subject: [PATCH] use only 1 uv_async_t for event loop wakeups flush logic calls in there too as this was causing unit tests to hang --- llarp/ev/ev_libuv.cpp | 40 +++++++++++++++++++--------------------- llarp/ev/ev_libuv.hpp | 9 ++++----- 2 files changed, 23 insertions(+), 26 deletions(-) diff --git a/llarp/ev/ev_libuv.cpp b/llarp/ev/ev_libuv.cpp index 8790359f5..21225b8bc 100644 --- a/llarp/ev/ev_libuv.cpp +++ b/llarp/ev/ev_libuv.cpp @@ -746,12 +746,23 @@ namespace libuv }; #endif + void + Loop::FlushLogic() + { + while (not m_LogicCalls.empty()) + { + auto f = m_LogicCalls.popFront(); + f(); + } + } + static void OnAsyncWake(uv_async_t* async_handle) { Loop* loop = static_cast(async_handle->data); loop->process_timer_queue(); loop->process_cancel_queue(); + loop->FlushLogic(); } Loop::Loop() : llarp_ev_loop(), m_LogicCalls(1024), m_timerQueue(20), m_timerCancelQueue(20) @@ -774,25 +785,6 @@ namespace libuv #else uv_loop_configure(&m_Impl, UV_LOOP_BLOCK_SIGNAL, SIGPIPE); #endif - m_LogicCaller.data = this; - int err; - if ((err = uv_async_init( - &m_Impl, - &m_LogicCaller, - [](uv_async_t* h) { - Loop* l = static_cast(h->data); - while (not l->m_LogicCalls.empty()) - { - auto f = l->m_LogicCalls.popFront(); - f(); - } - })) - != 0) - { - llarp::LogError("Libuv uv_async_init returned error: ", uv_strerror(err)); - return false; - } - m_TickTimer = new uv_timer_t; m_TickTimer->data = this; m_Run.store(true); @@ -834,6 +826,12 @@ namespace libuv uv_stop(timer->loop); } + int + Loop::run() + { + return uv_run(&m_Impl, UV_RUN_DEFAULT); + } + int Loop::tick(int ms) { @@ -951,7 +949,7 @@ namespace libuv { llarp::LogInfo("stopping event loop"); CloseAll(); - // uv_stop(&m_Impl); + uv_stop(&m_Impl); } m_Run.store(false); } @@ -1061,7 +1059,7 @@ namespace libuv Loop::call_soon(std::function f) { m_LogicCalls.tryPushBack(f); - uv_async_send(&m_LogicCaller); + uv_async_send(&m_WakeUp); } } // namespace libuv diff --git a/llarp/ev/ev_libuv.hpp b/llarp/ev/ev_libuv.hpp index f439f3b03..deb9afc38 100644 --- a/llarp/ev/ev_libuv.hpp +++ b/llarp/ev/ev_libuv.hpp @@ -30,10 +30,7 @@ namespace libuv init() override; int - run() override - { - return -1; - } + run() override; bool running() const override; @@ -128,12 +125,14 @@ namespace libuv void call_soon(std::function f) override; + void + FlushLogic(); + private: uv_loop_t m_Impl; uv_timer_t* m_TickTimer; uv_async_t m_WakeUp; std::atomic m_Run; - uv_async_t m_LogicCaller; using AtomicQueue_t = llarp::thread::Queue>; AtomicQueue_t m_LogicCalls;