lokinet/llarp/threadpool.hpp

105 lines
1.9 KiB
C++
Raw Normal View History

2018-01-29 14:19:00 +00:00
#ifndef LLARP_THREADPOOL_HPP
#define LLARP_THREADPOOL_HPP
#include <thread_pool.hpp>
#include <threading.hpp>
#include <threadpool.h>
2018-01-29 14:19:00 +00:00
namespace llarp
{
namespace thread
{
using mtx_t = util::Mutex;
using lock_t = util::Lock;
2018-11-19 11:56:40 +00:00
using Pool = ThreadPool;
2018-01-29 14:19:00 +00:00
struct IsolatedPool : public Pool
{
2018-11-19 11:56:40 +00:00
IsolatedPool(size_t workers, int flags)
: Pool(workers, workers * 128), m_flags(flags)
{
}
2018-08-08 17:47:13 +00:00
void
Join();
2018-08-26 12:51:22 +00:00
/// isolate current thread
/// return true for success
/// return false for failure
/// set errno on fail
/// override me in subclass
virtual bool
IsolateCurrentProcess()
{
return true;
}
// override me to do specific setups after isolation
// return true for success
virtual bool
Isolated()
{
return true;
}
2018-08-18 14:01:21 +00:00
/// called when isolation failed
virtual void
Fail()
{
}
std::thread* m_isolated = nullptr;
int m_flags;
2018-08-26 12:51:22 +00:00
int m_IsolatedWorkers = 0;
const char* IsolatedName = nullptr;
virtual void
MainLoop()
{
}
};
2018-08-26 12:51:22 +00:00
struct _NetIsolatedPool : public IsolatedPool
{
2018-08-26 12:51:22 +00:00
_NetIsolatedPool(std::function< bool(void*, bool) > setupNet,
std::function< void(void*) > runMain, void* user);
/// implement me per platform
virtual bool
IsolateNetwork() = 0;
bool
IsolateCurrentProcess()
{
return IsolateNetwork();
}
bool
Isolated()
{
2018-08-18 14:01:21 +00:00
return m_NetSetup(m_user, true);
}
void
Fail()
{
m_NetSetup(m_user, false);
}
void
MainLoop()
{
m_RunMain(m_user);
}
2018-08-18 14:01:21 +00:00
std::function< bool(void*, bool) > m_NetSetup;
std::function< void(void*) > m_RunMain;
2018-08-09 19:02:17 +00:00
void* m_user;
};
} // namespace thread
2018-02-01 13:21:00 +00:00
} // namespace llarp
2018-01-29 14:19:00 +00:00
#endif