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>
|
2019-11-14 15:06:53 +00:00
|
|
|
#include <util/types.hpp>
|
2019-07-09 00:06:22 +00:00
|
|
|
|
2019-02-11 14:43:48 +00:00
|
|
|
#include <memory>
|
|
|
|
#include <queue>
|
2020-05-01 19:51:15 +00:00
|
|
|
#include <string_view>
|
2019-02-11 14:43:48 +00:00
|
|
|
|
2019-11-14 15:06:53 +00:00
|
|
|
struct llarp_threadpool;
|
|
|
|
|
|
|
|
#ifdef __cplusplus
|
2019-02-11 14:43:48 +00:00
|
|
|
struct llarp_threadpool
|
|
|
|
{
|
2020-04-07 18:38:56 +00:00
|
|
|
std::unique_ptr<llarp::thread::ThreadPool> impl;
|
2019-02-11 14:43:48 +00:00
|
|
|
|
2020-05-01 19:51:15 +00:00
|
|
|
llarp_threadpool(int workers, std::string_view name, size_t queueLength = size_t{1024 * 8})
|
2020-04-07 18:38:56 +00:00
|
|
|
: impl(std::make_unique<llarp::thread::ThreadPool>(
|
2019-11-14 15:06:53 +00:00
|
|
|
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;
|
|
|
|
|
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
|
|
|
|
2020-04-07 18:38:56 +00:00
|
|
|
struct llarp_threadpool*
|
|
|
|
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
|
2020-04-07 18:38:56 +00:00
|
|
|
llarp_free_threadpool(struct llarp_threadpool** tp);
|
2018-01-27 01:18:10 +00:00
|
|
|
|
2020-04-07 18:38:56 +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 */
|
2020-04-07 18:38:56 +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
|
|
|
|
2020-04-07 18:38:56 +00:00
|
|
|
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;
|
2019-11-14 15:06:53 +00:00
|
|
|
#else
|
2020-04-07 18:38:56 +00:00
|
|
|
void* user;
|
2019-11-14 15:06:53 +00:00
|
|
|
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
|
2020-04-07 18:38:56 +00:00
|
|
|
llarp_threadpool_tick(struct llarp_threadpool* tp);
|
2018-06-06 12:46:26 +00:00
|
|
|
|
2019-11-14 15:06:53 +00:00
|
|
|
bool
|
2020-04-07 18:38:56 +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
|
2020-04-07 18:38:56 +00:00
|
|
|
llarp_threadpool_queue_job(struct llarp_threadpool* tp, std::function<void(void)> func);
|
2019-06-04 18:31:17 +00:00
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2018-11-19 16:40:49 +00:00
|
|
|
void
|
2020-04-07 18:38:56 +00:00
|
|
|
llarp_threadpool_start(struct llarp_threadpool* tp);
|
2018-05-22 15:54:19 +00:00
|
|
|
void
|
2020-04-07 18:38:56 +00:00
|
|
|
llarp_threadpool_stop(struct llarp_threadpool* tp);
|
2018-01-27 01:18:10 +00:00
|
|
|
|
|
|
|
#endif
|