lokinet/llarp/threadpool.cpp

100 lines
2.2 KiB
C++
Raw Normal View History

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