2019-09-01 13:26:16 +00:00
|
|
|
#include <util/thread/logic.hpp>
|
2019-09-01 12:10:49 +00:00
|
|
|
#include <util/logging/logger.hpp>
|
2019-01-10 19:41:51 +00:00
|
|
|
#include <util/mem.h>
|
2018-04-30 18:18:18 +00:00
|
|
|
|
2019-11-14 15:06:53 +00:00
|
|
|
#include <future>
|
|
|
|
|
2018-12-10 14:14:55 +00:00
|
|
|
namespace llarp
|
2018-05-22 15:54:19 +00:00
|
|
|
{
|
2018-12-10 14:14:55 +00:00
|
|
|
void
|
|
|
|
Logic::stop()
|
|
|
|
{
|
|
|
|
llarp::LogDebug("logic thread stop");
|
|
|
|
}
|
2018-05-18 20:08:57 +00:00
|
|
|
|
2020-05-23 20:04:43 +00:00
|
|
|
void
|
|
|
|
Logic::Call(std::function<void(void)> func)
|
2019-04-17 19:05:54 +00:00
|
|
|
{
|
2020-06-16 11:28:20 +00:00
|
|
|
m_Queue(std::move(func));
|
2019-04-17 19:05:54 +00:00
|
|
|
}
|
|
|
|
|
2019-12-10 15:21:52 +00:00
|
|
|
void
|
2020-04-07 18:38:56 +00:00
|
|
|
Logic::SetQueuer(std::function<void(std::function<void(void)>)> q)
|
2019-12-10 15:21:52 +00:00
|
|
|
{
|
2020-05-23 20:04:43 +00:00
|
|
|
m_Queue = std::move(q);
|
2019-12-10 15:21:52 +00:00
|
|
|
}
|
|
|
|
|
2019-11-23 04:47:08 +00:00
|
|
|
uint32_t
|
2020-04-07 18:38:56 +00:00
|
|
|
Logic::call_later(llarp_time_t timeout, std::function<void(void)> func)
|
2019-07-28 17:13:52 +00:00
|
|
|
{
|
2019-11-23 04:47:08 +00:00
|
|
|
auto loop = m_Loop;
|
2020-04-07 18:38:56 +00:00
|
|
|
if (loop != nullptr)
|
2019-11-23 04:47:08 +00:00
|
|
|
{
|
|
|
|
return loop->call_after_delay(timeout, func);
|
|
|
|
}
|
|
|
|
return 0;
|
2019-07-28 17:13:52 +00:00
|
|
|
}
|
|
|
|
|
2018-12-10 14:14:55 +00:00
|
|
|
void
|
|
|
|
Logic::cancel_call(uint32_t id)
|
|
|
|
{
|
2019-11-23 04:47:08 +00:00
|
|
|
auto loop = m_Loop;
|
2020-04-07 18:38:56 +00:00
|
|
|
if (loop != nullptr)
|
2019-11-23 04:47:08 +00:00
|
|
|
{
|
|
|
|
loop->cancel_delayed_call(id);
|
|
|
|
}
|
2018-12-10 14:14:55 +00:00
|
|
|
}
|
2018-05-27 12:49:10 +00:00
|
|
|
|
2018-12-10 14:14:55 +00:00
|
|
|
void
|
|
|
|
Logic::remove_call(uint32_t id)
|
|
|
|
{
|
2019-11-23 04:47:08 +00:00
|
|
|
auto loop = m_Loop;
|
2020-04-07 18:38:56 +00:00
|
|
|
if (loop != nullptr)
|
2019-11-23 04:47:08 +00:00
|
|
|
{
|
|
|
|
loop->cancel_delayed_call(id);
|
|
|
|
}
|
2018-12-10 14:14:55 +00:00
|
|
|
}
|
|
|
|
|
2019-11-23 04:47:08 +00:00
|
|
|
void
|
2021-01-11 23:13:22 +00:00
|
|
|
Logic::set_event_loop(EventLoop* loop)
|
2019-11-23 04:47:08 +00:00
|
|
|
{
|
|
|
|
m_Loop = loop;
|
2020-06-16 11:28:20 +00:00
|
|
|
SetQueuer([loop](std::function<void(void)> work) { loop->call_soon(work); });
|
2019-11-23 04:47:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Logic::clear_event_loop()
|
|
|
|
{
|
|
|
|
m_Loop = nullptr;
|
|
|
|
}
|
|
|
|
|
2018-12-10 14:14:55 +00:00
|
|
|
} // namespace llarp
|