lokinet/llarp/util/thread/threadpool.h

99 lines
2.1 KiB
C
Raw Normal View History

2018-01-27 01:18:10 +00:00
#ifndef LLARP_THREADPOOL_H
#define LLARP_THREADPOOL_H
#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>
#include <util/types.hpp>
#include <absl/base/thread_annotations.h>
#include <memory>
#include <queue>
struct llarp_threadpool;
#ifdef __cplusplus
struct llarp_threadpool
{
std::unique_ptr< llarp::thread::ThreadPool > impl;
llarp_threadpool(int workers, llarp::string_view name,
2019-11-25 21:15:31 +00:00
size_t queueLength = size_t{1024 * 8})
: impl(std::make_unique< llarp::thread::ThreadPool >(
workers, std::max(queueLength, size_t{32}), name))
{
}
size_t
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;
/// see if this thread is full given lookahead amount
bool
LooksFull(size_t lookahead) const
{
return (pendingJobs() + lookahead) >= size();
}
};
#endif
2018-01-27 01:18:10 +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
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 */
struct llarp_thread_job
{
#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};
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;
#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);
bool
llarp_threadpool_queue_job(struct llarp_threadpool *tp,
struct llarp_thread_job j);
#ifdef __cplusplus
bool
llarp_threadpool_queue_job(struct llarp_threadpool *tp,
std::function< void(void) > func);
#endif
2018-11-19 16:40:49 +00:00
void
llarp_threadpool_start(struct llarp_threadpool *tp);
void
llarp_threadpool_stop(struct llarp_threadpool *tp);
2018-01-27 01:18:10 +00:00
#endif