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;