diff --git a/include/echoion/core/YtDlpManager.hpp b/include/echoion/core/YtDlpManager.hpp index 516c44f..2d37309 100644 --- a/include/echoion/core/YtDlpManager.hpp +++ b/include/echoion/core/YtDlpManager.hpp @@ -3,13 +3,13 @@ #include "NetworkClient.hpp" #include #include +#include #include #include -#include #include #include +#include #include -#include #include #include #include @@ -105,7 +105,6 @@ namespace Echoion::Core // Thread pool for async operations void startThreadPool(); void stopThreadPool(); - void workerThread(); std::shared_ptr networkClient_; std::filesystem::path executablePath_; @@ -120,13 +119,10 @@ namespace Echoion::Core std::optional proxyConfig_; 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; - std::vector threadPool_; - std::queue> taskQueue_; - std::mutex taskMutex_; - std::condition_variable taskCv_; - std::atomic stopThreads_{false}; + std::unique_ptr pool_; // yt-dlp download URLs static constexpr std::string_view YTDLP_VERSION_URL = diff --git a/src/YtDlpManager.cpp b/src/YtDlpManager.cpp index 5733ef4..208d118 100644 --- a/src/YtDlpManager.cpp +++ b/src/YtDlpManager.cpp @@ -171,7 +171,7 @@ namespace Echoion::Core void YtDlpManager::startThreadPool() { - stopThreadPool(); // Clear any existing threads first + stopThreadPool(); // Drop any existing pool first // Auto-adjust thread pool size based on hardware unsigned int hardwareThreads = std::thread::hardware_concurrency(); @@ -185,50 +185,13 @@ namespace Echoion::Core ECHOION_LOG_INFO(std::format( "Starting yt-dlp thread pool with {} threads", threadPoolSize_)); - for (size_t i = 0; i < threadPoolSize_; ++i) { - threadPool_.emplace_back([this] { workerThread(); }); - } + pool_ = std::make_unique(threadPoolSize_); } void YtDlpManager::stopThreadPool() { - stopThreads_ = true; - taskCv_.notify_all(); - - for (auto& thread : threadPool_) { - if (thread.joinable()) { - thread.join(); - } - } - threadPool_.clear(); - stopThreads_ = false; - } - - void YtDlpManager::workerThread() - { - while (!stopThreads_.load()) { - std::packaged_task task; - - { - std::unique_lock 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(); - } - } + // ~ThreadPool drains in-flight work and joins its workers. + pool_.reset(); } void YtDlpManager::setThreadPoolSize(size_t size) @@ -631,18 +594,15 @@ namespace Echoion::Core std::future YtDlpManager::executeAsync(const std::vector& args) { - std::packaged_task task( - [this, args]() { return executeInternal(args); }); - - auto future = task.get_future(); - - { - std::lock_guard lock(taskMutex_); - taskQueue_.push(std::move(task)); + std::lock_guard lock(mutex_); + if (!pool_) { + // Not started (shouldn't happen post-init): run synchronously so + // the caller still gets a valid, ready future. + std::promise prom; + prom.set_value(executeInternal(args)); + return prom.get_future(); } - - taskCv_.notify_one(); - return future; + return pool_->submit([this, args]() { return executeInternal(args); }); } std::vector YtDlpManager::executeBatch(