mirror of https://github.com/oxen-io/lokinet
You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
114 lines
2.0 KiB
C++
114 lines
2.0 KiB
C++
6 years ago
|
#include <util/logger.hpp>
|
||
|
#include <util/threadpool.hpp>
|
||
|
#include <util/time.hpp>
|
||
6 years ago
|
|
||
6 years ago
|
#include <cstring>
|
||
6 years ago
|
#include <functional>
|
||
6 years ago
|
#include <queue>
|
||
|
|
||
6 years ago
|
struct llarp_threadpool
|
||
|
{
|
||
6 years ago
|
std::unique_ptr< llarp::thread::Pool > impl;
|
||
6 years ago
|
|
||
6 years ago
|
llarp::util::Mutex m_access;
|
||
6 years ago
|
uint32_t ids = 0;
|
||
6 years ago
|
std::queue< std::function< void(void) > > jobs;
|
||
7 years ago
|
|
||
6 years ago
|
llarp_threadpool(int workers, const char *name)
|
||
6 years ago
|
{
|
||
6 years ago
|
(void)name;
|
||
|
impl.reset(new llarp::thread::Pool(workers, workers * 128));
|
||
6 years ago
|
}
|
||
|
|
||
6 years ago
|
llarp_threadpool()
|
||
6 years ago
|
{
|
||
|
}
|
||
7 years ago
|
};
|
||
|
|
||
6 years ago
|
struct llarp_threadpool *
|
||
|
llarp_init_threadpool(int workers, const char *name)
|
||
|
{
|
||
6 years ago
|
if(workers <= 0)
|
||
|
workers = 1;
|
||
6 years ago
|
return new llarp_threadpool(workers, name);
|
||
7 years ago
|
}
|
||
7 years ago
|
|
||
6 years ago
|
struct llarp_threadpool *
|
||
|
llarp_init_same_process_threadpool()
|
||
|
{
|
||
|
return new llarp_threadpool();
|
||
|
}
|
||
|
|
||
6 years ago
|
void
|
||
|
llarp_threadpool_join(struct llarp_threadpool *pool)
|
||
|
{
|
||
6 years ago
|
llarp::LogDebug("threadpool join");
|
||
6 years ago
|
if(pool->impl)
|
||
6 years ago
|
pool->impl->drain();
|
||
6 years ago
|
}
|
||
7 years ago
|
|
||
6 years ago
|
void
|
||
6 years ago
|
llarp_threadpool_start(struct llarp_threadpool *pool)
|
||
|
{
|
||
|
if(pool->impl)
|
||
|
pool->impl->start();
|
||
7 years ago
|
}
|
||
|
|
||
6 years ago
|
void
|
||
|
llarp_threadpool_stop(struct llarp_threadpool *pool)
|
||
|
{
|
||
6 years ago
|
llarp::LogDebug("threadpool stop");
|
||
6 years ago
|
if(pool->impl)
|
||
6 years ago
|
pool->impl->stop();
|
||
6 years ago
|
}
|
||
7 years ago
|
|
||
6 years ago
|
void
|
||
|
llarp_threadpool_wait(struct llarp_threadpool *pool)
|
||
|
{
|
||
6 years ago
|
llarp::LogDebug("threadpool wait");
|
||
6 years ago
|
if(pool->impl)
|
||
7 years ago
|
{
|
||
6 years ago
|
pool->impl->drain();
|
||
7 years ago
|
}
|
||
|
}
|
||
|
|
||
6 years ago
|
void
|
||
|
llarp_threadpool_queue_job(struct llarp_threadpool *pool,
|
||
|
struct llarp_thread_job job)
|
||
|
{
|
||
6 years ago
|
if(pool->impl)
|
||
6 years ago
|
pool->impl->addJob(std::bind(job.work, job.user));
|
||
6 years ago
|
else
|
||
6 years ago
|
{
|
||
6 years ago
|
// single threaded mode
|
||
|
llarp::util::Lock lock(pool->m_access);
|
||
6 years ago
|
pool->jobs.emplace(std::bind(job.work, job.user));
|
||
6 years ago
|
}
|
||
6 years ago
|
}
|
||
|
|
||
|
void
|
||
|
llarp_threadpool_tick(struct llarp_threadpool *pool)
|
||
|
{
|
||
|
while(pool->jobs.size())
|
||
|
{
|
||
6 years ago
|
std::function< void(void) > job;
|
||
6 years ago
|
{
|
||
6 years ago
|
llarp::util::Lock lock(pool->m_access);
|
||
6 years ago
|
job = std::move(pool->jobs.front());
|
||
6 years ago
|
pool->jobs.pop();
|
||
|
}
|
||
6 years ago
|
if(job)
|
||
|
job();
|
||
6 years ago
|
}
|
||
7 years ago
|
}
|
||
|
|
||
6 years ago
|
void
|
||
|
llarp_free_threadpool(struct llarp_threadpool **pool)
|
||
|
{
|
||
6 years ago
|
if(*pool)
|
||
|
{
|
||
|
delete *pool;
|
||
|
}
|
||
7 years ago
|
*pool = nullptr;
|
||
|
}
|