2018-01-27 01:18:10 +00:00
|
|
|
#ifndef LLARP_THREADPOOL_H
|
|
|
|
#define LLARP_THREADPOOL_H
|
|
|
|
|
2019-05-22 17:18:19 +00:00
|
|
|
#include <util/queue.hpp>
|
2019-07-09 00:06:22 +00:00
|
|
|
#include <util/string_view.hpp>
|
2019-07-02 19:00:24 +00:00
|
|
|
#include <util/thread_pool.hpp>
|
2019-07-17 09:23:46 +00:00
|
|
|
#include <util/threading.hpp>
|
2019-07-09 00:06:22 +00:00
|
|
|
|
2019-03-03 20:51:47 +00:00
|
|
|
#include <absl/base/thread_annotations.h>
|
2019-02-11 14:43:48 +00:00
|
|
|
#include <memory>
|
|
|
|
#include <queue>
|
|
|
|
|
|
|
|
struct llarp_threadpool
|
|
|
|
{
|
|
|
|
std::unique_ptr< llarp::thread::ThreadPool > impl;
|
2019-05-22 17:18:19 +00:00
|
|
|
std::unique_ptr< llarp::thread::Queue< std::function< void(void) > > > jobs;
|
|
|
|
const pid_t callingPID;
|
2019-02-11 14:43:48 +00:00
|
|
|
|
2019-07-09 00:06:22 +00:00
|
|
|
llarp_threadpool(int workers, llarp::string_view name)
|
|
|
|
: impl(std::make_unique< llarp::thread::ThreadPool >(workers,
|
|
|
|
workers * 128, name))
|
2019-05-22 17:18:19 +00:00
|
|
|
, jobs(nullptr)
|
|
|
|
, callingPID(0)
|
2019-02-11 14:43:48 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
llarp_threadpool()
|
2019-05-22 17:18:19 +00:00
|
|
|
: jobs(new llarp::thread::Queue< std::function< void(void) > >(128))
|
2019-07-17 09:23:46 +00:00
|
|
|
, callingPID(llarp::util::GetPid())
|
2019-02-11 14:43:48 +00:00
|
|
|
{
|
2019-05-22 17:18:19 +00:00
|
|
|
jobs->enable();
|
2019-02-11 14:43:48 +00:00
|
|
|
}
|
2019-03-03 20:51:47 +00:00
|
|
|
|
|
|
|
size_t
|
2019-05-22 17:18:19 +00:00
|
|
|
size() const
|
2019-03-03 20:51:47 +00:00
|
|
|
{
|
2019-05-22 17:18:19 +00:00
|
|
|
if(jobs)
|
|
|
|
return jobs->size();
|
|
|
|
return 0;
|
2019-03-03 20:51:47 +00:00
|
|
|
}
|
2019-04-17 19:05:54 +00:00
|
|
|
|
2019-05-15 15:54:26 +00:00
|
|
|
bool
|
2019-05-22 17:18:19 +00:00
|
|
|
QueueFunc(std::function< void(void) > f)
|
2019-04-17 19:05:54 +00:00
|
|
|
{
|
|
|
|
if(impl)
|
2019-05-15 15:54:26 +00:00
|
|
|
return impl->tryAddJob(f);
|
2019-07-06 17:03:40 +00:00
|
|
|
|
|
|
|
return jobs->tryPushBack(f) == llarp::thread::QueueReturn::Success;
|
2019-04-17 19:05:54 +00:00
|
|
|
}
|
2019-02-11 14:43:48 +00:00
|
|
|
};
|
2018-01-27 01:18:10 +00:00
|
|
|
|
2018-05-22 15:54:19 +00:00
|
|
|
struct llarp_threadpool *
|
|
|
|
llarp_init_threadpool(int workers, const char *name);
|
2018-06-06 12:46:26 +00:00
|
|
|
|
|
|
|
/// for single process mode
|
|
|
|
struct llarp_threadpool *
|
|
|
|
llarp_init_same_process_threadpool();
|
|
|
|
|
2018-05-22 15:54:19 +00:00
|
|
|
void
|
|
|
|
llarp_free_threadpool(struct llarp_threadpool **tp);
|
2018-01-27 01:18:10 +00:00
|
|
|
|
2018-02-01 13:21:00 +00:00
|
|
|
typedef void (*llarp_thread_work_func)(void *);
|
2018-01-31 19:59:26 +00:00
|
|
|
|
2018-01-29 14:27:24 +00:00
|
|
|
/** job to be done in worker thread */
|
2018-05-22 15:54:19 +00:00
|
|
|
struct llarp_thread_job
|
|
|
|
{
|
2018-01-29 14:27:24 +00:00
|
|
|
/** user data to pass to work function */
|
|
|
|
void *user;
|
|
|
|
/** called in threadpool worker thread */
|
2018-01-31 19:59:26 +00:00
|
|
|
llarp_thread_work_func work;
|
2018-07-16 04:55:46 +00:00
|
|
|
#ifdef __cplusplus
|
2018-06-06 12:46:26 +00:00
|
|
|
llarp_thread_job(void *u, llarp_thread_work_func w) : user(u), work(w)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
llarp_thread_job() : user(nullptr), work(nullptr)
|
|
|
|
{
|
|
|
|
}
|
2018-07-16 04:55:46 +00:00
|
|
|
#endif
|
2018-01-29 14:27:24 +00:00
|
|
|
};
|
2018-01-27 01:18:10 +00:00
|
|
|
|
2018-06-06 12:46:26 +00:00
|
|
|
/// for single process mode
|
|
|
|
void
|
|
|
|
llarp_threadpool_tick(struct llarp_threadpool *tp);
|
|
|
|
|
2018-05-22 15:54:19 +00:00
|
|
|
void
|
|
|
|
llarp_threadpool_queue_job(struct llarp_threadpool *tp,
|
|
|
|
struct llarp_thread_job j);
|
2019-06-04 18:31:17 +00:00
|
|
|
|
|
|
|
#ifdef __cplusplus
|
|
|
|
|
|
|
|
void
|
|
|
|
llarp_threadpool_queue_job(struct llarp_threadpool *tp,
|
|
|
|
std::function< void() > func);
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2018-11-19 16:40:49 +00:00
|
|
|
void
|
|
|
|
llarp_threadpool_start(struct llarp_threadpool *tp);
|
2018-01-29 14:27:24 +00:00
|
|
|
|
2018-05-22 15:54:19 +00:00
|
|
|
void
|
|
|
|
llarp_threadpool_stop(struct llarp_threadpool *tp);
|
|
|
|
void
|
|
|
|
llarp_threadpool_join(struct llarp_threadpool *tp);
|
2018-04-30 16:14:20 +00:00
|
|
|
|
2018-05-22 15:54:19 +00:00
|
|
|
void
|
|
|
|
llarp_threadpool_wait(struct llarp_threadpool *tp);
|
2018-01-27 01:18:10 +00:00
|
|
|
|
|
|
|
#endif
|