lokinet/llarp/util/thread/logic.hpp

74 lines
1.5 KiB
C++
Raw Normal View History

2018-11-19 23:57:28 +00:00
#ifndef LLARP_LOGIC_HPP
#define LLARP_LOGIC_HPP
#include <ev/ev.hpp>
#include <util/mem.h>
2019-09-01 13:26:16 +00:00
#include <util/thread/threadpool.h>
#include <nonstd/optional.hpp>
2018-04-30 18:18:18 +00:00
2018-12-10 14:14:55 +00:00
namespace llarp
2018-07-09 04:26:27 +00:00
{
2018-12-10 14:14:55 +00:00
class Logic
{
public:
2019-11-25 21:30:34 +00:00
Logic(size_t queueLength = size_t{1024 * 8});
2018-06-06 12:46:26 +00:00
2019-09-05 21:28:50 +00:00
~Logic();
/// stop all operation and wait for that to die
2018-12-10 14:14:55 +00:00
void
stop();
2018-04-30 18:18:18 +00:00
bool
2018-12-10 14:14:55 +00:00
queue_job(struct llarp_thread_job job);
2018-04-30 18:18:18 +00:00
2019-05-15 15:54:26 +00:00
bool
_traceLogicCall(std::function<void(void)> func, const char* filename, int lineo);
uint32_t
call_later(llarp_time_t later, std::function<void(void)> func);
2018-12-10 14:14:55 +00:00
void
cancel_call(uint32_t id);
2018-12-10 14:14:55 +00:00
void
remove_call(uint32_t id);
size_t
numPendingJobs() const;
bool
can_flush() const;
void
SetQueuer(std::function<void(std::function<void(void)>)> q);
void
set_event_loop(llarp_ev_loop* loop);
void
clear_event_loop();
private:
using ID_t = std::thread::id;
llarp_threadpool* const m_Thread;
2019-12-19 12:19:47 +00:00
llarp_ev_loop* m_Loop = nullptr;
nonstd::optional<ID_t> m_ID;
util::ContentionKiller m_Killer;
std::function<void(std::function<void(void)>)> m_Queue;
2018-12-10 14:14:55 +00:00
};
} // namespace llarp
2018-04-30 18:18:18 +00:00
#ifndef LogicCall
#if defined(LOKINET_DEBUG)
#ifdef LOG_TAG
#define LogicCall(l, ...) l->_traceLogicCall(__VA_ARGS__, LOG_TAG, __LINE__)
#else
#define LogicCall(l, ...) l->_traceLogicCall(__VA_ARGS__, __FILE__, __LINE__)
#endif
#else
2019-11-14 23:24:12 +00:00
#define LogicCall(l, ...) l->_traceLogicCall(__VA_ARGS__, 0, 0)
#endif
#endif
2018-04-30 18:18:18 +00:00
#endif