Skip to content
Browse files

Common/HttpRequest: Simplify cURL initialization

std::call_once is guaranteed to execute the given callable object
exactly once. This guarantee holds even if the function is called
concurrently from several threads.

Given that, we can replace the mutex and boolean flag with
std::call_once and a std::once_flag to perform the same behavior.
  • Loading branch information...
lioncash committed May 27, 2019
1 parent b15f595 commit ab2adfb0a76e4e46a08f632cfb0525c40e7731f4
Showing with 2 additions and 13 deletions.
  1. +2 −13 Source/Core/Common/HttpRequest.cpp
@@ -39,15 +39,11 @@ class HttpRequest::Impl final
std::string EscapeComponent(const std::string& string);

static std::mutex s_curl_was_inited_mutex;
static bool s_curl_was_inited;
static inline std::once_flag s_curl_was_initialized;
ProgressCallback m_callback;
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, ProgressCallback callback)
: m_impl(std::make_unique<Impl>(timeout_ms, std::move(callback)))
@@ -109,14 +105,7 @@ int HttpRequest::Impl::CurlProgressCallback(Impl* impl, double dlnow, double dlt
HttpRequest::Impl::Impl(std::chrono::milliseconds timeout_ms, ProgressCallback callback)
: m_callback(std::move(callback))
std::lock_guard<std::mutex> lk(s_curl_was_inited_mutex);
if (!s_curl_was_inited)
s_curl_was_inited = true;
std::call_once(s_curl_was_initialized, [] { curl_global_init(CURL_GLOBAL_DEFAULT); });

if (!m_curl)

0 comments on commit ab2adfb

Please sign in to comment.
You can’t perform that action at this time.