2019-09-01 13:26:16 +00:00
|
|
|
#include <util/thread/logic.hpp>
|
2019-11-14 15:06:53 +00:00
|
|
|
#include <util/thread/timer.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>
|
2019-11-25 14:46:45 +00:00
|
|
|
#include <util/metrics/metrics.hpp>
|
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::tick(llarp_time_t now)
|
2018-05-22 15:54:19 +00:00
|
|
|
{
|
2019-12-10 15:26:57 +00:00
|
|
|
if(m_Queue)
|
|
|
|
{
|
|
|
|
llarp_timer_set_time(m_Timer, now);
|
2019-12-10 15:29:39 +00:00
|
|
|
if(llarp_timer_should_call(m_Timer))
|
|
|
|
m_Queue(std::bind(&llarp_timer_tick_all, m_Timer));
|
2019-12-10 15:26:57 +00:00
|
|
|
return;
|
|
|
|
}
|
2019-11-14 15:06:53 +00:00
|
|
|
llarp_timer_tick_all_async(m_Timer, m_Thread, now);
|
2018-04-30 18:18:18 +00:00
|
|
|
}
|
|
|
|
|
2019-11-25 21:30:34 +00:00
|
|
|
Logic::Logic(size_t sz)
|
|
|
|
: m_Thread(llarp_init_threadpool(1, "llarp-logic", sz))
|
2019-11-14 15:06:53 +00:00
|
|
|
, m_Timer(llarp_init_timer())
|
2019-09-16 10:21:12 +00:00
|
|
|
{
|
2019-11-14 15:06:53 +00:00
|
|
|
llarp_threadpool_start(m_Thread);
|
2019-09-16 10:21:12 +00:00
|
|
|
/// set thread id
|
2019-11-14 15:06:53 +00:00
|
|
|
std::promise< ID_t > result;
|
|
|
|
// queue setting id and try to get the result back
|
|
|
|
llarp_threadpool_queue_job(m_Thread, [&]() {
|
|
|
|
m_ID.emplace(std::this_thread::get_id());
|
|
|
|
result.set_value(m_ID.value());
|
|
|
|
});
|
|
|
|
// get the result back
|
|
|
|
ID_t spawned = result.get_future().get();
|
|
|
|
LogDebug("logic thread spawned on ", spawned);
|
2019-09-16 10:21:12 +00:00
|
|
|
}
|
|
|
|
|
2019-09-05 21:28:50 +00:00
|
|
|
Logic::~Logic()
|
|
|
|
{
|
2019-11-14 15:06:53 +00:00
|
|
|
delete m_Thread;
|
|
|
|
llarp_free_timer(m_Timer);
|
2018-04-30 18:18:18 +00:00
|
|
|
}
|
|
|
|
|
2019-11-29 18:46:58 +00:00
|
|
|
size_t
|
|
|
|
Logic::numPendingJobs() const
|
|
|
|
{
|
|
|
|
return m_Thread->pendingJobs();
|
|
|
|
}
|
|
|
|
|
2019-11-14 15:06:53 +00:00
|
|
|
bool
|
2018-12-10 14:14:55 +00:00
|
|
|
Logic::queue_job(struct llarp_thread_job job)
|
2018-05-28 20:51:15 +00:00
|
|
|
{
|
2019-11-14 21:56:01 +00:00
|
|
|
return job.user && job.work
|
|
|
|
&& LogicCall(this, std::bind(job.work, job.user));
|
2018-05-28 20:51:15 +00:00
|
|
|
}
|
|
|
|
|
2018-12-10 14:14:55 +00:00
|
|
|
void
|
|
|
|
Logic::stop()
|
|
|
|
{
|
|
|
|
llarp::LogDebug("logic thread stop");
|
2019-11-14 15:06:53 +00:00
|
|
|
// stop all timers from happening in the future
|
2019-11-14 21:56:01 +00:00
|
|
|
LogicCall(this, std::bind(&llarp_timer_stop, m_Timer));
|
2019-11-14 15:06:53 +00:00
|
|
|
// stop all operations on threadpool
|
|
|
|
llarp_threadpool_stop(m_Thread);
|
2018-12-10 14:14:55 +00:00
|
|
|
}
|
2018-05-18 20:08:57 +00:00
|
|
|
|
2019-05-15 15:54:26 +00:00
|
|
|
bool
|
2019-11-14 21:56:01 +00:00
|
|
|
Logic::_traceLogicCall(std::function< void(void) > func, const char* tag,
|
|
|
|
int line)
|
2019-04-17 19:05:54 +00:00
|
|
|
{
|
2019-11-14 21:56:01 +00:00
|
|
|
#define TAG (tag ? tag : LOG_TAG)
|
|
|
|
#define LINE (line ? line : __LINE__)
|
2019-11-14 19:32:03 +00:00
|
|
|
// wrap the function so that we ensure that it's always calling stuff one at
|
|
|
|
// a time
|
2019-11-14 21:56:01 +00:00
|
|
|
|
2019-11-25 14:46:45 +00:00
|
|
|
#if defined(LOKINET_DEBUG)
|
|
|
|
#define METRIC(action) \
|
|
|
|
metrics::integerTick("logic", action, 1, "tag", TAG, "line", \
|
|
|
|
std::to_string(LINE))
|
|
|
|
#else
|
|
|
|
#define METRIC(action) \
|
|
|
|
do \
|
|
|
|
{ \
|
|
|
|
} while(false)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
METRIC("queue");
|
2019-11-14 21:56:01 +00:00
|
|
|
auto f = [self = this, func, tag, line]() {
|
2019-11-25 17:39:21 +00:00
|
|
|
#if defined(LOKINET_DEBUG)
|
|
|
|
metrics::TimerGuard g("logic",
|
|
|
|
std::string(TAG) + ":" + std::to_string(LINE));
|
|
|
|
#endif
|
2019-12-10 15:43:27 +00:00
|
|
|
if(self->m_Queue)
|
|
|
|
{
|
|
|
|
func();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
self->m_Killer.TryAccess(func);
|
|
|
|
}
|
2019-11-14 21:56:01 +00:00
|
|
|
};
|
2019-11-29 01:08:57 +00:00
|
|
|
if(can_flush())
|
|
|
|
{
|
|
|
|
METRIC("fired");
|
|
|
|
f();
|
|
|
|
return true;
|
|
|
|
}
|
2019-12-10 15:21:52 +00:00
|
|
|
if(m_Queue)
|
|
|
|
{
|
|
|
|
m_Queue(f);
|
|
|
|
return true;
|
|
|
|
}
|
2019-11-14 17:18:20 +00:00
|
|
|
if(m_Thread->LooksFull(5))
|
2019-09-16 10:21:12 +00:00
|
|
|
{
|
2019-12-03 17:55:24 +00:00
|
|
|
LogErrorExplicit(TAG, LINE,
|
|
|
|
"holy crap, we are trying to queue a job "
|
2019-11-29 18:46:58 +00:00
|
|
|
"onto the logic thread but it looks full");
|
2019-11-25 14:46:45 +00:00
|
|
|
METRIC("full");
|
2019-11-29 05:21:47 +00:00
|
|
|
std::abort();
|
2019-09-16 10:21:12 +00:00
|
|
|
}
|
2019-11-14 21:56:01 +00:00
|
|
|
auto ret = llarp_threadpool_queue_job(m_Thread, f);
|
2019-11-25 14:46:45 +00:00
|
|
|
if(not ret)
|
|
|
|
{
|
|
|
|
METRIC("dropped");
|
|
|
|
}
|
2019-11-14 21:56:01 +00:00
|
|
|
return ret;
|
|
|
|
#undef TAG
|
|
|
|
#undef LINE
|
2019-11-25 14:46:45 +00:00
|
|
|
#undef METRIC
|
2019-04-17 19:05:54 +00:00
|
|
|
}
|
|
|
|
|
2019-12-10 15:21:52 +00:00
|
|
|
void
|
|
|
|
Logic::SetQueuer(std::function< void(std::function< void(void) >) > q)
|
|
|
|
{
|
|
|
|
m_Queue = q;
|
|
|
|
m_Queue([self = this]() { self->m_ID = std::this_thread::get_id(); });
|
|
|
|
}
|
|
|
|
|
2019-07-28 17:13:52 +00:00
|
|
|
void
|
|
|
|
Logic::call_later(llarp_time_t timeout, std::function< void(void) > func)
|
|
|
|
{
|
2019-11-14 15:06:53 +00:00
|
|
|
llarp_timer_call_func_later(m_Timer, timeout, func);
|
2019-07-28 17:13:52 +00:00
|
|
|
}
|
|
|
|
|
2018-12-10 14:14:55 +00:00
|
|
|
uint32_t
|
2019-01-03 21:54:26 +00:00
|
|
|
Logic::call_later(const llarp_timeout_job& job)
|
2018-12-10 14:14:55 +00:00
|
|
|
{
|
|
|
|
llarp_timeout_job j;
|
|
|
|
j.user = job.user;
|
|
|
|
j.timeout = job.timeout;
|
|
|
|
j.handler = job.handler;
|
2019-11-14 15:06:53 +00:00
|
|
|
return llarp_timer_call_later(m_Timer, j);
|
2018-12-10 14:14:55 +00:00
|
|
|
}
|
2018-05-17 20:00:58 +00:00
|
|
|
|
2018-12-10 14:14:55 +00:00
|
|
|
void
|
|
|
|
Logic::cancel_call(uint32_t id)
|
|
|
|
{
|
2019-11-14 15:06:53 +00:00
|
|
|
llarp_timer_cancel_job(m_Timer, 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-14 15:06:53 +00:00
|
|
|
llarp_timer_remove_job(m_Timer, id);
|
2018-12-10 14:14:55 +00:00
|
|
|
}
|
|
|
|
|
2019-05-22 17:18:19 +00:00
|
|
|
bool
|
|
|
|
Logic::can_flush() const
|
|
|
|
{
|
2019-11-14 15:06:53 +00:00
|
|
|
return m_ID.value() == std::this_thread::get_id();
|
2019-05-22 17:18:19 +00:00
|
|
|
}
|
|
|
|
|
2018-12-10 14:14:55 +00:00
|
|
|
} // namespace llarp
|