diff options
author | Konstantin Käfer <mail@kkaefer.com> | 2014-09-16 18:23:42 +0200 |
---|---|---|
committer | Konstantin Käfer <mail@kkaefer.com> | 2014-09-24 16:14:09 +0200 |
commit | fd2b2e208229259630f92bd7615f9197c163d27f (patch) | |
tree | 48cc7012e05c9c09e4bf437e0bab6598025e3f55 /include/mbgl/util/uv_detail.hpp | |
parent | f26bb724dc164b69b5358f30c6248fdf5cbd076b (diff) | |
download | qtlocation-mapboxgl-fd2b2e208229259630f92bd7615f9197c163d27f.tar.gz |
use separate workers for tile parsing to avoid blocking the threadpool for other libuv actions
Diffstat (limited to 'include/mbgl/util/uv_detail.hpp')
-rw-r--r-- | include/mbgl/util/uv_detail.hpp | 35 |
1 files changed, 24 insertions, 11 deletions
diff --git a/include/mbgl/util/uv_detail.hpp b/include/mbgl/util/uv_detail.hpp index 272ca41495..b0b4fe583f 100644 --- a/include/mbgl/util/uv_detail.hpp +++ b/include/mbgl/util/uv_detail.hpp @@ -2,6 +2,7 @@ #define MBGL_UTIL_UV_DETAIL #include <mbgl/util/ptr.hpp> +#include <mbgl/util/uv-worker.h> #include <uv.h> @@ -108,6 +109,22 @@ private: uv_once_t o = UV_ONCE_INIT; }; +class worker { +public: + inline worker(uv_loop_t *loop, unsigned int count, const char *name = nullptr) { + uv_worker_init(&w, loop, count, name); + } + inline ~worker() { + uv_worker_close(&w); + } + inline void add(void *data, uv_worker_cb work_cb, uv_worker_after_cb after_work_cb) { + uv_worker_send(&w, data, work_cb, after_work_cb); + } + +private: + uv_worker_t w; +}; + template <typename T> class work { public: @@ -115,30 +132,26 @@ public: typedef void (*after_work_callback)(T &object); template<typename... Args> - work(const mbgl::util::ptr<loop> &loop, work_callback work_cb, after_work_callback after_work_cb, Args&&... args) - : loop(loop), - data(std::forward<Args>(args)...), + work(worker &worker, work_callback work_cb, after_work_callback after_work_cb, Args&&... args) + : data(std::forward<Args>(args)...), work_cb(work_cb), after_work_cb(after_work_cb) { - req.data = this; - uv_queue_work(**loop, &req, do_work, after_work); + worker.add(this, do_work, after_work); } private: - static void do_work(uv_work_t *req) { - work<T> *w = static_cast<work<T> *>(req->data); + static void do_work(void *data) { + work<T> *w = reinterpret_cast<work<T> *>(data); w->work_cb(w->data); } - static void after_work(uv_work_t *req, int) { - work<T> *w = static_cast<work<T> *>(req->data); + static void after_work(void *data) { + work<T> *w = reinterpret_cast<work<T> *>(data); w->after_work_cb(w->data); delete w; } private: - mbgl::util::ptr<uv::loop> loop; - uv_work_t req; T data; work_callback work_cb; after_work_callback after_work_cb; |