mergerfs/vendored/libfuse/include/thread_pool.hpp

487 lines
10 KiB
C++
Raw Normal View History

/*
ISC License
Copyright (c) 2026, Antonio SJ Musumeci <trapexit@spawn.link>
Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#pragma once
#include "bounded_queue.hpp"
#include "invocable.h"
2026-02-20 22:27:55 -06:00
#include "mutex.hpp"
2023-09-24 17:55:09 -05:00
#include <algorithm>
#include <cerrno>
2023-08-14 17:51:41 -05:00
#include <csignal>
#include <cstring>
2023-08-14 17:51:41 -05:00
#include <future>
#include <memory>
#include <stdexcept>
2023-08-14 17:51:41 -05:00
#include <string>
#include <thread>
#include <utility>
#include <vector>
#include <pthread.h>
#include <syslog.h>
2023-09-24 17:55:09 -05:00
struct ThreadPoolTraits : public moodycamel::ConcurrentQueueDefaultTraits
{
2026-03-13 14:15:40 -05:00
static const int MAX_SEMA_SPINS = 1;
};
class ThreadPool
{
public:
using PToken = moodycamel::ProducerToken;
using CToken = moodycamel::ConsumerToken;
private:
using Func = ofats::any_invocable<void()>;
using Queue = BoundedQueue<Func,ThreadPoolTraits>;
public:
explicit
ThreadPool(const unsigned thread_count_ = std::thread::hardware_concurrency(),
const unsigned max_queue_depth_ = std::thread::hardware_concurrency() * 2,
const std::string &name_ = {});
~ThreadPool();
ThreadPool(const ThreadPool&) = delete;
ThreadPool& operator=(const ThreadPool&) = delete;
ThreadPool(ThreadPool&&) = delete;
ThreadPool& operator=(ThreadPool&&) = delete;
private:
static void *start_routine(void *arg_);
public:
int add_thread(const std::string &name = {});
int remove_thread();
int set_threads(const std::size_t count);
public:
template<typename FuncType>
void
enqueue_work(ThreadPool::PToken &ptok_,
FuncType &&func_);
template<typename FuncType>
void
enqueue_work(FuncType &&func_);
template<typename FuncType>
bool
try_enqueue_work(ThreadPool::PToken &ptok_,
FuncType &&func_);
template<typename FuncType>
bool
try_enqueue_work(FuncType &&func_);
template<typename FuncType>
bool
try_enqueue_work_for(ThreadPool::PToken &ptok_,
std::int64_t timeout_usecs_,
FuncType &&func_);
template<typename FuncType>
bool
try_enqueue_work_for(std::int64_t timeout_usecs_,
FuncType &&func_);
template<typename FuncType>
[[nodiscard]]
2025-09-29 00:17:51 -04:00
std::future<std::invoke_result_t<FuncType>>
enqueue_task(FuncType&& func_);
fix: validate and cap thread and queue counts - std::stoi threw std::out_of_range for a func.readdir concurrency or queue depth above INT_MAX, e.g. cor:99999999999. Given at mount, mergerfs aborted. Set at runtime through .mergerfs, the daemon shut down and the mount went away, or with process-thread-count>0 the request never got a reply. Parse with str::from, which reports the overflow, and reject the value like any other malformed one. - Reject a readdir queue depth whose product with the concurrency overflows an int, e.g. cor:2147483647:2147483647, instead of wrapping it. - Limit read-thread-count, process-thread-count and the cor/cosr pool size to 64 threads per pool, after resolving automatic and CPU-divided counts, and log a warning when clamping. Resolve negative FUSE counts without overflowing on INT_MIN, and drop the unreachable negative readdir concurrency branch. - process-thread-queue-depth is multiplied by the process thread count. Saturate the product at INT_MAX instead of overflowing. - Correct the docs: cor/cosr counts accept digits only and 0 means the core count, at most 8; a queue depth of 0 means 2 per thread; read-thread-count=0 with process-thread-count=0 is 2 read threads and max(2, cores - 2) process threads, at most 8; the queue limit is depth times process threads, each read thread holding one in-flight buffer of its own. Point at threads.md for how the settings combine. - Document the per-read-thread /dev/fuse clones and their fallback. - tests.cpp covers the overflow cases. Performance: default thread counts are unchanged (8 on hosts with 8 or more cores). Only explicit or CPU-divided counts above 64, which 2.42.0 honored, are reduced.
2026-11-21 14:33:27 -05:00
public:
static constexpr int max_thread_count();
public:
std::vector<pthread_t> threads() const;
ThreadPool::PToken ptoken();
private:
Queue _queue;
private:
std::string const _name;
std::vector<pthread_t> _threads;
2026-02-20 22:27:55 -06:00
mutable Mutex _threads_mutex;
};
fix: validate and cap thread and queue counts - std::stoi threw std::out_of_range for a func.readdir concurrency or queue depth above INT_MAX, e.g. cor:99999999999. Given at mount, mergerfs aborted. Set at runtime through .mergerfs, the daemon shut down and the mount went away, or with process-thread-count>0 the request never got a reply. Parse with str::from, which reports the overflow, and reject the value like any other malformed one. - Reject a readdir queue depth whose product with the concurrency overflows an int, e.g. cor:2147483647:2147483647, instead of wrapping it. - Limit read-thread-count, process-thread-count and the cor/cosr pool size to 64 threads per pool, after resolving automatic and CPU-divided counts, and log a warning when clamping. Resolve negative FUSE counts without overflowing on INT_MIN, and drop the unreachable negative readdir concurrency branch. - process-thread-queue-depth is multiplied by the process thread count. Saturate the product at INT_MAX instead of overflowing. - Correct the docs: cor/cosr counts accept digits only and 0 means the core count, at most 8; a queue depth of 0 means 2 per thread; read-thread-count=0 with process-thread-count=0 is 2 read threads and max(2, cores - 2) process threads, at most 8; the queue limit is depth times process threads, each read thread holding one in-flight buffer of its own. Point at threads.md for how the settings combine. - Document the per-read-thread /dev/fuse clones and their fallback. - tests.cpp covers the overflow cases. Performance: default thread counts are unchanged (8 on hosts with 8 or more cores). Only explicit or CPU-divided counts above 64, which 2.42.0 honored, are reduced.
2026-11-21 14:33:27 -05:00
// Cap configured pools to bound their memory and thread creation costs.
inline
constexpr int
ThreadPool::max_thread_count()
{
return 64;
}
inline
ThreadPool::ThreadPool(const unsigned thread_count_,
const unsigned max_queue_depth_,
const std::string &name_)
: _queue(max_queue_depth_),
_name(name_)
{
sigset_t oldset;
sigset_t newset;
sigfillset(&newset);
pthread_sigmask(SIG_BLOCK,&newset,&oldset);
_threads.reserve(thread_count_);
for(std::size_t i = 0; i < thread_count_; ++i)
{
int rv;
pthread_t t;
rv = pthread_create(&t,NULL,ThreadPool::start_routine,this);
if(rv != 0)
{
syslog(LOG_WARNING,
"threadpool (%s): error spawning thread - %d (%s)",
_name.c_str(),
rv,
strerror(rv));
continue;
}
2023-09-24 17:55:09 -05:00
if(not _name.empty())
{
std::string name = _name.substr(0, 15);
pthread_setname_np(t, name.c_str());
}
_threads.push_back(t);
}
pthread_sigmask(SIG_SETMASK,&oldset,NULL);
if(_threads.empty())
throw std::runtime_error("threadpool: failed to spawn any threads");
syslog(LOG_DEBUG,
"threadpool (%s): spawned %zu threads w/ max queue depth %u",
_name.c_str(),
_threads.size(),
max_queue_depth_);
}
inline
ThreadPool::~ThreadPool()
{
std::vector<pthread_t> threads;
{
mutex_lockguard(_threads_mutex);
threads = _threads;
}
syslog(LOG_DEBUG,
"threadpool (%s): destroying %zu threads",
_name.c_str(),
threads.size());
for(auto t : threads)
2026-03-13 14:15:40 -05:00
pthread_cancel(t);
for(auto t : threads)
pthread_join(t,NULL);
}
// Threads purposefully do not restore default signal handling.
inline
void*
ThreadPool::start_routine(void *arg_)
{
ThreadPool *btp = static_cast<ThreadPool*>(arg_);
ThreadPool::Func func;
ThreadPool::Queue &q = btp->_queue;
ThreadPool::CToken ctok(btp->_queue.make_ctoken());
2026-03-13 14:15:40 -05:00
while(true)
{
q.wait_dequeue(ctok,func);
pthread_setcancelstate(PTHREAD_CANCEL_DISABLE,NULL);
try
{
func();
}
2026-03-13 14:15:40 -05:00
#if defined(__GLIBCXX__)
catch(const __cxxabiv1::__forced_unwind&)
{
2026-03-13 14:15:40 -05:00
throw;
}
2026-03-13 14:15:40 -05:00
#endif
catch(const std::exception &e)
{
syslog(LOG_CRIT,
"threadpool (%s): uncaught exception caught by worker - %s",
btp->_name.c_str(),
e.what());
}
catch(...)
{
syslog(LOG_CRIT,
"threadpool (%s): uncaught non-standard exception caught by worker",
btp->_name.c_str());
2026-03-13 14:15:40 -05:00
}
// force destruction to release resources
func = {};
pthread_setcancelstate(PTHREAD_CANCEL_ENABLE,NULL);
}
return nullptr;
}
inline
int
ThreadPool::add_thread(const std::string &name_)
{
int rv;
pthread_t t;
sigset_t oldset;
sigset_t newset;
std::string name;
name = (name_.empty() ? _name : name_);
sigfillset(&newset);
pthread_sigmask(SIG_BLOCK,&newset,&oldset);
rv = pthread_create(&t,NULL,ThreadPool::start_routine,this);
pthread_sigmask(SIG_SETMASK,&oldset,NULL);
if(rv != 0)
{
syslog(LOG_WARNING,
"threadpool (%s): error spawning thread - %d (%s)",
_name.c_str(),
rv,
strerror(rv));
return -rv;
}
if(not name.empty())
{
std::string n = name.substr(0, 15);
pthread_setname_np(t,n.c_str());
}
{
mutex_lockguard(_threads_mutex);
_threads.push_back(t);
}
return 0;
}
inline
int
ThreadPool::remove_thread()
{
{
mutex_lockguard(_threads_mutex);
if(_threads.size() <= 1)
return -EINVAL;
}
2026-03-13 14:15:40 -05:00
auto promise = std::make_shared<std::promise<pthread_t>>();
auto func =
[this,promise]()
{
pthread_t t;
t = pthread_self();
{
mutex_lockguard(_threads_mutex);
auto it = std::find(_threads.begin(),
_threads.end(),
t);
if(it != _threads.end())
_threads.erase(it);
}
promise->set_value(t);
2026-03-13 14:15:40 -05:00
pthread_exit(NULL);
};
_queue.enqueue_unbounded(std::move(func));
2026-03-13 14:15:40 -05:00
pthread_join(promise->get_future().get(),NULL);
2026-03-13 14:15:40 -05:00
return 0;
}
inline
int
ThreadPool::set_threads(const std::size_t count_)
{
if(count_ == 0)
return -EINVAL;
2026-02-20 22:27:55 -06:00
std::size_t current;
{
mutex_lockguard(_threads_mutex);
current = _threads.size();
}
for(std::size_t i = current; i < count_; ++i)
add_thread();
for(std::size_t i = count_; i < current; ++i)
remove_thread();
return 0;
}
template<typename FuncType>
inline
void
ThreadPool::enqueue_work(ThreadPool::PToken &ptok_,
FuncType &&func_)
{
_queue.enqueue(ptok_,
std::forward<FuncType>(func_));
}
2023-09-24 17:55:09 -05:00
template<typename FuncType>
inline
void
ThreadPool::enqueue_work(FuncType &&func_)
{
_queue.enqueue(std::forward<FuncType>(func_));
}
2023-09-24 17:55:09 -05:00
template<typename FuncType>
inline
bool
ThreadPool::try_enqueue_work(ThreadPool::PToken &ptok_,
FuncType &&func_)
{
return _queue.try_enqueue(ptok_,
std::forward<FuncType>(func_));
}
template<typename FuncType>
inline
bool
ThreadPool::try_enqueue_work(FuncType &&func_)
{
return _queue.try_enqueue(std::forward<FuncType>(func_));
}
template<typename FuncType>
inline
bool
ThreadPool::try_enqueue_work_for(ThreadPool::PToken &ptok_,
std::int64_t timeout_usecs_,
FuncType &&func_)
{
return _queue.try_enqueue_for(ptok_,
timeout_usecs_,
std::forward<FuncType>(func_));
}
template<typename FuncType>
inline
bool
ThreadPool::try_enqueue_work_for(std::int64_t timeout_usecs_,
FuncType &&func_)
{
return _queue.try_enqueue_for(timeout_usecs_,
std::forward<FuncType>(func_));
}
template<typename FuncType>
[[nodiscard]]
inline
2025-09-29 00:17:51 -04:00
std::future<std::invoke_result_t<FuncType>>
ThreadPool::enqueue_task(FuncType&& func_)
{
2025-09-29 00:17:51 -04:00
using TaskReturnType = std::invoke_result_t<FuncType>;
using Promise = std::promise<TaskReturnType>;
2023-09-24 17:55:09 -05:00
Promise promise;
auto future = promise.get_future();
auto work =
[promise_ = std::move(promise),
func_ = std::forward<FuncType>(func_)]() mutable
{
try
{
if constexpr (std::is_void_v<TaskReturnType>)
{
func_();
promise_.set_value();
}
else
{
promise_.set_value(func_());
}
}
catch(...)
{
promise_.set_exception(std::current_exception());
}
};
_queue.enqueue(std::move(work));
2023-09-24 17:55:09 -05:00
return future;
}
inline
std::vector<pthread_t>
ThreadPool::threads() const
{
mutex_lockguard(_threads_mutex);
return _threads;
}
inline
ThreadPool::PToken
ThreadPool::ptoken()
{
return _queue.make_ptoken();
}