lokinet/llarp/util/threadpool.cpp

114 lines
2.0 KiB
C++
Raw Normal View History

#include <util/logger.hpp>
#include <util/threadpool.hpp>
#include <util/time.hpp>
2018-06-06 12:46:26 +00:00
#include <cstring>
#include <functional>
2018-06-06 12:46:26 +00:00
#include <queue>
struct llarp_threadpool
{
2018-11-19 11:56:40 +00:00
std::unique_ptr< llarp::thread::Pool > impl;
2018-06-06 12:46:26 +00:00
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;
2018-11-19 11:56:40 +00:00
std::queue< std::function< void(void) > > jobs;
2018-01-29 14:19:00 +00:00
2018-11-19 11:56:40 +00:00
llarp_threadpool(int workers, const char *name)
2018-06-06 12:46:26 +00:00
{
2018-11-19 11:56:40 +00:00
(void)name;
impl.reset(new llarp::thread::Pool(workers, workers * 128));
2018-06-06 12:46:26 +00:00
}
2018-11-19 11:56:40 +00:00
llarp_threadpool()
{
}
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;
2018-11-19 11:56:40 +00:00
return new llarp_threadpool(workers, name);
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();
}
void
llarp_threadpool_join(struct llarp_threadpool *pool)
{
llarp::LogDebug("threadpool join");
2018-06-06 12:46:26 +00:00
if(pool->impl)
2018-11-19 11:56:40 +00:00
pool->impl->drain();
}
2018-01-29 14:19:00 +00:00
void
2018-11-19 11:56:40 +00:00
llarp_threadpool_start(struct llarp_threadpool *pool)
{
if(pool->impl)
pool->impl->start();
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)
2018-11-19 11:56:40 +00:00
pool->impl->stop();
}
2018-04-30 14:57:13 +00:00
void
llarp_threadpool_wait(struct llarp_threadpool *pool)
{
llarp::LogDebug("threadpool wait");
2018-06-06 12:46:26 +00:00
if(pool->impl)
2018-04-30 14:57:13 +00:00
{
2018-11-19 11:56:40 +00:00
pool->impl->drain();
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)
2018-11-19 11:56:40 +00:00
pool->impl->addJob(std::bind(job.work, job.user));
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);
2018-11-19 11:56:40 +00:00
pool->jobs.emplace(std::bind(job.work, job.user));
}
2018-06-06 12:46:26 +00:00
}
void
llarp_threadpool_tick(struct llarp_threadpool *pool)
{
while(pool->jobs.size())
{
2018-11-19 11:56:40 +00:00
std::function< void(void) > 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-12-27 19:10:38 +00:00
if(job)
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;
}