commit 4146c9489f24cc8a372aee50b589916916d47b66
parent b32d2bbc261467edc39067f48650c800a2736e05
Author: cfillion <cfillion@users.noreply.github.com>
Date: Sun, 12 Aug 2018 22:46:09 -0400
create the curl context only in threads performing download tasks
...and make WorkerThread unaware of it
Diffstat:
3 files changed, 20 insertions(+), 24 deletions(-)
diff --git a/src/download.cpp b/src/download.cpp
@@ -102,7 +102,7 @@ int Download::UpdateProgress(void *ptr, const double, const double,
}
Download::Download(const string &url, const NetworkOpts &opts, const int flags)
- : m_url(url), m_opts(opts), m_flags(flags), m_ctx(nullptr)
+ : m_url(url), m_opts(opts), m_flags(flags)
{
}
@@ -117,28 +117,30 @@ bool Download::run()
if(!stream)
return false;
- curl_easy_setopt(m_ctx->m_curl, CURLOPT_URL, m_url.c_str());
- curl_easy_setopt(m_ctx->m_curl, CURLOPT_PROXY, m_opts.proxy.c_str());
- curl_easy_setopt(m_ctx->m_curl, CURLOPT_SSL_VERIFYPEER, m_opts.verifyPeer);
+ thread_local DownloadContext ctx;
+
+ curl_easy_setopt(ctx, CURLOPT_URL, m_url.c_str());
+ curl_easy_setopt(ctx, CURLOPT_PROXY, m_opts.proxy.c_str());
+ curl_easy_setopt(ctx, CURLOPT_SSL_VERIFYPEER, m_opts.verifyPeer);
#ifdef __APPLE__
- curl_easy_setopt(m_ctx->m_curl, CURLOPT_CAINFO, nullptr);
+ curl_easy_setopt(ctx, CURLOPT_CAINFO, nullptr);
#endif
- curl_easy_setopt(m_ctx->m_curl, CURLOPT_PROGRESSFUNCTION, UpdateProgress);
- curl_easy_setopt(m_ctx->m_curl, CURLOPT_PROGRESSDATA, this);
+ curl_easy_setopt(ctx, CURLOPT_PROGRESSFUNCTION, UpdateProgress);
+ curl_easy_setopt(ctx, CURLOPT_PROGRESSDATA, this);
- curl_easy_setopt(m_ctx->m_curl, CURLOPT_WRITEFUNCTION, WriteData);
- curl_easy_setopt(m_ctx->m_curl, CURLOPT_WRITEDATA, stream);
+ curl_easy_setopt(ctx, CURLOPT_WRITEFUNCTION, WriteData);
+ curl_easy_setopt(ctx, CURLOPT_WRITEDATA, stream);
curl_slist *headers = nullptr;
if(has(Download::NoCacheFlag))
headers = curl_slist_append(headers, "Cache-Control: no-cache");
- curl_easy_setopt(m_ctx->m_curl, CURLOPT_HTTPHEADER, headers);
+ curl_easy_setopt(ctx, CURLOPT_HTTPHEADER, headers);
char errbuf[CURL_ERROR_SIZE] = "No error message";
- curl_easy_setopt(m_ctx->m_curl, CURLOPT_ERRORBUFFER, errbuf);
+ curl_easy_setopt(ctx, CURLOPT_ERRORBUFFER, errbuf);
- const CURLcode res = curl_easy_perform(m_ctx->m_curl);
+ const CURLcode res = curl_easy_perform(ctx);
curl_slist_free_all(headers);
closeStream();
diff --git a/src/download.hpp b/src/download.hpp
@@ -26,13 +26,17 @@
#include <fstream>
#include <sstream>
-struct DownloadContext {
+class DownloadContext {
+public:
static void GlobalInit();
static void GlobalCleanup();
DownloadContext();
~DownloadContext();
+ operator CURL*() { return m_curl; }
+
+private:
CURL *m_curl;
};
@@ -46,7 +50,6 @@ public:
void setName(const std::string &);
const std::string &url() const { return m_url; }
- void setContext(DownloadContext *ctx) { m_ctx = ctx; }
bool concurrent() const override { return true; }
bool run() override;
@@ -63,7 +66,6 @@ private:
std::string m_url;
NetworkOpts m_opts;
int m_flags;
- DownloadContext *m_ctx;
};
class MemoryDownload : public Download {
diff --git a/src/thread.cpp b/src/thread.cpp
@@ -17,8 +17,6 @@
#include "thread.hpp"
-#include "download.hpp"
-
#include <reaper_plugin_functions.h>
using namespace std;
@@ -98,15 +96,9 @@ WorkerThread::~WorkerThread()
void WorkerThread::run()
{
- DownloadContext context;
-
do {
- while(ThreadTask *task = nextTask()) {
- if(auto dl = dynamic_cast<Download *>(task))
- dl->setContext(&context);
-
+ while(ThreadTask *task = nextTask())
task->exec();
- }
unique_lock<mutex> lock(m_mutex);
m_wake.wait(lock);