From d88762d2f1207d048f363746a88a3605df82308e Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Sun, 10 Jan 2021 20:42:38 +0100 Subject: [PATCH] Collect mutex and data protected by it in a single struct Also use critical section instead of a mutex, as this is more efficient under MSW. Main purpose of this commit is to make it clear that this mutex/critical section is only used together with the data from the same struct. No real changes. --- include/wx/private/webrequest_curl.h | 9 +++++++-- src/common/webrequest_curl.cpp | 12 ++++++------ 2 files changed, 13 insertions(+), 8 deletions(-) diff --git a/include/wx/private/webrequest_curl.h b/include/wx/private/webrequest_curl.h index f31adb806f..b16481bb1d 100644 --- a/include/wx/private/webrequest_curl.h +++ b/include/wx/private/webrequest_curl.h @@ -145,8 +145,13 @@ private: wxMutex m_mutex; wxCondition m_condition; bool m_shuttingDown; - wxMutex m_cancelledMutex; - wxVector< wxObjectDataPtr > m_cancelledRequests; + + // MT-safe vector of requests for which Cancel() was called. + struct CancelledData + { + wxCriticalSection cs; + wxVector< wxObjectDataPtr > requests; + } m_cancelled; static int ms_activeSessions; diff --git a/src/common/webrequest_curl.cpp b/src/common/webrequest_curl.cpp index 73f4e4c1c8..4ee6980d0f 100644 --- a/src/common/webrequest_curl.cpp +++ b/src/common/webrequest_curl.cpp @@ -448,11 +448,11 @@ wxThread::ExitCode wxWebSessionCURL::Entry() { // Handle cancelled requests { - wxMutexLocker lock(m_cancelledMutex); - while ( !m_cancelledRequests.empty() ) + wxCriticalSectionLocker lock(m_cancelled.cs); + while ( !m_cancelled.requests.empty() ) { - wxObjectDataPtr request(m_cancelledRequests.back()); - m_cancelledRequests.pop_back(); + wxObjectDataPtr request(m_cancelled.requests.back()); + m_cancelled.requests.pop_back(); curl_multi_remove_handle(m_handle, request->GetHandle()); request->SetState(wxWebRequest::State_Cancelled); } @@ -527,9 +527,9 @@ void wxWebSessionCURL::CancelRequest(wxWebRequestCURL* request) { // Add the request to a list of threads that will be removed from the curl // multi handle in the worker thread - wxMutexLocker lock(m_cancelledMutex); + wxCriticalSectionLocker lock(m_cancelled.cs); request->IncRef(); - m_cancelledRequests.push_back(wxObjectDataPtr(request)); + m_cancelled.requests.push_back(wxObjectDataPtr(request)); } wxVersionInfo wxWebSessionCURL::GetLibraryVersionInfo()