|
|
@ -3,6 +3,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
#include <util/threading.hpp>
|
|
|
|
#include <util/threading.hpp>
|
|
|
|
#include <util/threadpool.hpp>
|
|
|
|
#include <util/threadpool.hpp>
|
|
|
|
|
|
|
|
#include <util/queue.hpp>
|
|
|
|
|
|
|
|
|
|
|
|
#include <absl/base/thread_annotations.h>
|
|
|
|
#include <absl/base/thread_annotations.h>
|
|
|
|
#include <memory>
|
|
|
|
#include <memory>
|
|
|
@ -11,39 +12,40 @@
|
|
|
|
struct llarp_threadpool
|
|
|
|
struct llarp_threadpool
|
|
|
|
{
|
|
|
|
{
|
|
|
|
std::unique_ptr< llarp::thread::ThreadPool > impl;
|
|
|
|
std::unique_ptr< llarp::thread::ThreadPool > impl;
|
|
|
|
|
|
|
|
std::unique_ptr< llarp::thread::Queue< std::function< void(void) > > > jobs;
|
|
|
|
mutable llarp::util::Mutex m_access; // protects jobs
|
|
|
|
const pid_t callingPID;
|
|
|
|
std::queue< std::function< void(void) > > jobs GUARDED_BY(m_access);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
llarp_threadpool(int workers, const char *name)
|
|
|
|
llarp_threadpool(int workers, const char *name)
|
|
|
|
: impl(
|
|
|
|
: impl(
|
|
|
|
std::make_unique< llarp::thread::ThreadPool >(workers, workers * 128))
|
|
|
|
std::make_unique< llarp::thread::ThreadPool >(workers, workers * 128))
|
|
|
|
|
|
|
|
, jobs(nullptr)
|
|
|
|
|
|
|
|
, callingPID(0)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
(void)name;
|
|
|
|
(void)name;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
llarp_threadpool()
|
|
|
|
llarp_threadpool()
|
|
|
|
|
|
|
|
: jobs(new llarp::thread::Queue< std::function< void(void) > >(128))
|
|
|
|
|
|
|
|
, callingPID(::getpid())
|
|
|
|
{
|
|
|
|
{
|
|
|
|
|
|
|
|
jobs->enable();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
size_t
|
|
|
|
size_t
|
|
|
|
size() const LOCKS_EXCLUDED(m_access)
|
|
|
|
size() const
|
|
|
|
{
|
|
|
|
{
|
|
|
|
absl::ReaderMutexLock l(&m_access);
|
|
|
|
if(jobs)
|
|
|
|
return jobs.size();
|
|
|
|
return jobs->size();
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool
|
|
|
|
bool
|
|
|
|
QueueFunc(std::function< void(void) > f) LOCKS_EXCLUDED(m_access)
|
|
|
|
QueueFunc(std::function< void(void) > f)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
if(impl)
|
|
|
|
if(impl)
|
|
|
|
return impl->tryAddJob(f);
|
|
|
|
return impl->tryAddJob(f);
|
|
|
|
else
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return jobs->tryPushBack(f) == llarp::thread::QueueReturn::Success;
|
|
|
|
llarp::util::Lock lock(&m_access);
|
|
|
|
|
|
|
|
jobs.emplace(f);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
@ -54,14 +56,6 @@ llarp_init_threadpool(int workers, const char *name);
|
|
|
|
struct llarp_threadpool *
|
|
|
|
struct llarp_threadpool *
|
|
|
|
llarp_init_same_process_threadpool();
|
|
|
|
llarp_init_same_process_threadpool();
|
|
|
|
|
|
|
|
|
|
|
|
typedef bool (*setup_net_func)(void *, bool);
|
|
|
|
|
|
|
|
typedef void (*run_main_func)(void *);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// for network isolation
|
|
|
|
|
|
|
|
struct llarp_threadpool *
|
|
|
|
|
|
|
|
llarp_init_isolated_net_threadpool(const char *name, setup_net_func setupNet,
|
|
|
|
|
|
|
|
run_main_func runMain, void *context);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
void
|
|
|
|
llarp_free_threadpool(struct llarp_threadpool **tp);
|
|
|
|
llarp_free_threadpool(struct llarp_threadpool **tp);
|
|
|
|
|
|
|
|
|
|
|
|