2018-01-29 14:19:00 +00:00
|
|
|
#ifndef LLARP_THREADPOOL_HPP
|
|
|
|
#define LLARP_THREADPOOL_HPP
|
|
|
|
|
2018-12-12 02:52:51 +00:00
|
|
|
#include <thread_pool.hpp>
|
|
|
|
#include <threading.hpp>
|
|
|
|
#include <threadpool.h>
|
2018-01-29 14:19:00 +00:00
|
|
|
|
2018-05-22 15:54:19 +00:00
|
|
|
namespace llarp
|
|
|
|
{
|
|
|
|
namespace thread
|
|
|
|
{
|
2018-11-22 23:59:03 +00:00
|
|
|
using mtx_t = util::Mutex;
|
|
|
|
using lock_t = util::Lock;
|
2018-07-27 00:21:57 +00:00
|
|
|
|
2018-11-19 11:56:40 +00:00
|
|
|
using Pool = ThreadPool;
|
2018-01-29 14:19:00 +00:00
|
|
|
|
2018-08-08 17:43:46 +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 18:02:08 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2018-08-08 18:02:08 +00:00
|
|
|
// 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()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2018-08-08 18:02:08 +00:00
|
|
|
std::thread* m_isolated = nullptr;
|
|
|
|
int m_flags;
|
2018-08-26 12:51:22 +00:00
|
|
|
int m_IsolatedWorkers = 0;
|
|
|
|
const char* IsolatedName = nullptr;
|
2018-08-17 19:49:58 +00:00
|
|
|
|
|
|
|
virtual void
|
|
|
|
MainLoop()
|
|
|
|
{
|
|
|
|
}
|
2018-08-08 17:43:46 +00:00
|
|
|
};
|
|
|
|
|
2018-08-26 12:51:22 +00:00
|
|
|
struct _NetIsolatedPool : public IsolatedPool
|
2018-08-08 18:02:08 +00:00
|
|
|
{
|
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();
|
|
|
|
}
|
2018-08-08 18:02:08 +00:00
|
|
|
|
|
|
|
bool
|
|
|
|
Isolated()
|
|
|
|
{
|
2018-08-18 14:01:21 +00:00
|
|
|
return m_NetSetup(m_user, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Fail()
|
|
|
|
{
|
|
|
|
m_NetSetup(m_user, false);
|
2018-08-08 18:02:08 +00:00
|
|
|
}
|
|
|
|
|
2018-08-17 19:49:58 +00:00
|
|
|
void
|
|
|
|
MainLoop()
|
|
|
|
{
|
|
|
|
m_RunMain(m_user);
|
2018-08-08 18:02:08 +00:00
|
|
|
}
|
|
|
|
|
2018-08-18 14:01:21 +00:00
|
|
|
std::function< bool(void*, bool) > m_NetSetup;
|
2018-08-17 19:49:58 +00:00
|
|
|
std::function< void(void*) > m_RunMain;
|
2018-08-09 19:02:17 +00:00
|
|
|
void* m_user;
|
2018-08-08 18:02:08 +00:00
|
|
|
};
|
|
|
|
|
2018-05-22 15:54:19 +00:00
|
|
|
} // namespace thread
|
2018-02-01 13:21:00 +00:00
|
|
|
} // namespace llarp
|
2018-01-29 14:19:00 +00:00
|
|
|
|
|
|
|
#endif
|