diff options
author | Konstantin Käfer <mail@kkaefer.com> | 2014-04-22 16:54:55 +0200 |
---|---|---|
committer | Konstantin Käfer <mail@kkaefer.com> | 2014-04-22 16:54:55 +0200 |
commit | 1ecff578295a44cd33e476acf9960d989e65b99b (patch) | |
tree | 820b2769560b0460d188482dbd660639cacefd2f /linux | |
parent | 9ff9d0edb55bcf15eb030b96f57cc6d5c63b8c5c (diff) | |
download | qtlocation-mapboxgl-1ecff578295a44cd33e476acf9960d989e65b99b.tar.gz |
use libuv event loop in the glfw view
Integrates libuv's default loop with GLFW's glfwWaitEvents() call. See https://github.com/joyent/libuv/issues/1246 for more details on integrating event loops.
fixes #149
Diffstat (limited to 'linux')
-rw-r--r-- | linux/llmr-app.gyp | 4 | ||||
-rw-r--r-- | linux/main.cpp | 62 | ||||
-rw-r--r-- | linux/request.cpp | 119 | ||||
-rw-r--r-- | linux/request.hpp | 50 |
4 files changed, 2 insertions, 233 deletions
diff --git a/linux/llmr-app.gyp b/linux/llmr-app.gyp index e12737b92c..1bd6d55b18 100644 --- a/linux/llmr-app.gyp +++ b/linux/llmr-app.gyp @@ -12,10 +12,10 @@ './main.cpp', './settings.cpp', './settings.hpp', - './request.cpp', - './request.hpp', '../common/glfw_view.hpp', '../common/glfw_view.cpp', + '../common/curl_request.hpp', + '../common/curl_request.cpp', ], 'conditions': [ diff --git a/linux/main.cpp b/linux/main.cpp index 42aa579b8c..49de8a2ac8 100644 --- a/linux/main.cpp +++ b/linux/main.cpp @@ -6,15 +6,8 @@ #include "settings.hpp" #include "../common/glfw_view.hpp" -#include "request.hpp" - - MapView *mapView = nullptr; -std::forward_list<std::shared_ptr<llmr::platform::Request>> requests; - - - void quit_handler(int s) { if (mapView) { @@ -26,10 +19,6 @@ void quit_handler(int s) { } } - - - - int main(int argc, char *argv[]) { int fullscreen_flag = 0; @@ -51,13 +40,6 @@ int main(int argc, char *argv[]) { sigIntHandler.sa_flags = 0; sigaction(SIGINT, &sigIntHandler, NULL); - - // curl init - curl_global_init(CURL_GLOBAL_ALL); - - llmr::platform::Request::initialize(); - - // main loop llmr::Settings_JSON settings; mapView = new MapView(settings, fullscreen_flag); @@ -66,49 +48,5 @@ int main(int argc, char *argv[]) { mapView->settings.sync(); delete mapView; - - llmr::platform::Request::finish(); - - curl_global_cleanup(); return ret; } - -namespace llmr { - -void platform::cleanup() { - bool& dirty = mapView->dirty; - requests.remove_if([&dirty](std::shared_ptr<llmr::platform::Request> &req) { - if (req->done) { - req->foreground_callback(); - dirty = true; - return true; - } else { - return false; - } - }); -} - -void platform::restart() { - if (mapView) { - mapView->dirty = true; - } -} - -std::shared_ptr<platform::Request> -platform::request_http(const std::string &url, std::function<void(Response *)> background_function, - std::function<void()> foreground_callback) { - std::shared_ptr<Request> req = - std::make_shared<Request>(url, background_function, foreground_callback); - requests.push_front(req); - return req; -} - -// Cancels an HTTP request. -void platform::cancel_request_http(const std::shared_ptr<Request> &req) { - if (req) { - req->cancel(); - } -} - - -} diff --git a/linux/request.cpp b/linux/request.cpp deleted file mode 100644 index 617ee5e063..0000000000 --- a/linux/request.cpp +++ /dev/null @@ -1,119 +0,0 @@ -#include "request.hpp" - -#include <atomic> -#include <cassert> - -#include <llmr/util/threadpool.hpp> -#include <llmr/platform/platform.hpp> - -using namespace llmr::platform; - - -CURLSH *Request::curl_share = nullptr; -pthread_mutex_t Request::curl_share_mutex = PTHREAD_MUTEX_INITIALIZER; - -void Request::curl_share_lock(CURL *, curl_lock_data, curl_lock_access, void *) { - pthread_mutex_lock(&curl_share_mutex); -} - -void Request::curl_share_unlock(CURL *, curl_lock_data, void *) { - pthread_mutex_unlock(&curl_share_mutex); -} - -size_t Request::curl_write_callback(void *contents, size_t size, size_t nmemb, void *userp) { - ((std::string *)userp)->append((char *)contents, size * nmemb); - return size * nmemb; -} - -int Request::curl_progress_callback(void *ptr, double dltotal, double dlnow, double ultotal, double ulnow) { - Request *req = static_cast<Request *>(ptr); - return req->cancelled; -} - -void Request::initialize() { - // curl init - curl_global_init(CURL_GLOBAL_ALL); - - curl_share = curl_share_init(); - curl_share_setopt(curl_share, CURLSHOPT_LOCKFUNC, curl_share_lock); - curl_share_setopt(curl_share, CURLSHOPT_UNLOCKFUNC, curl_share_unlock); -} - -void Request::finish() { - pthread_key_delete(key); - curl_share_cleanup(curl_share); -} - -Request::Request(std::string url, std::function<void(platform::Response *)> bg, std::function<void()> fg) - : done(false), - cancelled(false), - url(url), - background_function(bg), - foreground_callback(fg) { - 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 = nullptr; - if ((curl = pthread_getspecific(key)) == nullptr) { - curl = curl_easy_init(); - - // stopgap to avoid libcurl crashes: - // see https://stackoverflow.com/q/9191668 - curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1); - - pthread_setspecific(key, curl); - } - - curl_easy_reset(curl); - - CURLcode code; - - curl_easy_setopt(curl, CURLOPT_URL, req->url.c_str()); - curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, curl_write_callback); - curl_easy_setopt(curl, CURLOPT_WRITEDATA, &res.body); - curl_easy_setopt(curl, CURLOPT_PROGRESSFUNCTION, curl_progress_callback); - curl_easy_setopt(curl, CURLOPT_PROGRESSDATA, req); - curl_easy_setopt(curl, CURLOPT_NOPROGRESS, 0); - curl_easy_setopt(curl, CURLOPT_ACCEPT_ENCODING, "deflate"); - curl_easy_setopt(curl, CURLOPT_SHARE, curl_share); - code = curl_easy_perform(curl); - curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &res.code); - - // Add human-readable error code - if (code != CURLE_OK) { - res.error_message = curl_easy_strerror(code); - res.code = -1; - } - - if (code != CURLE_ABORTED_BY_CALLBACK) { - req->background_function(&res); - } - - req->done = true; -} - -void Request::cancel() { - cancelled = true; -} diff --git a/linux/request.hpp b/linux/request.hpp deleted file mode 100644 index 8350d9d459..0000000000 --- a/linux/request.hpp +++ /dev/null @@ -1,50 +0,0 @@ -#ifndef LLMR_LINUX_REQUEST -#define LLMR_LINUX_REQUEST - -#include <string> -#include <functional> -#include <atomic> -#include <curl/curl.h> - - -namespace llmr { -namespace platform { - -struct Response; - -class Request { -public: - Request(std::string url, std::function<void(platform::Response *)> bg, std::function<void()> fg); - - static void initialize(); - static void finish(); - - 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); - static void curl_share_lock(CURL *, curl_lock_data, curl_lock_access, void *); - 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; - const std::function<void(platform::Response *)> background_function; - const std::function<void()> foreground_callback; - -private: - static CURLSH *curl_share; - static pthread_mutex_t curl_share_mutex; -}; - -} -} - -#endif |