2018-04-30 18:18:18 +00:00
|
|
|
#include <llarp/logic.h>
|
|
|
|
#include <llarp/mem.h>
|
2018-05-28 20:51:15 +00:00
|
|
|
#include "logger.hpp"
|
2018-04-30 18:18:18 +00:00
|
|
|
|
2018-05-22 15:54:19 +00:00
|
|
|
struct llarp_logic
|
|
|
|
{
|
2018-04-30 18:18:34 +00:00
|
|
|
struct llarp_threadpool* thread;
|
|
|
|
struct llarp_timer_context* timer;
|
2018-04-30 18:18:18 +00:00
|
|
|
};
|
|
|
|
|
2018-05-28 13:49:44 +00:00
|
|
|
extern "C" {
|
|
|
|
|
2018-05-22 15:54:19 +00:00
|
|
|
struct llarp_logic*
|
2018-05-28 13:49:44 +00:00
|
|
|
llarp_init_logic()
|
2018-05-22 15:54:19 +00:00
|
|
|
{
|
2018-05-28 13:49:44 +00:00
|
|
|
llarp_logic* logic = new llarp_logic;
|
2018-05-22 15:54:19 +00:00
|
|
|
if(logic)
|
|
|
|
{
|
2018-05-20 18:56:34 +00:00
|
|
|
logic->thread = llarp_init_threadpool(1, "llarp-logic");
|
2018-05-22 15:54:19 +00:00
|
|
|
logic->timer = llarp_init_timer();
|
2018-04-30 18:18:18 +00:00
|
|
|
}
|
|
|
|
return logic;
|
|
|
|
};
|
|
|
|
|
2018-06-06 12:46:26 +00:00
|
|
|
struct llarp_logic*
|
|
|
|
llarp_init_single_process_logic(struct llarp_threadpool* tp)
|
|
|
|
{
|
|
|
|
llarp_logic* logic = new llarp_logic;
|
|
|
|
if(logic)
|
|
|
|
{
|
|
|
|
logic->thread = tp;
|
|
|
|
logic->timer = llarp_init_timer();
|
|
|
|
}
|
|
|
|
return logic;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
llarp_logic_tick(struct llarp_logic* logic)
|
|
|
|
{
|
|
|
|
llarp_timer_tick_all(logic->timer, logic->thread);
|
|
|
|
}
|
|
|
|
|
2018-05-22 15:54:19 +00:00
|
|
|
void
|
|
|
|
llarp_free_logic(struct llarp_logic** logic)
|
|
|
|
{
|
|
|
|
if(*logic)
|
|
|
|
{
|
2018-05-28 20:51:15 +00:00
|
|
|
// llarp_free_timer(&(*logic)->timer);
|
2018-05-28 13:49:44 +00:00
|
|
|
delete *logic;
|
2018-04-30 18:18:18 +00:00
|
|
|
}
|
2018-05-28 20:51:15 +00:00
|
|
|
*logic = nullptr;
|
2018-04-30 18:18:18 +00:00
|
|
|
}
|
|
|
|
|
2018-07-03 11:25:36 +00:00
|
|
|
void
|
|
|
|
llarp_logic_stop_timer(struct llarp_logic* logic)
|
|
|
|
{
|
|
|
|
if(logic->timer)
|
|
|
|
llarp_timer_stop(logic->timer);
|
|
|
|
}
|
|
|
|
|
2018-05-22 15:54:19 +00:00
|
|
|
void
|
|
|
|
llarp_logic_stop(struct llarp_logic* logic)
|
|
|
|
{
|
2018-06-01 17:47:37 +00:00
|
|
|
llarp::Debug("logic thread stop");
|
2018-05-28 20:51:15 +00:00
|
|
|
if(logic->thread)
|
|
|
|
{
|
|
|
|
llarp_threadpool_stop(logic->thread);
|
|
|
|
llarp_threadpool_join(logic->thread);
|
|
|
|
}
|
|
|
|
llarp_free_threadpool(&logic->thread);
|
|
|
|
|
2018-06-01 17:47:37 +00:00
|
|
|
llarp::Debug("logic timer stop");
|
2018-05-28 20:51:15 +00:00
|
|
|
if(logic->timer)
|
|
|
|
llarp_timer_stop(logic->timer);
|
2018-04-30 18:18:18 +00:00
|
|
|
}
|
|
|
|
|
2018-05-22 15:54:19 +00:00
|
|
|
void
|
|
|
|
llarp_logic_mainloop(struct llarp_logic* logic)
|
|
|
|
{
|
2018-04-30 18:18:18 +00:00
|
|
|
llarp_timer_run(logic->timer, logic->thread);
|
|
|
|
}
|
2018-05-17 20:00:58 +00:00
|
|
|
|
2018-05-22 15:54:19 +00:00
|
|
|
void
|
|
|
|
llarp_logic_queue_job(struct llarp_logic* logic, struct llarp_thread_job job)
|
2018-05-18 20:08:57 +00:00
|
|
|
{
|
2018-06-06 12:46:26 +00:00
|
|
|
llarp_thread_job j;
|
|
|
|
j.user = job.user;
|
|
|
|
j.work = job.work;
|
|
|
|
llarp_threadpool_queue_job(logic->thread, j);
|
2018-05-18 20:08:57 +00:00
|
|
|
}
|
|
|
|
|
2018-05-22 15:54:19 +00:00
|
|
|
uint32_t
|
|
|
|
llarp_logic_call_later(struct llarp_logic* logic, struct llarp_timeout_job job)
|
2018-05-17 20:00:58 +00:00
|
|
|
{
|
2018-06-06 12:46:26 +00:00
|
|
|
llarp_timeout_job j;
|
|
|
|
j.user = job.user;
|
|
|
|
j.timeout = job.timeout;
|
|
|
|
j.handler = job.handler;
|
|
|
|
return llarp_timer_call_later(logic->timer, j);
|
2018-05-17 20:00:58 +00:00
|
|
|
}
|
|
|
|
|
2018-05-22 15:54:19 +00:00
|
|
|
void
|
|
|
|
llarp_logic_cancel_call(struct llarp_logic* logic, uint32_t id)
|
2018-05-17 20:00:58 +00:00
|
|
|
{
|
2018-05-27 12:49:10 +00:00
|
|
|
llarp_timer_cancel_job(logic->timer, id);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
llarp_logic_remove_call(struct llarp_logic* logic, uint32_t id)
|
|
|
|
{
|
|
|
|
llarp_timer_remove_job(logic->timer, id);
|
2018-05-17 20:00:58 +00:00
|
|
|
}
|
2018-05-28 13:49:44 +00:00
|
|
|
}
|