lokinet/llarp/threadpool.cpp

193 lines
3.5 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-06-06 14:46:26 +02:00
#include <llarp/time.h>
#include <queue>
2018-05-28 22:51:15 +02:00
#include "logger.hpp"
2018-01-29 15:19:00 +01:00
#if(__FreeBSD__)
#include <pthread_np.h>
#endif
namespace llarp
{
namespace thread
{
Pool::Pool(size_t workers, const char *name)
{
stop = false;
while(workers--)
{
threads.emplace_back([this, name] {
if(name)
{
#if(__APPLE__ && __MACH__)
pthread_setname_np(name);
#elif(__FreeBSD__)
pthread_set_name_np(pthread_self(), name);
#else
pthread_setname_np(pthread_self(), name);
#endif
}
for(;;)
{
2018-06-06 14:46:26 +02:00
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;
2018-06-06 14:46:26 +02:00
job = this->jobs.front();
this->jobs.pop_front();
}
2018-06-06 14:46:26 +02:00
auto now = llarp_time_now_ms();
// do work
2018-06-06 14:46:26 +02:00
job->work(job->user);
auto after = llarp_time_now_ms();
auto dlt = after - now;
2018-06-07 18:30:54 +02:00
if(dlt > 10)
2018-06-06 14:46:26 +02:00
llarp::Warn("work took ", dlt, " ms");
delete job;
}
});
}
}
void
Pool::Stop()
{
{
lock_t lock(queue_mutex);
stop = true;
}
condition.notify_all();
}
void
Pool::Join()
{
for(auto &t : threads)
t.join();
threads.clear();
2018-05-28 22:51:15 +02:00
done.notify_all();
}
void
Pool::QueueJob(const llarp_thread_job &job)
{
{
lock_t lock(queue_mutex);
// don't allow enqueueing after stopping the pool
if(stop)
return;
2018-06-06 14:46:26 +02:00
jobs.push_back(new llarp_thread_job(job.user, job.work));
}
condition.notify_one();
}
2018-01-29 15:19:00 +01:00
} // namespace thread
2018-02-01 14:21:00 +01:00
} // namespace llarp
2018-01-29 15:27:24 +01:00
struct llarp_threadpool
{
2018-06-06 14:46:26 +02:00
llarp::thread::Pool *impl;
std::queue< llarp_thread_job > jobs;
2018-01-29 15:19:00 +01:00
2018-06-06 14:46:26 +02:00
llarp_threadpool(int workers, const char *name)
: impl(new llarp::thread::Pool(workers, name))
{
}
llarp_threadpool() : impl(nullptr)
{
}
2018-01-29 15:19:00 +01:00
};
extern "C" {
struct llarp_threadpool *
llarp_init_threadpool(int workers, const char *name)
{
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-06-06 14:46:26 +02:00
struct llarp_threadpool *
llarp_init_same_process_threadpool()
{
return new llarp_threadpool();
}
void
llarp_threadpool_join(struct llarp_threadpool *pool)
{
llarp::Debug("threadpool join");
2018-06-06 14:46:26 +02:00
if(pool->impl)
pool->impl->Join();
}
2018-01-29 15:19:00 +01:00
void
llarp_threadpool_start(struct llarp_threadpool *pool)
{ /** no op */
2018-02-01 14:21:00 +01:00
}
void
llarp_threadpool_stop(struct llarp_threadpool *pool)
{
llarp::Debug("threadpool stop");
2018-06-06 14:46:26 +02:00
if(pool->impl)
pool->impl->Stop();
}
2018-04-30 16:57:13 +02:00
void
llarp_threadpool_wait(struct llarp_threadpool *pool)
{
2018-04-30 16:57:13 +02:00
std::mutex mtx;
llarp::Debug("threadpool wait");
2018-06-06 14:46:26 +02:00
if(pool->impl)
2018-04-30 16:57:13 +02:00
{
std::unique_lock< std::mutex > lock(mtx);
2018-06-06 14:46:26 +02:00
pool->impl->done.wait(lock);
2018-04-30 16:57:13 +02:00
}
}
void
llarp_threadpool_queue_job(struct llarp_threadpool *pool,
struct llarp_thread_job job)
{
2018-06-06 14:46:26 +02:00
if(pool->impl)
pool->impl->QueueJob(job);
else
pool->jobs.push(job);
}
void
llarp_threadpool_tick(struct llarp_threadpool *pool)
{
while(pool->jobs.size())
{
auto &job = pool->jobs.front();
job.work(job.user);
pool->jobs.pop();
}
2018-01-31 20:59:26 +01:00
}
void
llarp_free_threadpool(struct llarp_threadpool **pool)
{
if(*pool)
{
delete *pool;
}
2018-01-29 15:27:24 +01:00
*pool = nullptr;
}
2018-01-29 15:19:00 +01:00
}