From 52ae5c3a5ffc34b15f4eb8049811851c8c099e05 Mon Sep 17 00:00:00 2001 From: Matthieu Gautier Date: Tue, 7 Feb 2023 10:51:42 +0100 Subject: [PATCH] Make Downloader thread safe. --- include/downloader.h | 4 +++- src/downloader.cpp | 8 ++++++++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/include/downloader.h b/include/downloader.h index 4cd8337ad..726049ba0 100644 --- a/include/downloader.h +++ b/include/downloader.h @@ -25,6 +25,7 @@ #include #include #include +#include namespace kiwix { @@ -95,10 +96,11 @@ class Downloader Download* startDownload(const std::string& uri, const std::vector>& options = {}); Download* getDownload(const std::string& did); - size_t getNbDownload() { return m_knownDownloads.size(); } + size_t getNbDownload(); std::vector getDownloadIds(); private: + std::mutex m_lock; std::map> m_knownDownloads; std::shared_ptr mp_aria; }; diff --git a/src/downloader.cpp b/src/downloader.cpp index cd342b3e6..f6b13a21a 100644 --- a/src/downloader.cpp +++ b/src/downloader.cpp @@ -156,6 +156,7 @@ void Downloader::close() } std::vector Downloader::getDownloadIds() { + std::unique_lock lock(m_lock); std::vector ret; for(auto& p:m_knownDownloads) { ret.push_back(p.first); @@ -165,6 +166,7 @@ std::vector Downloader::getDownloadIds() { Download* Downloader::startDownload(const std::string& uri, const std::vector>& options) { + std::unique_lock lock(m_lock); for (auto& p: m_knownDownloads) { auto& d = p.second; auto& uris = d->getUris(); @@ -179,6 +181,7 @@ Download* Downloader::startDownload(const std::string& uri, const std::vector lock(m_lock); try { return m_knownDownloads.at(did).get(); } catch(std::exception& e) { @@ -198,4 +201,9 @@ Download* Downloader::getDownload(const std::string& did) } } +size_t Downloader::getNbDownload() { + std::unique_lock lock(m_lock); + return m_knownDownloads.size(); +} + }