Phase C: replace YtDlpManager's hand-rolled pool with Void ThreadPool
YtDlpManager carried a bespoke worker pool — a jthread vector, a packaged_task queue, and a mutex/condition_variable/stop-flag loop. Swap all of it for a single Void::Concurrency::ThreadPool (held by unique_ptr so setThreadPoolSize can recreate it): - startThreadPool() constructs the pool at the (auto-adjusted) size; stopThreadPool() resets it (the destructor drains and joins). - executeAsync() submits to the pool under mutex_ (mutually exclusive with start/stop), with a synchronous fallback if called before start. - workerThread() and the taskQueue_/taskCv_/taskMutex_/stopThreads_ members are gone. Behaviorally equivalent (bounded yt-dlp concurrency, same std::future results) with far less concurrency code to own. Build green. The init-time "update yt-dlp in background" detached thread is left as-is: it's a one-shot fired before the pool exists. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
1 parent
5775c522eb
commit
da708fa9bb
2 files changed
+17
-61
No files matched your search
@@ -3,13 +3,13 @@
|
|||||||
#include "NetworkClient.hpp"
|
#include "NetworkClient.hpp"
|
||||||
#include <TrackTypes.hpp>
|
#include <TrackTypes.hpp>
|
||||||
#include <Types.hpp>
|
#include <Types.hpp>
|
||||||
|
#include <Void/Async/ThreadPool.hpp>
|
||||||
#include <YouTubeTypes.hpp>
|
#include <YouTubeTypes.hpp>
|
||||||
#include <atomic>
|
#include <atomic>
|
||||||
#include <condition_variable>
|
|
||||||
#include <filesystem>
|
#include <filesystem>
|
||||||
#include <future>
|
#include <future>
|
||||||
|
#include <memory>
|
||||||
#include <mutex>
|
#include <mutex>
|
||||||
#include <queue>
|
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <thread>
|
#include <thread>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
@@ -105,7 +105,6 @@ namespace Echoion::Core
|
|||||||
// Thread pool for async operations
|
// Thread pool for async operations
|
||||||
void startThreadPool();
|
void startThreadPool();
|
||||||
void stopThreadPool();
|
void stopThreadPool();
|
||||||
void workerThread();
|
|
||||||
|
|
||||||
std::shared_ptr<NetworkClient> networkClient_;
|
std::shared_ptr<NetworkClient> networkClient_;
|
||||||
std::filesystem::path executablePath_;
|
std::filesystem::path executablePath_;
|
||||||
@@ -120,13 +119,10 @@ namespace Echoion::Core
|
|||||||
std::optional<Api::ProxyConfig> proxyConfig_;
|
std::optional<Api::ProxyConfig> proxyConfig_;
|
||||||
mutable std::mutex proxyMutex_;
|
mutable std::mutex proxyMutex_;
|
||||||
|
|
||||||
// Thread pool for async operations
|
// Thread pool for async operations (Void worker pool; recreated
|
||||||
|
// on resize). Guarded by mutex_ for start/stop/submit.
|
||||||
size_t threadPoolSize_ = 4;
|
size_t threadPoolSize_ = 4;
|
||||||
std::vector<std::jthread> threadPool_;
|
std::unique_ptr<Void::Concurrency::ThreadPool> pool_;
|
||||||
std::queue<std::packaged_task<YtDlpResult()>> taskQueue_;
|
|
||||||
std::mutex taskMutex_;
|
|
||||||
std::condition_variable taskCv_;
|
|
||||||
std::atomic<bool> stopThreads_{false};
|
|
||||||
|
|
||||||
// yt-dlp download URLs
|
// yt-dlp download URLs
|
||||||
static constexpr std::string_view YTDLP_VERSION_URL =
|
static constexpr std::string_view YTDLP_VERSION_URL =
|
||||||
|
|||||||
+12
-52
@@ -171,7 +171,7 @@ namespace Echoion::Core
|
|||||||
|
|
||||||
void YtDlpManager::startThreadPool()
|
void YtDlpManager::startThreadPool()
|
||||||
{
|
{
|
||||||
stopThreadPool(); // Clear any existing threads first
|
stopThreadPool(); // Drop any existing pool first
|
||||||
|
|
||||||
// Auto-adjust thread pool size based on hardware
|
// Auto-adjust thread pool size based on hardware
|
||||||
unsigned int hardwareThreads = std::thread::hardware_concurrency();
|
unsigned int hardwareThreads = std::thread::hardware_concurrency();
|
||||||
@@ -185,50 +185,13 @@ namespace Echoion::Core
|
|||||||
ECHOION_LOG_INFO(std::format(
|
ECHOION_LOG_INFO(std::format(
|
||||||
"Starting yt-dlp thread pool with {} threads", threadPoolSize_));
|
"Starting yt-dlp thread pool with {} threads", threadPoolSize_));
|
||||||
|
|
||||||
for (size_t i = 0; i < threadPoolSize_; ++i) {
|
pool_ = std::make_unique<Void::Concurrency::ThreadPool>(threadPoolSize_);
|
||||||
threadPool_.emplace_back([this] { workerThread(); });
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void YtDlpManager::stopThreadPool()
|
void YtDlpManager::stopThreadPool()
|
||||||
{
|
{
|
||||||
stopThreads_ = true;
|
// ~ThreadPool drains in-flight work and joins its workers.
|
||||||
taskCv_.notify_all();
|
pool_.reset();
|
||||||
|
|
||||||
for (auto& thread : threadPool_) {
|
|
||||||
if (thread.joinable()) {
|
|
||||||
thread.join();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
threadPool_.clear();
|
|
||||||
stopThreads_ = false;
|
|
||||||
}
|
|
||||||
|
|
||||||
void YtDlpManager::workerThread()
|
|
||||||
{
|
|
||||||
while (!stopThreads_.load()) {
|
|
||||||
std::packaged_task<YtDlpResult()> task;
|
|
||||||
|
|
||||||
{
|
|
||||||
std::unique_lock<std::mutex> lock(taskMutex_);
|
|
||||||
taskCv_.wait(lock, [this] {
|
|
||||||
return !taskQueue_.empty() || stopThreads_.load();
|
|
||||||
});
|
|
||||||
|
|
||||||
if (stopThreads_.load()) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!taskQueue_.empty()) {
|
|
||||||
task = std::move(taskQueue_.front());
|
|
||||||
taskQueue_.pop();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (task.valid()) {
|
|
||||||
task();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void YtDlpManager::setThreadPoolSize(size_t size)
|
void YtDlpManager::setThreadPoolSize(size_t size)
|
||||||
@@ -631,18 +594,15 @@ namespace Echoion::Core
|
|||||||
std::future<YtDlpResult>
|
std::future<YtDlpResult>
|
||||||
YtDlpManager::executeAsync(const std::vector<std::string>& args)
|
YtDlpManager::executeAsync(const std::vector<std::string>& args)
|
||||||
{
|
{
|
||||||
std::packaged_task<YtDlpResult()> task(
|
std::lock_guard<std::mutex> lock(mutex_);
|
||||||
[this, args]() { return executeInternal(args); });
|
if (!pool_) {
|
||||||
|
// Not started (shouldn't happen post-init): run synchronously so
|
||||||
auto future = task.get_future();
|
// the caller still gets a valid, ready future.
|
||||||
|
std::promise<YtDlpResult> prom;
|
||||||
{
|
prom.set_value(executeInternal(args));
|
||||||
std::lock_guard<std::mutex> lock(taskMutex_);
|
return prom.get_future();
|
||||||
taskQueue_.push(std::move(task));
|
|
||||||
}
|
}
|
||||||
|
return pool_->submit([this, args]() { return executeInternal(args); });
|
||||||
taskCv_.notify_one();
|
|
||||||
return future;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
std::vector<YtDlpResult> YtDlpManager::executeBatch(
|
std::vector<YtDlpResult> YtDlpManager::executeBatch(
|
||||||
|
|||||||
Reference in new issue
Block a user