2018-01-27 01:18:10 +00:00
|
|
|
#ifndef LLARP_THREADPOOL_H
|
|
|
|
#define LLARP_THREADPOOL_H
|
|
|
|
|
2019-07-09 00:06:22 +00:00
|
|
|
#include <util/string_view.hpp>
|
2019-09-01 13:26:16 +00:00
|
|
|
#include <util/thread/queue.hpp>
|
|
|
|
#include <util/thread/thread_pool.hpp>
|
|
|
|
#include <util/thread/threading.hpp>
|
2019-11-14 15:06:53 +00:00
|
|
|
#include <util/types.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>
|
|
|
|
|
2019-11-14 15:06:53 +00:00
|
|
|
struct llarp_threadpool;
|
|
|
|
|
|
|
|
#ifdef __cplusplus
|
2019-02-11 14:43:48 +00:00
|
|
|
struct llarp_threadpool
|
|
|
|
{
|
|
|
|
std::unique_ptr< llarp::thread::ThreadPool > impl;
|
|
|
|
|
2019-11-14 15:06:53 +00:00
|
|
|
llarp_threadpool(int workers, llarp::string_view name,
|
2019-11-25 21:15:31 +00:00
|
|
|
size_t queueLength = size_t{1024 * 8})
|
2019-11-14 15:06:53 +00:00
|
|
|
: impl(std::make_unique< llarp::thread::ThreadPool >(
|
|
|
|
workers, std::max(queueLength, size_t{32}), name))
|
2019-02-11 14:43:48 +00:00
|
|
|
{
|
|
|
|
}
|
2019-03-03 20:51:47 +00:00
|
|
|
|
|
|
|
size_t
|
2019-11-14 15:06:53 +00:00
|
|
|
size() const;
|
|
|
|
|
|
|
|
size_t
|
|
|
|
pendingJobs() const;
|
|
|
|
|
|
|
|
size_t
|
|
|
|
numThreads() const;
|
|
|
|
|
|
|
|
/// try to guess how big our job latency is on this threadpool
|
|
|
|
llarp_time_t
|
|
|
|
GuessJobLatency(llarp_time_t granulairty = 1000) const;
|
|
|
|
|
2019-11-14 17:18:20 +00:00
|
|
|
/// see if this thread is full given lookahead amount
|
2019-11-14 15:06:53 +00:00
|
|
|
bool
|
2019-11-14 17:18:20 +00:00
|
|
|
LooksFull(size_t lookahead) const
|
2019-03-03 20:51:47 +00:00
|
|
|
{
|
2019-11-14 17:18:20 +00:00
|
|
|
return (pendingJobs() + lookahead) >= size();
|
2019-03-03 20:51:47 +00:00
|
|
|
}
|
2019-02-11 14:43:48 +00:00
|
|
|
};
|
2019-11-14 15:06:53 +00:00
|
|
|
#endif
|
2018-01-27 01:18:10 +00:00
|
|
|
|
2018-05-22 15:54:19 +00:00
|
|
|
struct llarp_threadpool *
|
2019-11-25 21:30:34 +00:00
|
|
|
llarp_init_threadpool(int workers, const char *name, size_t queueLength);
|
2018-06-06 12:46:26 +00:00
|
|
|
|
2018-05-22 15:54:19 +00:00
|
|
|
void
|
|
|
|
llarp_free_threadpool(struct llarp_threadpool **tp);
|
2018-01-27 01:18:10 +00:00
|
|
|
|
2019-07-30 23:42:13 +00:00
|
|
|
using llarp_thread_work_func = void (*)(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
|
|
|
|
{
|
2019-11-14 15:06:53 +00:00
|
|
|
#ifdef __cplusplus
|
2018-01-29 14:27:24 +00:00
|
|
|
/** user data to pass to work function */
|
2019-07-30 23:42:13 +00:00
|
|
|
void *user{nullptr};
|
2018-01-29 14:27:24 +00:00
|
|
|
/** called in threadpool worker thread */
|
2019-07-30 23:42:13 +00:00
|
|
|
llarp_thread_work_func work{nullptr};
|
2019-11-14 15:06:53 +00:00
|
|
|
|
2018-06-06 12:46:26 +00:00
|
|
|
llarp_thread_job(void *u, llarp_thread_work_func w) : user(u), work(w)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2019-07-30 23:42:13 +00:00
|
|
|
llarp_thread_job() = default;
|
2019-11-14 15:06:53 +00:00
|
|
|
#else
|
|
|
|
void *user;
|
|
|
|
llarp_thread_work_func work;
|
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
|
|
|
void
|
|
|
|
llarp_threadpool_tick(struct llarp_threadpool *tp);
|
|
|
|
|
2019-11-14 15:06:53 +00:00
|
|
|
bool
|
2018-05-22 15:54:19 +00:00
|
|
|
llarp_threadpool_queue_job(struct llarp_threadpool *tp,
|
|
|
|
struct llarp_thread_job j);
|
2019-06-04 18:31:17 +00:00
|
|
|
|
|
|
|
#ifdef __cplusplus
|
|
|
|
|
2019-11-14 15:06:53 +00:00
|
|
|
bool
|
2019-06-04 18:31:17 +00:00
|
|
|
llarp_threadpool_queue_job(struct llarp_threadpool *tp,
|
2019-11-14 15:06:53 +00:00
|
|
|
std::function< void(void) > func);
|
2019-06-04 18:31:17 +00:00
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2018-11-19 16:40:49 +00:00
|
|
|
void
|
|
|
|
llarp_threadpool_start(struct llarp_threadpool *tp);
|
2018-05-22 15:54:19 +00:00
|
|
|
void
|
|
|
|
llarp_threadpool_stop(struct llarp_threadpool *tp);
|
2018-01-27 01:18:10 +00:00
|
|
|
|
|
|
|
#endif
|