2018-01-29 14:19:00 +00:00
|
|
|
#include "threadpool.hpp"
|
2018-05-20 18:56:34 +00:00
|
|
|
#include <pthread.h>
|
|
|
|
#include <cstring>
|
2018-01-29 14:19:00 +00:00
|
|
|
|
2018-05-22 15:54:19 +00:00
|
|
|
namespace llarp
|
|
|
|
{
|
|
|
|
namespace thread
|
|
|
|
{
|
|
|
|
Pool::Pool(size_t workers, const char *name)
|
|
|
|
{
|
|
|
|
stop = false;
|
|
|
|
while(workers--)
|
|
|
|
{
|
|
|
|
threads.emplace_back([this] {
|
|
|
|
|
|
|
|
for(;;)
|
|
|
|
{
|
|
|
|
llarp_thread_job job;
|
|
|
|
{
|
|
|
|
lock_t lock(this->queue_mutex);
|
|
|
|
this->condition.wait(
|
|
|
|
lock, [this] { return this->stop || !this->jobs.empty(); });
|
|
|
|
if(this->stop && this->jobs.empty())
|
|
|
|
return;
|
|
|
|
job = std::move(this->jobs.front());
|
|
|
|
this->jobs.pop_front();
|
|
|
|
}
|
|
|
|
// do work
|
|
|
|
job.work(job.user);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2018-05-20 18:56:34 +00:00
|
|
|
if(name)
|
2018-05-22 15:54:19 +00:00
|
|
|
{
|
|
|
|
for(auto &t : threads)
|
|
|
|
pthread_setname_np(t.native_handle(), name);
|
2018-01-29 14:19:00 +00:00
|
|
|
}
|
2018-05-22 15:54:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Pool::Stop()
|
|
|
|
{
|
|
|
|
{
|
|
|
|
lock_t lock(queue_mutex);
|
|
|
|
stop = true;
|
|
|
|
}
|
|
|
|
condition.notify_all();
|
|
|
|
done.notify_all();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Pool::Join()
|
|
|
|
{
|
|
|
|
for(auto &t : threads)
|
|
|
|
t.join();
|
|
|
|
threads.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Pool::QueueJob(const llarp_thread_job &job)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
lock_t lock(queue_mutex);
|
|
|
|
|
|
|
|
// don't allow enqueueing after stopping the pool
|
|
|
|
if(stop)
|
|
|
|
throw std::runtime_error("enqueue on stopped ThreadPool");
|
|
|
|
|
|
|
|
jobs.emplace_back(job);
|
|
|
|
}
|
|
|
|
condition.notify_one();
|
|
|
|
}
|
2018-01-29 14:19:00 +00:00
|
|
|
|
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-01-29 14:19:00 +00:00
|
|
|
llarp::thread::Pool impl;
|
|
|
|
|
2018-05-22 15:54:19 +00:00
|
|
|
llarp_threadpool(int workers, const char *name) : impl(workers, name)
|
|
|
|
{
|
|
|
|
}
|
2018-01-29 14:19:00 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
extern "C" {
|
|
|
|
|
2018-05-22 15:54:19 +00:00
|
|
|
struct llarp_threadpool *
|
|
|
|
llarp_init_threadpool(int workers, const char *name)
|
|
|
|
{
|
|
|
|
if(workers > 0)
|
2018-05-20 18:56:34 +00:00
|
|
|
return new llarp_threadpool(workers, name);
|
2018-01-29 14:27:24 +00:00
|
|
|
else
|
|
|
|
return nullptr;
|
|
|
|
}
|
2018-01-29 14:19:00 +00:00
|
|
|
|
2018-05-22 15:54:19 +00:00
|
|
|
void
|
|
|
|
llarp_threadpool_join(struct llarp_threadpool *pool)
|
|
|
|
{
|
|
|
|
pool->impl.Join();
|
|
|
|
}
|
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)
|
|
|
|
{
|
|
|
|
pool->impl.Stop();
|
|
|
|
}
|
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-05-22 15:54:19 +00:00
|
|
|
std::unique_lock< std::mutex > lock(mtx);
|
2018-04-30 14:57:13 +00:00
|
|
|
pool->impl.done.wait(lock);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-22 15:54:19 +00:00
|
|
|
void
|
|
|
|
llarp_threadpool_queue_job(struct llarp_threadpool *pool,
|
|
|
|
struct llarp_thread_job job)
|
|
|
|
{
|
2018-02-01 13:21:00 +00:00
|
|
|
pool->impl.QueueJob(job);
|
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-01-29 14:27:24 +00:00
|
|
|
delete *pool;
|
|
|
|
*pool = nullptr;
|
|
|
|
}
|
2018-01-29 14:19:00 +00:00
|
|
|
}
|