Use std::mutex and std::unique_lock instead of pthread mutex/lock.

It simplify a bit the code and ensure that mutex is correctly unlock
even in case of exception.
This commit is contained in:
Matthieu Gautier 2020-08-26 12:12:14 +02:00
parent 39611cbd60
commit af9e03904c
2 changed files with 25 additions and 24 deletions

View File

@ -26,8 +26,7 @@ Aria2::Aria2():
mp_aria(nullptr), mp_aria(nullptr),
m_port(42042), m_port(42042),
m_secret("kiwixariarpc"), m_secret("kiwixariarpc"),
mp_curl(nullptr), mp_curl(nullptr)
m_lock(PTHREAD_MUTEX_INITIALIZER)
{ {
m_downloadDir = getDataDirectory(); m_downloadDir = getDataDirectory();
makeDirectory(m_downloadDir); makeDirectory(m_downloadDir);
@ -115,6 +114,7 @@ Aria2::Aria2():
Aria2::~Aria2() Aria2::~Aria2()
{ {
std::unique_lock<std::mutex> lock(m_lock);
curl_easy_cleanup(mp_curl); curl_easy_cleanup(mp_curl);
} }
@ -133,19 +133,23 @@ size_t write_callback_to_iss(char* ptr, size_t size, size_t nmemb, void* userdat
std::string Aria2::doRequest(const MethodCall& methodCall) std::string Aria2::doRequest(const MethodCall& methodCall)
{ {
pthread_mutex_lock(&m_lock);
auto requestContent = methodCall.toString(); auto requestContent = methodCall.toString();
std::stringstream stringstream; std::stringstream stringstream;
CURLcode res; CURLcode res;
long response_code;
{
std::unique_lock<std::mutex> lock(m_lock);
curl_easy_setopt(mp_curl, CURLOPT_POSTFIELDSIZE, requestContent.size()); curl_easy_setopt(mp_curl, CURLOPT_POSTFIELDSIZE, requestContent.size());
curl_easy_setopt(mp_curl, CURLOPT_POSTFIELDS, requestContent.c_str()); curl_easy_setopt(mp_curl, CURLOPT_POSTFIELDS, requestContent.c_str());
curl_easy_setopt(mp_curl, CURLOPT_WRITEFUNCTION, &write_callback_to_iss); curl_easy_setopt(mp_curl, CURLOPT_WRITEFUNCTION, &write_callback_to_iss);
curl_easy_setopt(mp_curl, CURLOPT_WRITEDATA, &stringstream); curl_easy_setopt(mp_curl, CURLOPT_WRITEDATA, &stringstream);
res = curl_easy_perform(mp_curl); res = curl_easy_perform(mp_curl);
if (res == CURLE_OK) { if (res != CURLE_OK) {
long response_code; throw std::runtime_error("Cannot perform request");
}
curl_easy_getinfo(mp_curl, CURLINFO_RESPONSE_CODE, &response_code); curl_easy_getinfo(mp_curl, CURLINFO_RESPONSE_CODE, &response_code);
pthread_mutex_unlock(&m_lock); }
if (response_code != 200) { if (response_code != 200) {
throw std::runtime_error("Invalid return code from aria"); throw std::runtime_error("Invalid return code from aria");
} }
@ -155,9 +159,6 @@ std::string Aria2::doRequest(const MethodCall& methodCall)
throw AriaError(response.getFault().getFaultString()); throw AriaError(response.getFault().getFaultString());
} }
return responseContent; return responseContent;
}
pthread_mutex_unlock(&m_lock);
throw std::runtime_error("Cannot perform request");
} }
std::string Aria2::addUri(const std::vector<std::string>& uris, const std::vector<std::pair<std::string, std::string>>& options) std::string Aria2::addUri(const std::vector<std::string>& uris, const std::vector<std::pair<std::string, std::string>>& options)

View File

@ -12,8 +12,8 @@
#include "xmlrpc.h" #include "xmlrpc.h"
#include <memory> #include <memory>
#include <mutex>
#include <curl/curl.h> #include <curl/curl.h>
#include <pthread.h>
namespace kiwix { namespace kiwix {
@ -25,7 +25,7 @@ class Aria2
std::string m_secret; std::string m_secret;
std::string m_downloadDir; std::string m_downloadDir;
CURL* mp_curl; CURL* mp_curl;
pthread_mutex_t m_lock; std::mutex m_lock;
std::string doRequest(const MethodCall& methodCall); std::string doRequest(const MethodCall& methodCall);