mirror of https://github.com/oxen-io/lokinet
You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
102 lines
1.9 KiB
C++
102 lines
1.9 KiB
C++
7 years ago
|
#ifndef LLARP_THREADPOOL_HPP
|
||
|
#define LLARP_THREADPOOL_HPP
|
||
|
|
||
6 years ago
|
#include <util/thread_pool.hpp>
|
||
|
#include <util/threading.hpp>
|
||
|
#include <util/threadpool.h>
|
||
7 years ago
|
|
||
6 years ago
|
namespace llarp
|
||
|
{
|
||
|
namespace thread
|
||
|
{
|
||
6 years ago
|
using Pool = ThreadPool;
|
||
7 years ago
|
|
||
6 years ago
|
struct IsolatedPool : public Pool
|
||
|
{
|
||
6 years ago
|
IsolatedPool(size_t workers, int flags)
|
||
|
: Pool(workers, workers * 128), m_flags(flags)
|
||
6 years ago
|
{
|
||
|
}
|
||
|
|
||
6 years ago
|
void
|
||
|
Join();
|
||
|
|
||
6 years ago
|
/// isolate current thread
|
||
|
/// return true for success
|
||
|
/// return false for failure
|
||
|
/// set errno on fail
|
||
|
/// override me in subclass
|
||
|
virtual bool
|
||
|
IsolateCurrentProcess()
|
||
|
{
|
||
|
return true;
|
||
|
}
|
||
|
|
||
6 years ago
|
// override me to do specific setups after isolation
|
||
|
// return true for success
|
||
|
virtual bool
|
||
|
Isolated()
|
||
|
{
|
||
|
return true;
|
||
|
}
|
||
|
|
||
6 years ago
|
/// called when isolation failed
|
||
|
virtual void
|
||
|
Fail()
|
||
|
{
|
||
|
}
|
||
|
|
||
6 years ago
|
std::thread* m_isolated = nullptr;
|
||
|
int m_flags;
|
||
6 years ago
|
int m_IsolatedWorkers = 0;
|
||
|
const char* IsolatedName = nullptr;
|
||
6 years ago
|
|
||
|
virtual void
|
||
|
MainLoop()
|
||
|
{
|
||
|
}
|
||
6 years ago
|
};
|
||
|
|
||
6 years ago
|
struct _NetIsolatedPool : public IsolatedPool
|
||
6 years ago
|
{
|
||
6 years ago
|
_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();
|
||
|
}
|
||
6 years ago
|
|
||
|
bool
|
||
|
Isolated()
|
||
|
{
|
||
6 years ago
|
return m_NetSetup(m_user, true);
|
||
|
}
|
||
|
|
||
|
void
|
||
|
Fail()
|
||
|
{
|
||
|
m_NetSetup(m_user, false);
|
||
6 years ago
|
}
|
||
|
|
||
6 years ago
|
void
|
||
|
MainLoop()
|
||
|
{
|
||
|
m_RunMain(m_user);
|
||
6 years ago
|
}
|
||
|
|
||
6 years ago
|
std::function< bool(void*, bool) > m_NetSetup;
|
||
6 years ago
|
std::function< void(void*) > m_RunMain;
|
||
6 years ago
|
void* m_user;
|
||
6 years ago
|
};
|
||
|
|
||
6 years ago
|
} // namespace thread
|
||
7 years ago
|
} // namespace llarp
|
||
7 years ago
|
|
||
|
#endif
|