mirror of https://github.com/kiwix/libkiwix.git
Make Downloader thread safe.
This commit is contained in:
parent
d1fe1b89ae
commit
52ae5c3a5f
|
@ -25,6 +25,7 @@
|
||||||
#include <map>
|
#include <map>
|
||||||
#include <memory>
|
#include <memory>
|
||||||
#include <stdexcept>
|
#include <stdexcept>
|
||||||
|
#include <mutex>
|
||||||
|
|
||||||
namespace kiwix
|
namespace kiwix
|
||||||
{
|
{
|
||||||
|
@ -95,10 +96,11 @@ class Downloader
|
||||||
Download* startDownload(const std::string& uri, const std::vector<std::pair<std::string, std::string>>& options = {});
|
Download* startDownload(const std::string& uri, const std::vector<std::pair<std::string, std::string>>& options = {});
|
||||||
Download* getDownload(const std::string& did);
|
Download* getDownload(const std::string& did);
|
||||||
|
|
||||||
size_t getNbDownload() { return m_knownDownloads.size(); }
|
size_t getNbDownload();
|
||||||
std::vector<std::string> getDownloadIds();
|
std::vector<std::string> getDownloadIds();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
std::mutex m_lock;
|
||||||
std::map<std::string, std::unique_ptr<Download>> m_knownDownloads;
|
std::map<std::string, std::unique_ptr<Download>> m_knownDownloads;
|
||||||
std::shared_ptr<Aria2> mp_aria;
|
std::shared_ptr<Aria2> mp_aria;
|
||||||
};
|
};
|
||||||
|
|
|
@ -156,6 +156,7 @@ void Downloader::close()
|
||||||
}
|
}
|
||||||
|
|
||||||
std::vector<std::string> Downloader::getDownloadIds() {
|
std::vector<std::string> Downloader::getDownloadIds() {
|
||||||
|
std::unique_lock<std::mutex> lock(m_lock);
|
||||||
std::vector<std::string> ret;
|
std::vector<std::string> ret;
|
||||||
for(auto& p:m_knownDownloads) {
|
for(auto& p:m_knownDownloads) {
|
||||||
ret.push_back(p.first);
|
ret.push_back(p.first);
|
||||||
|
@ -165,6 +166,7 @@ std::vector<std::string> Downloader::getDownloadIds() {
|
||||||
|
|
||||||
Download* Downloader::startDownload(const std::string& uri, const std::vector<std::pair<std::string, std::string>>& options)
|
Download* Downloader::startDownload(const std::string& uri, const std::vector<std::pair<std::string, std::string>>& options)
|
||||||
{
|
{
|
||||||
|
std::unique_lock<std::mutex> lock(m_lock);
|
||||||
for (auto& p: m_knownDownloads) {
|
for (auto& p: m_knownDownloads) {
|
||||||
auto& d = p.second;
|
auto& d = p.second;
|
||||||
auto& uris = d->getUris();
|
auto& uris = d->getUris();
|
||||||
|
@ -179,6 +181,7 @@ Download* Downloader::startDownload(const std::string& uri, const std::vector<st
|
||||||
|
|
||||||
Download* Downloader::getDownload(const std::string& did)
|
Download* Downloader::getDownload(const std::string& did)
|
||||||
{
|
{
|
||||||
|
std::unique_lock<std::mutex> lock(m_lock);
|
||||||
try {
|
try {
|
||||||
return m_knownDownloads.at(did).get();
|
return m_knownDownloads.at(did).get();
|
||||||
} catch(std::exception& e) {
|
} catch(std::exception& e) {
|
||||||
|
@ -198,4 +201,9 @@ Download* Downloader::getDownload(const std::string& did)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
size_t Downloader::getNbDownload() {
|
||||||
|
std::unique_lock<std::mutex> lock(m_lock);
|
||||||
|
return m_knownDownloads.size();
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue