2018-04-30 18:18:18 +00:00
|
|
|
#include <llarp/logic.h>
|
|
|
|
#include <llarp/mem.h>
|
|
|
|
|
2018-04-30 18:18:34 +00:00
|
|
|
struct llarp_logic {
|
2018-05-16 18:13:18 +00:00
|
|
|
struct llarp_alloc * mem;
|
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-16 18:13:18 +00:00
|
|
|
struct llarp_logic* llarp_init_logic(struct llarp_alloc * mem) {
|
|
|
|
struct llarp_logic* logic = mem->alloc(mem, sizeof(struct llarp_logic), 8);
|
2018-04-30 18:18:34 +00:00
|
|
|
if (logic) {
|
2018-05-16 18:13:18 +00:00
|
|
|
logic->mem = mem;
|
2018-04-30 18:18:18 +00:00
|
|
|
logic->thread = llarp_init_threadpool(1);
|
|
|
|
logic->timer = llarp_init_timer();
|
|
|
|
}
|
|
|
|
return logic;
|
|
|
|
};
|
|
|
|
|
2018-04-30 18:18:34 +00:00
|
|
|
void llarp_free_logic(struct llarp_logic** logic) {
|
|
|
|
if (*logic) {
|
2018-05-16 18:13:18 +00:00
|
|
|
struct llarp_alloc * mem = (*logic)->mem;
|
2018-04-30 18:18:18 +00:00
|
|
|
llarp_free_threadpool(&(*logic)->thread);
|
|
|
|
llarp_free_timer(&(*logic)->timer);
|
2018-05-16 18:13:18 +00:00
|
|
|
mem->free(mem, *logic);
|
2018-04-30 18:18:18 +00:00
|
|
|
*logic = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-30 18:18:34 +00:00
|
|
|
static void llarp_logic_stop_work(void* user) {
|
|
|
|
struct llarp_logic* logic = user;
|
2018-04-30 18:18:18 +00:00
|
|
|
llarp_timer_stop(logic->timer);
|
|
|
|
}
|
|
|
|
|
2018-04-30 18:18:34 +00:00
|
|
|
void llarp_logic_stop(struct llarp_logic* logic) {
|
|
|
|
struct llarp_thread_job job = {.user = logic, .work = &llarp_logic_stop_work};
|
2018-04-30 18:18:18 +00:00
|
|
|
llarp_threadpool_queue_job(logic->thread, job);
|
|
|
|
llarp_threadpool_stop(logic->thread);
|
|
|
|
llarp_threadpool_join(logic->thread);
|
|
|
|
}
|
|
|
|
|
2018-04-30 18:18:34 +00:00
|
|
|
void llarp_logic_mainloop(struct llarp_logic* logic) {
|
2018-04-30 18:18:18 +00:00
|
|
|
llarp_threadpool_start(logic->thread);
|
|
|
|
llarp_timer_run(logic->timer, logic->thread);
|
|
|
|
llarp_threadpool_wait(logic->thread);
|
|
|
|
}
|
2018-05-17 20:00:58 +00:00
|
|
|
|
|
|
|
uint32_t llarp_logic_call_later(struct llarp_logic* logic, struct llarp_timeout_job job)
|
|
|
|
{
|
|
|
|
return llarp_timer_call_later(logic->timer, job);
|
|
|
|
}
|
|
|
|
|
|
|
|
void llarp_logic_cancel_call(struct llarp_logic * logic, uint32_t id)
|
|
|
|
{
|
|
|
|
llarp_timer_cancel(logic->timer, id);
|
|
|
|
}
|
|
|
|
|