summaryrefslogtreecommitdiff
path: root/linux
diff options
context:
space:
mode:
authorKonstantin Käfer <mail@kkaefer.com>2014-03-03 14:57:12 +0100
committerKonstantin Käfer <mail@kkaefer.com>2014-03-03 14:57:12 +0100
commit880dc2d841af85e6651444f64ee09dcc72e85825 (patch)
treea9f724d0913fff394d7d751543b3996e4073e7c4 /linux
parente13b519745a5f1f3cfad66b9ef28c9292d550531 (diff)
downloadqtlocation-mapboxgl-880dc2d841af85e6651444f64ee09dcc72e85825.tar.gz
reuse curl handles
Diffstat (limited to 'linux')
-rw-r--r--linux/request.cpp25
-rw-r--r--linux/request.hpp4
2 files changed, 27 insertions, 2 deletions
diff --git a/linux/request.cpp b/linux/request.cpp
index b9066bf240..2c4ebd1bb1 100644
--- a/linux/request.cpp
+++ b/linux/request.cpp
@@ -51,15 +51,37 @@ Request::Request(std::string url, std::function<void(platform::Response&)> bg, s
llmr::util::threadpool->add(request, this);
}
+
+pthread_key_t Request::key;
+pthread_once_t Request::key_once = PTHREAD_ONCE_INIT;
+
+void Request::create_key() {
+ pthread_key_create(&key, delete_key);
+}
+
+void Request::delete_key(void *ptr) {
+ if (ptr != nullptr) {
+ curl_easy_cleanup(ptr);
+ }
+}
+
void Request::request(void *ptr) {
assert(curl_share);
Request *req = static_cast<Request *>(ptr);
Response res;
+ pthread_once(&key_once, create_key);
// TODO: use curl multi to be able to cancel, or to
- CURL *curl = curl_easy_init();
+ CURL *curl = nullptr;
+ if ((curl = pthread_getspecific(key)) == nullptr) {
+ curl = curl_easy_init();
+ pthread_setspecific(key, curl);
+ }
+
+ curl_easy_reset(curl);
+
CURLcode code;
curl_easy_setopt(curl, CURLOPT_URL, req->url.c_str());
@@ -72,7 +94,6 @@ void Request::request(void *ptr) {
curl_easy_setopt(curl, CURLOPT_SHARE, curl_share);
code = curl_easy_perform(curl);
curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &res.code);
- curl_easy_cleanup(curl);
if (code != CURLE_ABORTED_BY_CALLBACK) {
req->background_function(res);
diff --git a/linux/request.hpp b/linux/request.hpp
index ae673e0da7..a61437c942 100644
--- a/linux/request.hpp
+++ b/linux/request.hpp
@@ -21,6 +21,8 @@ public:
void cancel();
private:
+ static void create_key();
+ static void delete_key(void *ptr);
static void request(void *);
static size_t curl_write_callback(void *, size_t, size_t, void *);
static int curl_progress_callback(void *, double, double, double, double);
@@ -28,6 +30,8 @@ private:
static void curl_share_unlock(CURL *, curl_lock_data, void *);
public:
+ static pthread_key_t key;
+ static pthread_once_t key_once;
std::atomic<bool> done;
std::atomic<bool> cancelled;
const std::string url;