diff --git a/include/llmr/util/threadpool.hpp b/include/llmr/util/threadpool.hpp index f47bd5bf8..817cdc651 100644 --- a/include/llmr/util/threadpool.hpp +++ b/include/llmr/util/threadpool.hpp @@ -4,30 +4,33 @@ #include #include #include +#include +#include "std.hpp" namespace llmr { namespace util { class Threadpool { +private: class Worker { public: - Worker(Threadpool& pool); + Worker(Threadpool& pool, const std::string name); ~Worker(); static void *loop(void *ptr); private: Threadpool& pool; + const std::string name; pthread_t thread; }; public: - typedef void (*Callback)(void *); - typedef std::pair Task; Threadpool(int max_workers = 4); - + typedef void (*Callback)(void *); void add(Callback callback, void *data); private: + typedef std::pair Task; const int max_workers; pthread_mutex_t mutex = PTHREAD_RECURSIVE_MUTEX_INITIALIZER; pthread_cond_t condition = PTHREAD_COND_INITIALIZER; @@ -36,6 +39,8 @@ private: std::queue tasks; }; +extern std::unique_ptr threadpool; + } } diff --git a/linux/main.cpp b/linux/main.cpp index e9b6e4c36..a6814ab92 100644 --- a/linux/main.cpp +++ b/linux/main.cpp @@ -39,8 +39,13 @@ public: glfwSetWindowUserPointer(window, this); glfwMakeContextCurrent(window); + int width, height; + glfwGetWindowSize(window, &width, &height); + int fb_width, fb_height; + glfwGetFramebufferSize(window, &fb_width, &fb_height); + settings.load(); - map.setup(); + map.setup((double)fb_width / width); resize(window, 0, 0); diff --git a/linux/request.cpp b/linux/request.cpp index 2a5d5d9be..b9066bf24 100644 --- a/linux/request.cpp +++ b/linux/request.cpp @@ -9,7 +9,6 @@ using namespace llmr::platform; -llmr::util::Threadpool *Request::pool = nullptr; CURLSH *Request::curl_share = nullptr; pthread_mutex_t Request::curl_share_mutex = PTHREAD_MUTEX_INITIALIZER; @@ -28,16 +27,10 @@ size_t Request::curl_write_callback(void *contents, size_t size, size_t nmemb, v int Request::curl_progress_callback(void *ptr, double dltotal, double dlnow, double ultotal, double ulnow) { Request *req = static_cast(ptr); - bool cancel = req->cancelled; - if (cancel) { - fprintf(stderr, "cancel download %s\n", req->url.c_str()); - } - return cancel; + return req->cancelled; } void Request::initialize() { - pool = new llmr::util::Threadpool(); - // curl init curl_global_init(CURL_GLOBAL_ALL); curl_share = curl_share_init(); @@ -46,7 +39,6 @@ void Request::initialize() { } void Request::finish() { - delete pool; curl_share_cleanup(curl_share); } @@ -56,8 +48,7 @@ Request::Request(std::string url, std::function bg, s url(url), background_function(bg), foreground_callback(fg) { - assert(pool); - pool->add(request, this); + llmr::util::threadpool->add(request, this); } void Request::request(void *ptr) { diff --git a/linux/request.hpp b/linux/request.hpp index ff72afc96..ae673e0da 100644 --- a/linux/request.hpp +++ b/linux/request.hpp @@ -7,11 +7,6 @@ namespace llmr { - -namespace util { -class Threadpool; -} - namespace platform { struct Response; @@ -40,7 +35,6 @@ public: const std::function foreground_callback; private: - static util::Threadpool* pool; static CURLSH *curl_share; static pthread_mutex_t curl_share_mutex; }; diff --git a/src/util/threadpool.cpp b/src/util/threadpool.cpp index cf533c22b..31d4fdc39 100644 --- a/src/util/threadpool.cpp +++ b/src/util/threadpool.cpp @@ -1,15 +1,18 @@ #include +#include using namespace llmr::util; +std::unique_ptr llmr::util::threadpool = std::make_unique(std::thread::hardware_concurrency()); + Threadpool::Threadpool(int max_workers) : max_workers(max_workers) { } void Threadpool::add(Callback callback, void *data) { if (worker_count < max_workers) { - workers.emplace_front(*this); worker_count++; + workers.emplace_front(*this, std::string("worker ") + std::to_string(worker_count)); } pthread_mutex_lock(&mutex); @@ -18,8 +21,9 @@ void Threadpool::add(Callback callback, void *data) { pthread_cond_signal(&condition); } -Threadpool::Worker::Worker(Threadpool& pool) - : pool(pool) { +Threadpool::Worker::Worker(Threadpool& pool, const std::string name) + : pool(pool), + name(name) { pthread_create(&thread, nullptr, loop, (void *)this); } @@ -32,7 +36,7 @@ void *Threadpool::Worker::loop(void *ptr) { Worker *worker = static_cast(ptr); Threadpool& pool = worker->pool; - pthread_setname_np("worker"); + pthread_setname_np(worker->name.c_str()); pthread_mutex_lock(&pool.mutex); while (true) { if (pool.tasks.size()) {