mirror of
https://github.com/dolphin-emu/dolphin.git
synced 2025-02-13 15:59:23 +01:00
HttpRequest: make curl initialization thread-safe
This commit is contained in:
parent
5498f9b7ef
commit
bc9deb7be3
@ -7,6 +7,7 @@
|
||||
#include <chrono>
|
||||
#include <cstddef>
|
||||
#include <curl/curl.h>
|
||||
#include <mutex>
|
||||
|
||||
#include "Common/Logging/Log.h"
|
||||
#include "Common/ScopeGuard.h"
|
||||
@ -30,9 +31,14 @@ public:
|
||||
size_t size);
|
||||
|
||||
private:
|
||||
std::unique_ptr<CURL, decltype(&curl_easy_cleanup)> m_curl{curl_easy_init(), curl_easy_cleanup};
|
||||
static std::mutex s_curl_was_inited_mutex;
|
||||
static bool s_curl_was_inited;
|
||||
std::unique_ptr<CURL, decltype(&curl_easy_cleanup)> m_curl{nullptr, curl_easy_cleanup};
|
||||
};
|
||||
|
||||
std::mutex HttpRequest::Impl::s_curl_was_inited_mutex;
|
||||
bool HttpRequest::Impl::s_curl_was_inited = false;
|
||||
|
||||
HttpRequest::HttpRequest(std::chrono::milliseconds timeout_ms)
|
||||
: m_impl(std::make_unique<Impl>(timeout_ms))
|
||||
{
|
||||
@ -65,6 +71,16 @@ HttpRequest::Response HttpRequest::Post(const std::string& url, const std::strin
|
||||
|
||||
HttpRequest::Impl::Impl(std::chrono::milliseconds timeout_ms)
|
||||
{
|
||||
{
|
||||
std::lock_guard<std::mutex> lk(s_curl_was_inited_mutex);
|
||||
if (!s_curl_was_inited)
|
||||
{
|
||||
curl_global_init(CURL_GLOBAL_DEFAULT);
|
||||
s_curl_was_inited = true;
|
||||
}
|
||||
}
|
||||
|
||||
m_curl.reset(curl_easy_init());
|
||||
if (!m_curl)
|
||||
return;
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user