Optimise : use condition variable instead of busyloop

This commit is contained in:
timoreo 2023-12-12 21:29:25 +01:00
parent 6d83e1d4d3
commit 93dd64ceca
Signed by: timoreo
GPG key ID: 121A72C3512BA288

View file

@ -19,6 +19,7 @@
#include "util.h"
#include <sys/socket.h>
#include <condition_variable>
#define SOC_ALIGN 0x1000
#define SOC_BUFFERSIZE 0x100000
@ -803,6 +804,8 @@ json_t* Client::doRequest(const char* method, const std::string& path, json_t* b
CURLM* curl_multi_handle;
std::map<CURLM*, CURLcode> curl_handles_done;
std::condition_variable on_done;
std::mutex handles_done_m;
Thread curl_multi_loop_thread;
[[noreturn]] void curl_multi_loop(void* p) {
@ -818,6 +821,7 @@ Thread curl_multi_loop_thread;
while ((msg = curl_multi_info_read(curl_multi_handle, &msgsLeft))) {
if (msg->msg == CURLMSG_DONE) {
curl_handles_done[msg->easy_handle] = msg->data.result;
on_done.notify_all(); // wake up all threads, since we don't know what thread needs this req
}
}
if (!openHandles) {
@ -874,13 +878,15 @@ json_t* Client::doRequestCurl(const char* method, const std::string& url, json_t
curl_easy_setopt(curl, CURLOPT_TIMEOUT, timeout);
curl_multi_add_handle(curl_multi_handle, curl);
CURLcode res;
{
std::unique_lock lk(handles_done_m);
while (curl_handles_done.count(curl) == 0) {
svcSleepThread((u64)1000000ULL * 1);
on_done.wait(lk, [&curl]{return curl_handles_done.count(curl) != 0;});
res = curl_handles_done[curl];
curl_handles_done.erase(curl);
}
CURLcode res = curl_handles_done[curl];
curl_handles_done.erase(curl);
curl_multi_remove_handle(curl_multi_handle, curl);
// curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);