2019-09-01 12:10:49 +00:00
|
|
|
#include <util/logging/logger.hpp>
|
2019-01-10 19:41:51 +00:00
|
|
|
#include <util/time.hpp>
|
2019-09-01 13:26:16 +00:00
|
|
|
#include <util/thread/threadpool.h>
|
|
|
|
#include <util/thread/thread_pool.hpp>
|
2018-06-06 12:46:26 +00:00
|
|
|
|
2018-12-12 02:52:51 +00:00
|
|
|
#include <cstring>
|
2018-08-08 17:43:46 +00:00
|
|
|
#include <functional>
|
2018-06-06 12:46:26 +00:00
|
|
|
#include <queue>
|
|
|
|
|
2018-05-22 15:54:19 +00:00
|
|
|
struct llarp_threadpool *
|
2019-11-25 21:30:34 +00:00
|
|
|
llarp_init_threadpool(int workers, const char *name, size_t queueLength)
|
2018-05-22 15:54:19 +00:00
|
|
|
{
|
2018-09-07 20:48:52 +00:00
|
|
|
if(workers <= 0)
|
|
|
|
workers = 1;
|
2019-11-25 21:30:34 +00:00
|
|
|
return new llarp_threadpool(workers, name, queueLength);
|
2018-01-29 14:27:24 +00:00
|
|
|
}
|
2018-01-29 14:19:00 +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)
|
2019-11-14 15:06:53 +00:00
|
|
|
pool->impl->stop();
|
|
|
|
pool->impl.reset();
|
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
|
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
|
|
|
}
|
|
|
|
|
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)
|
2019-11-14 15:06:53 +00:00
|
|
|
pool->impl->disable();
|
2018-05-22 15:54:19 +00:00
|
|
|
}
|
2018-04-30 14:57:13 +00:00
|
|
|
|
2019-11-14 15:06:53 +00:00
|
|
|
bool
|
2018-05-22 15:54:19 +00:00
|
|
|
llarp_threadpool_queue_job(struct llarp_threadpool *pool,
|
|
|
|
struct llarp_thread_job job)
|
2019-06-04 18:31:17 +00:00
|
|
|
{
|
2019-11-14 15:06:53 +00:00
|
|
|
return llarp_threadpool_queue_job(pool, std::bind(job.work, job.user));
|
2019-06-04 18:31:17 +00:00
|
|
|
}
|
|
|
|
|
2019-11-14 15:06:53 +00:00
|
|
|
bool
|
2019-06-04 18:31:17 +00:00
|
|
|
llarp_threadpool_queue_job(struct llarp_threadpool *pool,
|
2019-11-14 15:06:53 +00:00
|
|
|
std::function< void(void) > func)
|
2018-05-22 15:54:19 +00:00
|
|
|
{
|
2019-11-14 15:06:53 +00:00
|
|
|
return pool->impl && pool->impl->addJob(func);
|
2018-06-06 12:46:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
llarp_threadpool_tick(struct llarp_threadpool *pool)
|
|
|
|
{
|
2019-11-14 15:06:53 +00:00
|
|
|
if(pool->impl)
|
2018-06-06 12:46:26 +00:00
|
|
|
{
|
2019-11-14 15:06:53 +00:00
|
|
|
pool->impl->drain();
|
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;
|
|
|
|
}
|
2019-11-14 15:06:53 +00:00
|
|
|
|
|
|
|
size_t
|
|
|
|
llarp_threadpool::size() const
|
|
|
|
{
|
|
|
|
return impl ? impl->capacity() : 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t
|
|
|
|
llarp_threadpool::pendingJobs() const
|
|
|
|
{
|
|
|
|
return impl ? impl->jobCount() : 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t
|
|
|
|
llarp_threadpool::numThreads() const
|
|
|
|
{
|
|
|
|
return impl ? impl->activeThreadCount() : 0;
|
|
|
|
}
|