From 895acc45ffea6b8a60e95043ab7f6733ea1379dc Mon Sep 17 00:00:00 2001 From: Jason Rhinelander Date: Wed, 3 Mar 2021 14:59:39 -0400 Subject: [PATCH] EventLoop: add public wakeup() method, and call it from call() call(), when invoked from the logic thread, wasn't triggering a wakeup which stalled some traffic (such as client-to-snode packets). Fix it by triggering a wakeup on `call()`, and expose it because this is a useful thing to be able to do. --- llarp/ev/ev.hpp | 9 +++++++++ llarp/ev/ev_libuv.cpp | 6 ++++++ llarp/ev/ev_libuv.hpp | 3 +++ 3 files changed, 18 insertions(+) diff --git a/llarp/ev/ev.hpp b/llarp/ev/ev.hpp index 737fc7774..3eb00581b 100644 --- a/llarp/ev/ev.hpp +++ b/llarp/ev/ev.hpp @@ -77,6 +77,12 @@ namespace llarp return llarp::time_now_ms(); } + // Triggers an event loop wakeup; use when something has been done that requires the event loop + // to wake up (e.g. adding to queues). This is called implicitly by call() and call_soon(). + // Idempotent and thread-safe. + virtual void + wakeup() = 0; + // Calls a function/lambda/etc. If invoked from within the event loop itself this calls the // given lambda immediately; otherwise it passes it to `call_soon()` to be queued to run at the // next event loop iteration. @@ -85,7 +91,10 @@ namespace llarp call(Callable&& f) { if (inEventLoop()) + { f(); + wakeup(); + } else call_soon(std::forward(f)); } diff --git a/llarp/ev/ev_libuv.cpp b/llarp/ev/ev_libuv.cpp index 02f836cf3..a48fe1bf8 100644 --- a/llarp/ev/ev_libuv.cpp +++ b/llarp/ev/ev_libuv.cpp @@ -149,6 +149,12 @@ namespace llarp::uv llarp::LogInfo("we have stopped"); } + void + Loop::wakeup() + { + m_WakeUp->send(); + } + void Loop::set_pump_function(std::function pump) { diff --git a/llarp/ev/ev_libuv.hpp b/llarp/ev/ev_libuv.hpp index cfd19cf3b..8499c2e7a 100644 --- a/llarp/ev/ev_libuv.hpp +++ b/llarp/ev/ev_libuv.hpp @@ -32,6 +32,9 @@ namespace llarp::uv bool running() const override; + void + wakeup() override; + void call_later(llarp_time_t delay_ms, std::function callback) override;