You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
lokinet/llarp/util/thread/threadpool.h

95 lines
2.0 KiB
C

#ifndef LLARP_THREADPOOL_H
#define LLARP_THREADPOOL_H
#include <util/string_view.hpp>
#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
4 years ago
#include <util/thread/annotations.hpp>
#include <util/types.hpp>
#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,
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
struct llarp_threadpool *
llarp_init_threadpool(int workers, const char *name, size_t queueLength);
void
llarp_free_threadpool(struct llarp_threadpool **tp);
using llarp_thread_work_func = void (*)(void *);
7 years ago
7 years ago
/** job to be done in worker thread */
struct llarp_thread_job
{
#ifdef __cplusplus
7 years ago
/** user data to pass to work function */
void *user{nullptr};
7 years ago
/** called in threadpool worker thread */
llarp_thread_work_func work{nullptr};
llarp_thread_job(void *u, llarp_thread_work_func w) : user(u), work(w)
{
}
llarp_thread_job() = default;
#else
void *user;
llarp_thread_work_func work;
#endif
7 years ago
};
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
void
llarp_threadpool_start(struct llarp_threadpool *tp);
void
llarp_threadpool_stop(struct llarp_threadpool *tp);
#endif