lokinet/llarp/util/thread/threadpool.h

92 lines
1.9 KiB
C
Raw Normal View History

2018-01-27 01:18:10 +00:00
#ifndef LLARP_THREADPOOL_H
#define LLARP_THREADPOOL_H
2019-09-01 13:26:16 +00:00
#include <util/thread/queue.hpp>
#include <util/thread/thread_pool.hpp>
#include <util/thread/threading.hpp>
De-abseil, part 2: mutex, locks, (most) time - util::Mutex is now a std::shared_timed_mutex, which is capable of exclusive and shared locks. - util::Lock is still present as a std::lock_guard<util::Mutex>. - the locking annotations are preserved, but updated to the latest supported by clang rather than using abseil's older/deprecated ones. - ACQUIRE_LOCK macro is gone since we don't pass mutexes by pointer into locks anymore (WTF abseil). - ReleasableLock is gone. Instead there are now some llarp::util helper methods to obtain unique and/or shared locks: - `auto lock = util::unique_lock(mutex);` gets an RAII-but-also unlockable object (std::unique_lock<T>, with T inferred from `mutex`). - `auto lock = util::shared_lock(mutex);` gets an RAII shared (i.e. "reader") lock of the mutex. - `auto lock = util::unique_locks(mutex1, mutex2, mutex3);` can be used to atomically lock multiple mutexes at once (returning a tuple of the locks). This are templated on the mutex which makes them a bit more flexible than using a concrete type: they can be used for any type of lockable mutex, not only util::Mutex. (Some of the code here uses them for getting locks around a std::mutex). Until C++17, using the RAII types is painfully verbose: ```C++ // pre-C++17 - needing to figure out the mutex type here is annoying: std::unique_lock<util::Mutex> lock(mutex); // pre-C++17 and even more verbose (but at least the type isn't needed): std::unique_lock<decltype(mutex)> lock(mutex); // our compromise: auto lock = util::unique_lock(mutex); // C++17: std::unique_lock lock(mutex); ``` All of these functions will also warn (under gcc or clang) if you discard the return value. You can also do fancy things like `auto l = util::unique_lock(mutex, std::adopt_lock)` (which lets a lock take over an already-locked mutex). - metrics code is gone, which also removes a big pile of code that was only used by metrics: - llarp::util::Scheduler - llarp::thread::TimerQueue - llarp::util::Stopwatch
2020-02-21 17:21:11 +00:00
#include <util/thread/annotations.hpp>
#include <util/types.hpp>
#include <memory>
#include <queue>
#include <string_view>
struct llarp_threadpool;
#ifdef __cplusplus
struct llarp_threadpool
{
std::unique_ptr<llarp::thread::ThreadPool> impl;
llarp_threadpool(int workers, std::string_view name, 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;
/// 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*
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
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 */
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};
llarp_thread_job(void* u, llarp_thread_work_func w) : user(u), work(w)
2018-06-06 12:46:26 +00:00
{
}
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);
2018-06-06 12:46:26 +00:00
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