1
0
Fork 0
mirror of https://github.com/transmission/transmission synced 2024-12-30 19:46:56 +00:00

refactor: replace tr_list with std::set in web.cc (#1845)

This commit is contained in:
Charles Kerr 2021-09-27 11:27:02 -05:00 committed by GitHub
parent 3504933fba
commit c42a6c785d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -8,6 +8,7 @@
#include <algorithm> #include <algorithm>
#include <cstring> /* strlen(), strstr() */ #include <cstring> /* strlen(), strstr() */
#include <set>
#ifdef _WIN32 #ifdef _WIN32
#include <windows.h> #include <windows.h>
@ -21,7 +22,6 @@
#include "transmission.h" #include "transmission.h"
#include "crypto-utils.h" #include "crypto-utils.h"
#include "file.h" #include "file.h"
#include "list.h"
#include "log.h" #include "log.h"
#include "net.h" /* tr_address */ #include "net.h" /* tr_address */
#include "torrent.h" #include "torrent.h"
@ -89,8 +89,6 @@ static void task_free(struct tr_web_task* task)
**** ****
***/ ***/
static tr_list* paused_easy_handles = nullptr;
struct tr_web struct tr_web
{ {
bool curl_verbose; bool curl_verbose;
@ -100,6 +98,7 @@ struct tr_web
struct tr_web_task* tasks; struct tr_web_task* tasks;
tr_lock* taskLock; tr_lock* taskLock;
char* cookie_filename; char* cookie_filename;
std::set<CURL*> paused_easy_handles;
}; };
/*** /***
@ -118,7 +117,7 @@ static size_t writeFunc(void* ptr, size_t size, size_t nmemb, void* vtask)
if (tor != nullptr && tr_bandwidthClamp(&tor->bandwidth, TR_DOWN, nmemb) == 0) if (tor != nullptr && tr_bandwidthClamp(&tor->bandwidth, TR_DOWN, nmemb) == 0)
{ {
tr_list_append(&paused_easy_handles, task->curl_easy); task->session->web->paused_easy_handles.insert(task->curl_easy);
return CURL_WRITEFUNC_PAUSE; return CURL_WRITEFUNC_PAUSE;
} }
} }
@ -418,7 +417,6 @@ static void tr_webThreadFunc(void* vsession)
{ {
char* str; char* str;
CURLM* multi; CURLM* multi;
struct tr_web* web;
int taskCount = 0; int taskCount = 0;
uint32_t repeats = 0; uint32_t repeats = 0;
auto* session = static_cast<tr_session*>(vsession); auto* session = static_cast<tr_session*>(vsession);
@ -430,7 +428,7 @@ static void tr_webThreadFunc(void* vsession)
curl_global_init(0); curl_global_init(0);
} }
web = tr_new0(struct tr_web, 1); auto* web = new tr_web{};
web->close_mode = ~0; web->close_mode = ~0;
web->taskLock = tr_lockNew(); web->taskLock = tr_lockNew();
web->tasks = nullptr; web->tasks = nullptr;
@ -495,22 +493,12 @@ static void tr_webThreadFunc(void* vsession)
tr_lockUnlock(web->taskLock); tr_lockUnlock(web->taskLock);
/* unpause any paused curl handles */ /* resume any paused curl handles.
if (paused_easy_handles != nullptr) swap paused_easy_handles to prevent oscillation
{ between writeFunc this while loop */
CURL* handle; auto paused = decltype(web->paused_easy_handles){};
tr_list* tmp; std::swap(paused, web->paused_easy_handles);
std::for_each(std::begin(paused), std::end(paused), [](auto* curl) { curl_easy_pause(curl, CURLPAUSE_CONT); });
/* swap paused_easy_handles to prevent oscillation
between writeFunc this while loop */
tmp = paused_easy_handles;
paused_easy_handles = nullptr;
while ((handle = tr_list_pop_front(&tmp)) != nullptr)
{
curl_easy_pause(handle, CURLPAUSE_CONT);
}
}
/* maybe wait a little while before calling curl_multi_perform() */ /* maybe wait a little while before calling curl_multi_perform() */
msec = 0; msec = 0;
@ -576,7 +564,7 @@ static void tr_webThreadFunc(void* vsession)
task->did_connect = task->code > 0 || req_bytes_sent > 0; task->did_connect = task->code > 0 || req_bytes_sent > 0;
task->did_timeout = task->code == 0 && total_time >= task->timeout_secs; task->did_timeout = task->code == 0 && total_time >= task->timeout_secs;
curl_multi_remove_handle(multi, e); curl_multi_remove_handle(multi, e);
tr_list_remove_data(&paused_easy_handles, e); web->paused_easy_handles.erase(e);
curl_easy_cleanup(e); curl_easy_cleanup(e);
tr_runInEventThread(task->session, task_finish_func, task); tr_runInEventThread(task->session, task_finish_func, task);
--taskCount; --taskCount;
@ -595,12 +583,11 @@ static void tr_webThreadFunc(void* vsession)
} }
/* cleanup */ /* cleanup */
tr_list_free(&paused_easy_handles, nullptr);
curl_multi_cleanup(multi); curl_multi_cleanup(multi);
tr_lockFree(web->taskLock); tr_lockFree(web->taskLock);
tr_free(web->curl_ca_bundle); tr_free(web->curl_ca_bundle);
tr_free(web->cookie_filename); tr_free(web->cookie_filename);
tr_free(web); delete web;
session->web = nullptr; session->web = nullptr;
} }