use only 1 uv_async_t for event loop wakeups

flush logic calls in there too as this was causing unit tests to hang
pull/1269/head
Jeff Becker 4 years ago
parent ad9edc4ecf
commit f04407f183
No known key found for this signature in database
GPG Key ID: F357B3B42F6F9B05

@ -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<Loop*>(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<Loop*>(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<void(void)> f)
{
m_LogicCalls.tryPushBack(f);
uv_async_send(&m_LogicCaller);
uv_async_send(&m_WakeUp);
}
} // namespace libuv

@ -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<void(void)> f) override;
void
FlushLogic();
private:
uv_loop_t m_Impl;
uv_timer_t* m_TickTimer;
uv_async_t m_WakeUp;
std::atomic<bool> m_Run;
uv_async_t m_LogicCaller;
using AtomicQueue_t = llarp::thread::Queue<std::function<void(void)>>;
AtomicQueue_t m_LogicCalls;

Loading…
Cancel
Save