lokinet/llarp/threadpool.cpp

314 lines
6.5 KiB
C++
Raw Normal View History

2018-01-29 14:19:00 +00:00
#include "threadpool.hpp"
#ifndef _MSC_VER
2018-05-20 18:56:34 +00:00
#include <pthread.h>
#endif
2018-05-20 18:56:34 +00:00
#include <cstring>
2018-06-06 12:46:26 +00:00
#include <llarp/time.h>
#include <functional>
2018-06-06 12:46:26 +00:00
#include <queue>
2018-05-28 20:51:15 +00:00
#include "logger.hpp"
2018-01-29 14:19:00 +00:00
#if(__FreeBSD__) || (__OpenBSD__) || (__NetBSD__)
#include <pthread_np.h>
#endif
2018-08-09 15:55:37 +00:00
#ifdef __linux__
2018-08-26 12:51:22 +00:00
#include <llarp/linux/netns.hpp>
2018-08-09 15:55:37 +00:00
#endif
#ifdef _MSC_VER
#include <windows.h>
extern "C" void
SetThreadName(DWORD dwThreadID, LPCSTR szThreadName);
#endif
namespace llarp
{
namespace thread
{
void
Pool::Spawn(size_t workers, const char *name)
{
stop = false;
while(workers--)
{
threads.emplace_back([this, name] {
if(name)
{
#if(__APPLE__ && __MACH__)
pthread_setname_np(name);
#elif(__FreeBSD__) || (__OpenBSD__) || (__NetBSD__)
pthread_set_name_np(pthread_self(), name);
2018-08-20 10:52:47 +00:00
#elif(__linux__) || (__MINGW32__)
pthread_setname_np(pthread_self(), name);
#elif defined(_MSC_VER)
SetThreadName(GetCurrentThreadId(), name);
#endif
}
for(;;)
{
2018-08-30 18:48:43 +00:00
Job_t job;
{
lock_t lock(this->queue_mutex);
2018-08-12 17:22:29 +00:00
this->condition.WaitUntil(
lock, [this] { return this->stop || !this->jobs.empty(); });
2018-08-12 17:22:29 +00:00
if(this->stop)
{
// discard pending jobs
while(this->jobs.size())
{
this->jobs.pop();
}
return;
2018-08-12 17:22:29 +00:00
}
2018-08-30 18:48:43 +00:00
job = std::move(this->jobs.top());
this->jobs.pop();
}
// do work
2018-08-30 18:48:43 +00:00
job();
}
});
}
}
void
Pool::Stop()
{
{
lock_t lock(queue_mutex);
stop = true;
}
2018-08-12 17:22:29 +00:00
condition.NotifyAll();
}
void
Pool::Join()
{
for(auto &t : threads)
t.join();
threads.clear();
2018-08-12 17:22:29 +00:00
done.NotifyAll();
}
void
Pool::QueueJob(const llarp_thread_job &job)
{
{
lock_t lock(queue_mutex);
// don't allow enqueueing after stopping the pool
if(stop)
return;
2018-08-30 18:48:43 +00:00
jobs.emplace(ids++, job);
}
2018-08-12 17:22:29 +00:00
condition.NotifyOne();
}
2018-01-29 14:19:00 +00:00
void
IsolatedPool::Spawn(size_t workers, const char *name)
{
IsolatedPool *self = this;
2018-08-26 12:51:22 +00:00
self->IsolatedName = name;
self->m_IsolatedWorkers = workers;
m_isolated = new std::thread([self] {
2018-08-26 12:51:22 +00:00
if(!self->IsolateCurrentProcess())
2018-08-18 14:01:21 +00:00
{
2018-08-26 12:51:22 +00:00
llarp::LogError("isolation failed: ", strerror(errno));
2018-08-18 14:01:21 +00:00
self->Fail();
2018-08-26 12:51:22 +00:00
return;
2018-08-18 14:01:21 +00:00
}
2018-08-26 12:51:22 +00:00
llarp::LogInfo("spawning isolated environment");
self->Pool::Spawn(self->m_IsolatedWorkers, self->IsolatedName);
if(self->Isolated())
{
2018-08-26 12:51:22 +00:00
self->MainLoop();
}
});
}
2018-08-08 17:47:13 +00:00
void
IsolatedPool::Join()
{
Pool::Join();
if(m_isolated)
{
m_isolated->join();
delete m_isolated;
m_isolated = nullptr;
}
}
2018-08-26 12:51:22 +00:00
_NetIsolatedPool::_NetIsolatedPool(
2018-08-18 14:01:21 +00:00
std::function< bool(void *, bool) > setupNet,
std::function< void(void *) > runMain, void *user)
2018-08-26 12:51:22 +00:00
: IsolatedPool(0)
{
m_NetSetup = setupNet;
m_RunMain = runMain;
2018-08-09 19:02:17 +00:00
m_user = user;
}
2018-08-26 12:51:22 +00:00
#ifdef __linux__
struct LinuxNetNSIsolatedPool : public _NetIsolatedPool
{
2018-08-26 12:51:22 +00:00
LinuxNetNSIsolatedPool(std::function< bool(void *, bool) > setup,
std::function< void(void *) > run, void *user)
: _NetIsolatedPool(setup, run, user)
{
}
bool
IsolateNetwork()
{
return llarp::linux::NetNSSwitch(IsolatedName);
}
};
typedef LinuxNetNSIsolatedPool NetIsolatedPool;
#define NET_ISOLATION_SUPPORTED
#endif
2018-08-26 12:51:22 +00:00
#if defined(__FreeBSD__)
struct FreeBSDJailedThreadPool : public _NetIsolatedPool
{
bool
IsolateNetwork()
{
// TODO: implement me
return false;
}
};
typedef FreeBSDJailedThreadPool NetIsolatedPool;
#define NET_ISOLATION_SUPPORTED
#endif
} // namespace thread
2018-02-01 13:21:00 +00:00
} // namespace llarp
2018-01-29 14:27:24 +00:00
struct llarp_threadpool
{
2018-06-06 12:46:26 +00:00
llarp::thread::Pool *impl;
2018-08-12 17:22:29 +00:00
llarp::util::Mutex m_access;
2018-08-30 18:48:43 +00:00
uint32_t ids = 0;
std::queue< llarp::thread::Pool::Job_t > jobs;
2018-01-29 14:19:00 +00:00
llarp_threadpool(int workers, const char *name, bool isolate,
setup_net_func setup = nullptr,
run_main_func runmain = nullptr, void *user = nullptr)
2018-06-06 12:46:26 +00:00
{
2018-08-26 12:51:22 +00:00
#ifdef NET_ISOLATION_SUPPORTED
if(isolate)
impl = new llarp::thread::NetIsolatedPool(setup, runmain, user);
else
2018-08-26 12:51:22 +00:00
#else
if(isolate)
{
llarp::LogError("network isolation not supported");
}
#endif
impl = new llarp::thread::Pool();
impl->Spawn(workers, name);
2018-06-06 12:46:26 +00:00
}
llarp_threadpool() : impl(nullptr)
{
}
2018-01-29 14:19:00 +00:00
};
struct llarp_threadpool *
llarp_init_threadpool(int workers, const char *name)
{
2018-09-07 20:48:52 +00:00
if(workers <= 0)
workers = 1;
return new llarp_threadpool(workers, name, false);
2018-01-29 14:27:24 +00:00
}
2018-01-29 14:19:00 +00:00
2018-06-06 12:46:26 +00:00
struct llarp_threadpool *
llarp_init_same_process_threadpool()
{
return new llarp_threadpool();
}
struct llarp_threadpool *
2018-08-09 19:02:17 +00:00
llarp_init_isolated_net_threadpool(const char *name, setup_net_func setup,
run_main_func runmain, void *context)
{
return new llarp_threadpool(1, name, true, setup, runmain, context);
}
void
llarp_threadpool_join(struct llarp_threadpool *pool)
{
llarp::LogDebug("threadpool join");
2018-06-06 12:46:26 +00:00
if(pool->impl)
pool->impl->Join();
}
2018-01-29 14:19:00 +00:00
void
llarp_threadpool_start(struct llarp_threadpool *pool)
{ /** no op */
2018-02-01 13:21:00 +00:00
}
void
llarp_threadpool_stop(struct llarp_threadpool *pool)
{
llarp::LogDebug("threadpool stop");
2018-06-06 12:46:26 +00:00
if(pool->impl)
pool->impl->Stop();
}
2018-04-30 14:57:13 +00:00
void
llarp_threadpool_wait(struct llarp_threadpool *pool)
{
2018-08-12 17:22:29 +00:00
llarp::util::Mutex mtx;
llarp::LogDebug("threadpool wait");
2018-06-06 12:46:26 +00:00
if(pool->impl)
2018-04-30 14:57:13 +00:00
{
2018-08-12 17:22:29 +00:00
llarp::util::Lock lock(mtx);
pool->impl->done.Wait(lock);
2018-04-30 14:57:13 +00:00
}
}
void
llarp_threadpool_queue_job(struct llarp_threadpool *pool,
struct llarp_thread_job job)
{
2018-06-06 12:46:26 +00:00
if(pool->impl)
pool->impl->QueueJob(job);
2018-08-30 18:48:43 +00:00
else
{
2018-08-30 18:48:43 +00:00
// single threaded mode
llarp::util::Lock lock(pool->m_access);
pool->jobs.emplace(++pool->ids, job);
}
2018-06-06 12:46:26 +00:00
}
void
llarp_threadpool_tick(struct llarp_threadpool *pool)
{
while(pool->jobs.size())
{
2018-08-30 18:48:43 +00:00
llarp::thread::Pool::Job_t job;
{
2018-08-12 17:22:29 +00:00
llarp::util::Lock lock(pool->m_access);
2018-08-30 18:48:43 +00:00
job = std::move(pool->jobs.front());
pool->jobs.pop();
}
2018-08-30 18:48:43 +00:00
job();
2018-06-06 12:46:26 +00:00
}
2018-01-31 19:59:26 +00:00
}
void
llarp_free_threadpool(struct llarp_threadpool **pool)
{
if(*pool)
{
delete *pool;
}
2018-01-29 14:27:24 +00:00
*pool = nullptr;
}