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 <TrackTypes.hpp>
|
||||
#include <Types.hpp>
|
||||
#include <Void/Async/ThreadPool.hpp>
|
||||
#include <YouTubeTypes.hpp>
|
||||
#include <atomic>
|
||||
#include <condition_variable>
|
||||
#include <filesystem>
|
||||
#include <future>
|
||||
#include <memory>
|
||||
#include <mutex>
|
||||
#include <queue>
|
||||
#include <string>
|
||||
#include <thread>
|
||||
#include <vector>
|
||||
@@ -105,7 +105,6 @@ namespace Echoion::Core
|
||||
// Thread pool for async operations
|
||||
void startThreadPool();
|
||||
void stopThreadPool();
|
||||
void workerThread();
|
||||
|
||||
std::shared_ptr<NetworkClient> networkClient_;
|
||||
std::filesystem::path executablePath_;
|
||||
@@ -120,13 +119,10 @@ namespace Echoion::Core
|
||||
std::optional<Api::ProxyConfig> 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<std::jthread> threadPool_;
|
||||
std::queue<std::packaged_task<YtDlpResult()>> taskQueue_;
|
||||
std::mutex taskMutex_;
|
||||
std::condition_variable taskCv_;
|
||||
std::atomic<bool> stopThreads_{false};
|
||||
std::unique_ptr<Void::Concurrency::ThreadPool> pool_;
|
||||
|
||||
// yt-dlp download URLs
|
||||
static constexpr std::string_view YTDLP_VERSION_URL =
|
||||
|
||||
+12
-52
@@ -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<Void::Concurrency::ThreadPool>(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<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();
|
||||
}
|
||||
}
|
||||
// ~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<YtDlpResult>
|
||||
YtDlpManager::executeAsync(const std::vector<std::string>& args)
|
||||
{
|
||||
std::packaged_task<YtDlpResult()> task(
|
||||
[this, args]() { return executeInternal(args); });
|
||||
|
||||
auto future = task.get_future();
|
||||
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(taskMutex_);
|
||||
taskQueue_.push(std::move(task));
|
||||
std::lock_guard<std::mutex> lock(mutex_);
|
||||
if (!pool_) {
|
||||
// Not started (shouldn't happen post-init): run synchronously so
|
||||
// the caller still gets a valid, ready future.
|
||||
std::promise<YtDlpResult> 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<YtDlpResult> YtDlpManager::executeBatch(
|
||||
|
||||
Reference in new issue
Block a user