From fd2b2e208229259630f92bd7615f9197c163d27f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Konstantin=20Ka=CC=88fer?= Date: Tue, 16 Sep 2014 18:23:42 +0200 Subject: use separate workers for tile parsing to avoid blocking the threadpool for other libuv actions --- include/mbgl/util/uv_detail.hpp | 35 ++++++++++++++++++++++++----------- 1 file changed, 24 insertions(+), 11 deletions(-) (limited to 'include/mbgl/util/uv_detail.hpp') 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 +#include #include @@ -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 class work { public: @@ -115,30 +132,26 @@ public: typedef void (*after_work_callback)(T &object); template - work(const mbgl::util::ptr &loop, work_callback work_cb, after_work_callback after_work_cb, Args&&... args) - : loop(loop), - data(std::forward(args)...), + work(worker &worker, work_callback work_cb, after_work_callback after_work_cb, Args&&... args) + : data(std::forward(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 *w = static_cast *>(req->data); + static void do_work(void *data) { + work *w = reinterpret_cast *>(data); w->work_cb(w->data); } - static void after_work(uv_work_t *req, int) { - work *w = static_cast *>(req->data); + static void after_work(void *data) { + work *w = reinterpret_cast *>(data); w->after_work_cb(w->data); delete w; } private: - mbgl::util::ptr loop; - uv_work_t req; T data; work_callback work_cb; after_work_callback after_work_cb; -- cgit v1.2.1