diff options
| author | Lioncash <mathew1800@gmail.com> | 2019-05-27 09:46:54 -0400 |
|---|---|---|
| committer | Lioncash <mathew1800@gmail.com> | 2019-05-27 09:46:57 -0400 |
| commit | ab2adfb0a76e4e46a08f632cfb0525c40e7731f4 (patch) | |
| tree | 24f4f9439168bec3f4f1fda1d201fa71493136f1 /Source/Core/Common/HttpRequest.cpp | |
| parent | b15f595130c22c595464707a372132fc8e7ea72b (diff) | |
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.
Diffstat (limited to 'Source/Core/Common/HttpRequest.cpp')
| -rw-r--r-- | Source/Core/Common/HttpRequest.cpp | 15 |
1 files changed, 2 insertions, 13 deletions
diff --git a/Source/Core/Common/HttpRequest.cpp b/Source/Core/Common/HttpRequest.cpp index 18ea402dc5..8929185a86 100644 --- a/Source/Core/Common/HttpRequest.cpp +++ b/Source/Core/Common/HttpRequest.cpp @@ -39,15 +39,11 @@ public: std::string EscapeComponent(const std::string& string); private: - 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) - { - curl_global_init(CURL_GLOBAL_DEFAULT); - s_curl_was_inited = true; - } - } + std::call_once(s_curl_was_initialized, [] { curl_global_init(CURL_GLOBAL_DEFAULT); }); m_curl.reset(curl_easy_init()); if (!m_curl) |
