2019-01-10 20:41:51 +01:00
|
|
|
#include <util/logger.hpp>
|
|
|
|
#include <util/threadpool.hpp>
|
|
|
|
#include <util/time.hpp>
|
2018-06-06 14:46:26 +02:00
|
|
|
|
2018-12-12 03:52:51 +01:00
|
|
|
#include <cstring>
|
2018-08-08 19:43:46 +02:00
|
|
|
#include <functional>
|
2018-06-06 14:46:26 +02:00
|
|
|
#include <queue>
|
|
|
|
|
2018-05-22 17:54:19 +02:00
|
|
|
struct llarp_threadpool *
|
|
|
|
llarp_init_threadpool(int workers, const char *name)
|
|
|
|
{
|
2018-09-07 22:48:52 +02:00
|
|
|
if(workers <= 0)
|
|
|
|
workers = 1;
|
2018-11-19 12:56:40 +01:00
|
|
|
return new llarp_threadpool(workers, name);
|
2018-01-29 15:27:24 +01:00
|
|
|
}
|
2018-01-29 15:19:00 +01:00
|
|
|
|
2018-06-06 14:46:26 +02:00
|
|
|
struct llarp_threadpool *
|
|
|
|
llarp_init_same_process_threadpool()
|
|
|
|
{
|
|
|
|
return new llarp_threadpool();
|
|
|
|
}
|
|
|
|
|
2018-05-22 17:54:19 +02:00
|
|
|
void
|
|
|
|
llarp_threadpool_join(struct llarp_threadpool *pool)
|
|
|
|
{
|
2018-07-05 17:44:06 +02:00
|
|
|
llarp::LogDebug("threadpool join");
|
2018-06-06 14:46:26 +02:00
|
|
|
if(pool->impl)
|
2018-11-19 12:56:40 +01:00
|
|
|
pool->impl->drain();
|
2018-05-22 17:54:19 +02:00
|
|
|
}
|
2018-01-29 15:19:00 +01:00
|
|
|
|
2018-05-22 17:54:19 +02:00
|
|
|
void
|
2018-11-19 12:56:40 +01:00
|
|
|
llarp_threadpool_start(struct llarp_threadpool *pool)
|
|
|
|
{
|
|
|
|
if(pool->impl)
|
|
|
|
pool->impl->start();
|
2018-02-01 14:21:00 +01:00
|
|
|
}
|
|
|
|
|
2018-05-22 17:54:19 +02:00
|
|
|
void
|
|
|
|
llarp_threadpool_stop(struct llarp_threadpool *pool)
|
|
|
|
{
|
2018-07-05 17:44:06 +02:00
|
|
|
llarp::LogDebug("threadpool stop");
|
2018-06-06 14:46:26 +02:00
|
|
|
if(pool->impl)
|
2018-11-19 12:56:40 +01:00
|
|
|
pool->impl->stop();
|
2018-05-22 17:54:19 +02:00
|
|
|
}
|
2018-04-30 16:57:13 +02:00
|
|
|
|
2018-05-22 17:54:19 +02:00
|
|
|
void
|
|
|
|
llarp_threadpool_wait(struct llarp_threadpool *pool)
|
|
|
|
{
|
2018-07-05 17:44:06 +02:00
|
|
|
llarp::LogDebug("threadpool wait");
|
2018-06-06 14:46:26 +02:00
|
|
|
if(pool->impl)
|
2018-04-30 16:57:13 +02:00
|
|
|
{
|
2018-11-19 12:56:40 +01:00
|
|
|
pool->impl->drain();
|
2018-04-30 16:57:13 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-22 17:54:19 +02:00
|
|
|
void
|
|
|
|
llarp_threadpool_queue_job(struct llarp_threadpool *pool,
|
|
|
|
struct llarp_thread_job job)
|
|
|
|
{
|
2018-06-06 14:46:26 +02:00
|
|
|
if(pool->impl)
|
2019-05-15 17:54:26 +02:00
|
|
|
{
|
|
|
|
while(!pool->impl->tryAddJob(std::bind(job.work, job.user)))
|
|
|
|
{
|
2019-05-16 00:03:24 +02:00
|
|
|
std::this_thread::sleep_for(std::chrono::microseconds(1000));
|
2019-05-15 17:54:26 +02:00
|
|
|
}
|
|
|
|
}
|
2018-08-30 20:48:43 +02:00
|
|
|
else
|
2018-08-02 23:26:14 +02:00
|
|
|
{
|
2018-08-30 20:48:43 +02:00
|
|
|
// single threaded mode
|
2019-03-03 16:01:05 +01:00
|
|
|
llarp::util::Lock lock(&pool->m_access);
|
2018-11-19 12:56:40 +01:00
|
|
|
pool->jobs.emplace(std::bind(job.work, job.user));
|
2018-08-02 23:26:14 +02:00
|
|
|
}
|
2018-06-06 14:46:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
llarp_threadpool_tick(struct llarp_threadpool *pool)
|
|
|
|
{
|
2019-03-03 21:51:47 +01:00
|
|
|
while(pool->size())
|
2018-06-06 14:46:26 +02:00
|
|
|
{
|
2018-11-19 12:56:40 +01:00
|
|
|
std::function< void(void) > job;
|
2018-08-02 23:26:14 +02:00
|
|
|
{
|
2019-03-03 16:01:05 +01:00
|
|
|
llarp::util::Lock lock(&pool->m_access);
|
2018-08-30 20:48:43 +02:00
|
|
|
job = std::move(pool->jobs.front());
|
2018-08-02 23:26:14 +02:00
|
|
|
pool->jobs.pop();
|
|
|
|
}
|
2018-12-27 20:10:38 +01:00
|
|
|
if(job)
|
2019-03-03 21:51:47 +01:00
|
|
|
{
|
2018-12-27 20:10:38 +01:00
|
|
|
job();
|
2019-03-03 21:51:47 +01:00
|
|
|
}
|
2018-06-06 14:46:26 +02:00
|
|
|
}
|
2018-01-31 20:59:26 +01:00
|
|
|
}
|
|
|
|
|
2018-05-22 17:54:19 +02:00
|
|
|
void
|
|
|
|
llarp_free_threadpool(struct llarp_threadpool **pool)
|
|
|
|
{
|
2018-05-27 18:45:04 +02:00
|
|
|
if(*pool)
|
|
|
|
{
|
|
|
|
delete *pool;
|
|
|
|
}
|
2018-01-29 15:27:24 +01:00
|
|
|
*pool = nullptr;
|
|
|
|
}
|