Skip to content

Commit d6f7084

Browse files
committed
HTTPDownloader: Use release-acquire ordering for request state
1 parent a171c25 commit d6f7084

File tree

2 files changed

+18
-16
lines changed

2 files changed

+18
-16
lines changed

src/util/http_downloader.cpp

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -98,20 +98,21 @@ void HTTPDownloader::LockedPollRequests(std::unique_lock<std::mutex>& lock)
9898
for (size_t index = 0; index < m_pending_http_requests.size();)
9999
{
100100
Request* req = m_pending_http_requests[index];
101-
if (req->state == Request::State::Pending)
101+
const Request::State req_state = req->state.load(std::memory_order_acquire);
102+
if (req_state == Request::State::Pending)
102103
{
103104
unstarted_requests++;
104105
index++;
105106
continue;
106107
}
107108

108-
if ((req->state == Request::State::Started || req->state == Request::State::Receiving) &&
109+
if ((req_state == Request::State::Started || req_state == Request::State::Receiving) &&
109110
current_time >= req->start_time && Timer::ConvertValueToSeconds(current_time - req->start_time) >= m_timeout)
110111
{
111112
// request timed out
112113
ERROR_LOG("Request for '{}' timed out", req->url);
113114

114-
req->state.store(Request::State::Cancelled);
115+
req->state.store(Request::State::Cancelled, std::memory_order_release);
115116
m_pending_http_requests.erase(m_pending_http_requests.begin() + index);
116117
lock.unlock();
117118

@@ -123,13 +124,13 @@ void HTTPDownloader::LockedPollRequests(std::unique_lock<std::mutex>& lock)
123124
lock.lock();
124125
continue;
125126
}
126-
else if ((req->state == Request::State::Started || req->state == Request::State::Receiving) && req->progress &&
127+
else if ((req_state == Request::State::Started || req_state == Request::State::Receiving) && req->progress &&
127128
req->progress->IsCancelled())
128129
{
129130
// request timed out
130131
ERROR_LOG("Request for '{}' cancelled", req->url);
131132

132-
req->state.store(Request::State::Cancelled);
133+
req->state.store(Request::State::Cancelled, std::memory_order_release);
133134
m_pending_http_requests.erase(m_pending_http_requests.begin() + index);
134135
lock.unlock();
135136

@@ -142,7 +143,7 @@ void HTTPDownloader::LockedPollRequests(std::unique_lock<std::mutex>& lock)
142143
continue;
143144
}
144145

145-
if (req->state != Request::State::Complete)
146+
if (req_state != Request::State::Complete)
146147
{
147148
if (req->progress)
148149
{
@@ -245,9 +246,10 @@ void HTTPDownloader::LockedAddRequest(Request* request)
245246
u32 HTTPDownloader::LockedGetActiveRequestCount()
246247
{
247248
u32 count = 0;
248-
for (Request* req : m_pending_http_requests)
249+
for (const Request* const req : m_pending_http_requests)
249250
{
250-
if (req->state == Request::State::Started || req->state == Request::State::Receiving)
251+
const Request::State req_state = req->state.load(std::memory_order_acquire);
252+
if (req_state == Request::State::Started || req_state == Request::State::Receiving)
251253
count++;
252254
}
253255
return count;

src/util/http_downloader_winhttp.cpp

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ void CALLBACK HTTPDownloaderWinHttp::HTTPStatusCallback(HINTERNET hRequest, DWOR
127127
ERROR_LOG("WinHttp async function {} returned error {}", res->dwResult, res->dwError);
128128
req->status_code = HTTP_STATUS_ERROR;
129129
req->error.SetStringFmt("WinHttp async function {} returned error {}", res->dwResult, res->dwError);
130-
req->state.store(Request::State::Complete);
130+
req->state.store(Request::State::Complete, std::memory_order_release);
131131
return;
132132
}
133133
case WINHTTP_CALLBACK_STATUS_SENDREQUEST_COMPLETE:
@@ -139,7 +139,7 @@ void CALLBACK HTTPDownloaderWinHttp::HTTPStatusCallback(HINTERNET hRequest, DWOR
139139
ERROR_LOG("WinHttpReceiveResponse() failed: {}", err);
140140
req->status_code = HTTP_STATUS_ERROR;
141141
req->error.SetWin32("WinHttpReceiveResponse() failed: ", err);
142-
req->state.store(Request::State::Complete);
142+
req->state.store(Request::State::Complete, std::memory_order_release);
143143
}
144144

145145
return;
@@ -156,7 +156,7 @@ void CALLBACK HTTPDownloaderWinHttp::HTTPStatusCallback(HINTERNET hRequest, DWOR
156156
ERROR_LOG("WinHttpQueryHeaders() for status code failed: {}", err);
157157
req->status_code = HTTP_STATUS_ERROR;
158158
req->error.SetWin32("WinHttpQueryHeaders() failed: ", err);
159-
req->state.store(Request::State::Complete);
159+
req->state.store(Request::State::Complete, std::memory_order_release);
160160
return;
161161
}
162162

@@ -188,7 +188,7 @@ void CALLBACK HTTPDownloaderWinHttp::HTTPStatusCallback(HINTERNET hRequest, DWOR
188188

189189
DEV_LOG("Status code {}, content-length is {}", req->status_code, req->content_length);
190190
req->data.reserve(req->content_length);
191-
req->state = Request::State::Receiving;
191+
req->state.store(Request::State::Receiving, std::memory_order_release);
192192

193193
// start reading
194194
if (!WinHttpQueryDataAvailable(hRequest, nullptr) && GetLastError() != ERROR_IO_PENDING)
@@ -197,7 +197,7 @@ void CALLBACK HTTPDownloaderWinHttp::HTTPStatusCallback(HINTERNET hRequest, DWOR
197197
ERROR_LOG("WinHttpQueryDataAvailable() failed: {}", err);
198198
req->status_code = HTTP_STATUS_ERROR;
199199
req->error.SetWin32("WinHttpQueryDataAvailable() failed: ", err);
200-
req->state.store(Request::State::Complete);
200+
req->state.store(Request::State::Complete, std::memory_order_release);
201201
}
202202

203203
return;
@@ -210,7 +210,7 @@ void CALLBACK HTTPDownloaderWinHttp::HTTPStatusCallback(HINTERNET hRequest, DWOR
210210
{
211211
// end of request
212212
DEV_LOG("End of request '{}', {} bytes received", req->url, req->data.size());
213-
req->state.store(Request::State::Complete);
213+
req->state.store(Request::State::Complete, std::memory_order_release);
214214
return;
215215
}
216216

@@ -225,7 +225,7 @@ void CALLBACK HTTPDownloaderWinHttp::HTTPStatusCallback(HINTERNET hRequest, DWOR
225225
ERROR_LOG("WinHttpReadData() failed: {}", err);
226226
req->status_code = HTTP_STATUS_ERROR;
227227
req->error.SetWin32("WinHttpReadData() failed: ", err);
228-
req->state.store(Request::State::Complete);
228+
req->state.store(Request::State::Complete, std::memory_order_release);
229229
}
230230

231231
return;
@@ -245,7 +245,7 @@ void CALLBACK HTTPDownloaderWinHttp::HTTPStatusCallback(HINTERNET hRequest, DWOR
245245
ERROR_LOG("WinHttpQueryDataAvailable() failed: {}", err);
246246
req->status_code = HTTP_STATUS_ERROR;
247247
req->error.SetWin32("WinHttpQueryDataAvailable() failed: ", err);
248-
req->state.store(Request::State::Complete);
248+
req->state.store(Request::State::Complete, std::memory_order_release);
249249
}
250250

251251
return;

0 commit comments

Comments
 (0)