2018-01-29 14:19:00 +00:00
|
|
|
#include "threadpool.hpp"
|
2018-07-30 04:38:14 +00:00
|
|
|
#ifndef _MSC_VER
|
2018-05-20 18:56:34 +00:00
|
|
|
#include <pthread.h>
|
2018-07-30 04:38:14 +00:00
|
|
|
#endif
|
2018-05-20 18:56:34 +00:00
|
|
|
#include <cstring>
|
2018-06-06 12:46:26 +00:00
|
|
|
|
|
|
|
#include <llarp/time.h>
|
2018-08-08 17:43:46 +00:00
|
|
|
#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
|
|
|
|
2018-07-26 01:00:15 +00:00
|
|
|
#if(__FreeBSD__) || (__OpenBSD__) || (__NetBSD__)
|
2018-05-29 12:16:44 +00:00
|
|
|
#include <pthread_np.h>
|
|
|
|
#endif
|
|
|
|
|
2018-08-09 15:55:37 +00:00
|
|
|
#ifdef __linux__
|
2018-08-08 17:43:46 +00:00
|
|
|
#include <sys/wait.h>
|
2018-08-09 15:55:37 +00:00
|
|
|
#endif
|
2018-08-08 17:43:46 +00:00
|
|
|
|
2018-07-30 04:38:14 +00:00
|
|
|
#ifdef _MSC_VER
|
|
|
|
#include <windows.h>
|
|
|
|
extern "C" void
|
|
|
|
SetThreadName(DWORD dwThreadID, LPCSTR szThreadName);
|
|
|
|
#endif
|
|
|
|
|
2018-05-22 15:54:19 +00:00
|
|
|
namespace llarp
|
|
|
|
{
|
|
|
|
namespace thread
|
|
|
|
{
|
2018-08-08 17:43:46 +00:00
|
|
|
void
|
|
|
|
Pool::Spawn(size_t workers, const char *name)
|
2018-05-22 15:54:19 +00:00
|
|
|
{
|
|
|
|
stop = false;
|
|
|
|
while(workers--)
|
|
|
|
{
|
2018-06-01 17:47:37 +00:00
|
|
|
threads.emplace_back([this, name] {
|
|
|
|
if(name)
|
|
|
|
{
|
|
|
|
#if(__APPLE__ && __MACH__)
|
|
|
|
pthread_setname_np(name);
|
2018-07-26 01:00:15 +00:00
|
|
|
#elif(__FreeBSD__) || (__OpenBSD__) || (__NetBSD__)
|
2018-06-01 17:47:37 +00:00
|
|
|
pthread_set_name_np(pthread_self(), name);
|
2018-07-30 04:38:14 +00:00
|
|
|
#elif !defined(_MSC_VER) || !defined(_WIN32)
|
2018-06-01 17:47:37 +00:00
|
|
|
pthread_setname_np(pthread_self(), name);
|
2018-07-30 04:38:14 +00:00
|
|
|
#else
|
2018-08-02 21:26:14 +00:00
|
|
|
SetThreadName(GetCurrentThreadId(), name);
|
2018-06-01 17:47:37 +00:00
|
|
|
#endif
|
|
|
|
}
|
2018-05-22 15:54:19 +00:00
|
|
|
for(;;)
|
|
|
|
{
|
2018-06-06 12:46:26 +00:00
|
|
|
llarp_thread_job *job;
|
2018-05-22 15:54:19 +00:00
|
|
|
{
|
|
|
|
lock_t lock(this->queue_mutex);
|
|
|
|
this->condition.wait(
|
|
|
|
lock, [this] { return this->stop || !this->jobs.empty(); });
|
|
|
|
if(this->stop && this->jobs.empty())
|
|
|
|
return;
|
2018-07-27 00:21:57 +00:00
|
|
|
job = this->jobs.top().job;
|
|
|
|
this->jobs.pop();
|
2018-05-22 15:54:19 +00:00
|
|
|
}
|
|
|
|
// do work
|
2018-06-06 12:46:26 +00:00
|
|
|
job->work(job->user);
|
2018-07-11 13:20:14 +00:00
|
|
|
|
2018-06-06 12:46:26 +00:00
|
|
|
delete job;
|
2018-05-22 15:54:19 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Pool::Stop()
|
|
|
|
{
|
|
|
|
{
|
|
|
|
lock_t lock(queue_mutex);
|
|
|
|
stop = true;
|
|
|
|
}
|
|
|
|
condition.notify_all();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Pool::Join()
|
|
|
|
{
|
|
|
|
for(auto &t : threads)
|
|
|
|
t.join();
|
|
|
|
threads.clear();
|
2018-05-28 20:51:15 +00:00
|
|
|
done.notify_all();
|
2018-05-22 15:54:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Pool::QueueJob(const llarp_thread_job &job)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
lock_t lock(queue_mutex);
|
|
|
|
|
|
|
|
// don't allow enqueueing after stopping the pool
|
|
|
|
if(stop)
|
2018-05-27 16:45:04 +00:00
|
|
|
return;
|
2018-05-22 15:54:19 +00:00
|
|
|
|
2018-07-27 00:21:57 +00:00
|
|
|
jobs.emplace(ids++, new llarp_thread_job(job.user, job.work));
|
2018-05-22 15:54:19 +00:00
|
|
|
}
|
|
|
|
condition.notify_one();
|
|
|
|
}
|
2018-01-29 14:19:00 +00:00
|
|
|
|
2018-08-08 17:43:46 +00:00
|
|
|
static int
|
|
|
|
runIsolated(void *arg)
|
|
|
|
{
|
|
|
|
IsolatedPool *self = static_cast< IsolatedPool * >(arg);
|
2018-08-08 18:02:08 +00:00
|
|
|
if(!self->Isolated())
|
|
|
|
{
|
|
|
|
llarp::LogError("failed to set up isolated environment");
|
|
|
|
return 1;
|
|
|
|
}
|
2018-08-08 17:43:46 +00:00
|
|
|
auto func = std::bind(&Pool::Spawn, self, self->m_IsolatedWorkers,
|
|
|
|
self->m_IsolatedName);
|
|
|
|
func();
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
IsolatedPool::Spawn(int workers, const char *name)
|
|
|
|
{
|
|
|
|
if(m_isolated)
|
|
|
|
return;
|
|
|
|
#ifdef __linux__
|
|
|
|
IsolatedPool *self = this;
|
|
|
|
self->m_IsolatedName = name;
|
|
|
|
self->m_IsolatedWorkers = workers;
|
|
|
|
m_isolated = new std::thread([self] {
|
|
|
|
pid_t isolated;
|
|
|
|
isolated =
|
|
|
|
clone(runIsolated, self->m_childstack + sizeof(self->m_childstack),
|
2018-08-08 18:50:45 +00:00
|
|
|
self->m_flags | SIGCHLD, self);
|
2018-08-08 17:43:46 +00:00
|
|
|
if(isolated == -1)
|
|
|
|
{
|
|
|
|
llarp::LogError("failed to run isolated threadpool, ",
|
|
|
|
strerror(errno));
|
|
|
|
return;
|
|
|
|
}
|
2018-08-08 18:02:08 +00:00
|
|
|
llarp::LogInfo("Spawned isolated process pool");
|
2018-08-08 17:43:46 +00:00
|
|
|
if(waitpid(isolated, nullptr, 0) == -1)
|
|
|
|
{
|
|
|
|
llarp::LogError("failed to wait for pid ", isolated, ", ",
|
|
|
|
strerror(errno));
|
|
|
|
}
|
|
|
|
});
|
|
|
|
#else
|
2018-08-08 18:02:08 +00:00
|
|
|
llarp::LogError("isolated processes not supported on your platform");
|
2018-08-08 17:43:46 +00:00
|
|
|
Pool::Spawn(workers, name);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
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-08 18:02:08 +00:00
|
|
|
#ifdef __linux__
|
2018-08-09 19:02:17 +00:00
|
|
|
NetIsolatedPool::NetIsolatedPool(std::function< bool(void *) > setupNet,
|
|
|
|
void *user)
|
2018-08-08 18:50:45 +00:00
|
|
|
: IsolatedPool(CLONE_NEWNET)
|
2018-08-08 18:02:08 +00:00
|
|
|
{
|
|
|
|
m_NetSetup = setupNet;
|
2018-08-09 19:02:17 +00:00
|
|
|
m_user = user;
|
2018-08-08 18:02:08 +00:00
|
|
|
}
|
|
|
|
#else
|
2018-08-09 19:02:17 +00:00
|
|
|
NetIsolatedPool::NetIsolatedPool(std::function< bool(void *) > setupNet,
|
|
|
|
void *user)
|
2018-08-08 18:02:08 +00:00
|
|
|
: IsolatedPool(0)
|
|
|
|
{
|
|
|
|
m_NetSetup = setupNet;
|
2018-08-09 19:02:17 +00:00
|
|
|
m_user = user;
|
2018-08-08 18:02:08 +00:00
|
|
|
}
|
|
|
|
#endif
|
2018-05-22 15:54:19 +00:00
|
|
|
} // namespace thread
|
2018-02-01 13:21:00 +00:00
|
|
|
} // namespace llarp
|
2018-01-29 14:27:24 +00:00
|
|
|
|
2018-05-22 15:54:19 +00:00
|
|
|
struct llarp_threadpool
|
|
|
|
{
|
2018-06-06 12:46:26 +00:00
|
|
|
llarp::thread::Pool *impl;
|
|
|
|
|
2018-08-02 21:26:14 +00:00
|
|
|
std::mutex m_access;
|
2018-07-30 00:30:10 +00:00
|
|
|
std::queue< llarp_thread_job * > jobs;
|
2018-01-29 14:19:00 +00:00
|
|
|
|
2018-08-08 18:02:08 +00:00
|
|
|
llarp_threadpool(int workers, const char *name, bool isolate,
|
2018-08-09 19:02:17 +00:00
|
|
|
setup_net_func setup = nullptr, void *user = nullptr)
|
2018-06-06 12:46:26 +00:00
|
|
|
{
|
2018-08-08 17:43:46 +00:00
|
|
|
if(isolate)
|
2018-08-09 19:02:17 +00:00
|
|
|
impl = new llarp::thread::NetIsolatedPool(setup, user);
|
2018-08-08 17:43:46 +00:00
|
|
|
else
|
|
|
|
impl = new llarp::thread::Pool();
|
|
|
|
impl->Spawn(workers, name);
|
2018-06-06 12:46:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
llarp_threadpool() : impl(nullptr)
|
2018-05-22 15:54:19 +00:00
|
|
|
{
|
|
|
|
}
|
2018-01-29 14:19:00 +00:00
|
|
|
};
|
|
|
|
|
2018-05-22 15:54:19 +00:00
|
|
|
struct llarp_threadpool *
|
|
|
|
llarp_init_threadpool(int workers, const char *name)
|
|
|
|
{
|
|
|
|
if(workers > 0)
|
2018-08-08 17:43:46 +00:00
|
|
|
return new llarp_threadpool(workers, name, false);
|
2018-01-29 14:27:24 +00:00
|
|
|
else
|
|
|
|
return nullptr;
|
|
|
|
}
|
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();
|
|
|
|
}
|
|
|
|
|
2018-08-08 17:43:46 +00:00
|
|
|
struct llarp_threadpool *
|
2018-08-09 19:02:17 +00:00
|
|
|
llarp_init_isolated_net_threadpool(const char *name, setup_net_func setup,
|
|
|
|
void *context)
|
2018-08-08 17:43:46 +00:00
|
|
|
{
|
2018-08-09 19:02:17 +00:00
|
|
|
return new llarp_threadpool(1, name, true, setup, context);
|
2018-08-08 17:43:46 +00:00
|
|
|
}
|
|
|
|
|
2018-05-22 15:54:19 +00:00
|
|
|
void
|
|
|
|
llarp_threadpool_join(struct llarp_threadpool *pool)
|
|
|
|
{
|
2018-07-05 15:44:06 +00:00
|
|
|
llarp::LogDebug("threadpool join");
|
2018-06-06 12:46:26 +00:00
|
|
|
if(pool->impl)
|
|
|
|
pool->impl->Join();
|
2018-05-22 15:54:19 +00:00
|
|
|
}
|
2018-01-29 14:19:00 +00:00
|
|
|
|
2018-05-22 15:54:19 +00:00
|
|
|
void
|
|
|
|
llarp_threadpool_start(struct llarp_threadpool *pool)
|
|
|
|
{ /** no op */
|
2018-02-01 13:21:00 +00:00
|
|
|
}
|
|
|
|
|
2018-05-22 15:54:19 +00:00
|
|
|
void
|
|
|
|
llarp_threadpool_stop(struct llarp_threadpool *pool)
|
|
|
|
{
|
2018-07-05 15:44:06 +00:00
|
|
|
llarp::LogDebug("threadpool stop");
|
2018-06-06 12:46:26 +00:00
|
|
|
if(pool->impl)
|
|
|
|
pool->impl->Stop();
|
2018-05-22 15:54:19 +00:00
|
|
|
}
|
2018-04-30 14:57:13 +00:00
|
|
|
|
2018-05-22 15:54:19 +00:00
|
|
|
void
|
|
|
|
llarp_threadpool_wait(struct llarp_threadpool *pool)
|
|
|
|
{
|
2018-04-30 14:57:13 +00:00
|
|
|
std::mutex mtx;
|
2018-07-05 15:44:06 +00:00
|
|
|
llarp::LogDebug("threadpool wait");
|
2018-06-06 12:46:26 +00:00
|
|
|
if(pool->impl)
|
2018-04-30 14:57:13 +00:00
|
|
|
{
|
2018-05-22 15:54:19 +00:00
|
|
|
std::unique_lock< std::mutex > lock(mtx);
|
2018-06-06 12:46:26 +00:00
|
|
|
pool->impl->done.wait(lock);
|
2018-04-30 14:57:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-22 15:54:19 +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-02 21:26:14 +00:00
|
|
|
else if(job.user && job.work)
|
|
|
|
{
|
|
|
|
auto j = new llarp_thread_job;
|
|
|
|
j->work = job.work;
|
|
|
|
j->user = job.user;
|
|
|
|
{
|
|
|
|
std::unique_lock< std::mutex > lock(pool->m_access);
|
|
|
|
pool->jobs.push(j);
|
|
|
|
}
|
|
|
|
}
|
2018-06-06 12:46:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
llarp_threadpool_tick(struct llarp_threadpool *pool)
|
|
|
|
{
|
|
|
|
while(pool->jobs.size())
|
|
|
|
{
|
2018-08-02 21:26:14 +00:00
|
|
|
llarp_thread_job *job;
|
|
|
|
{
|
|
|
|
std::unique_lock< std::mutex > lock(pool->m_access);
|
|
|
|
job = pool->jobs.front();
|
|
|
|
pool->jobs.pop();
|
|
|
|
}
|
|
|
|
job->work(job->user);
|
2018-07-30 00:30:10 +00:00
|
|
|
delete job;
|
2018-06-06 12:46:26 +00:00
|
|
|
}
|
2018-01-31 19:59:26 +00:00
|
|
|
}
|
|
|
|
|
2018-05-22 15:54:19 +00:00
|
|
|
void
|
|
|
|
llarp_free_threadpool(struct llarp_threadpool **pool)
|
|
|
|
{
|
2018-05-27 16:45:04 +00:00
|
|
|
if(*pool)
|
|
|
|
{
|
|
|
|
delete *pool;
|
|
|
|
}
|
2018-01-29 14:27:24 +00:00
|
|
|
*pool = nullptr;
|
|
|
|
}
|