summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--include/mbgl/util/uv_detail.hpp18
-rw-r--r--src/map/map.cpp21
2 files changed, 28 insertions, 11 deletions
diff --git a/include/mbgl/util/uv_detail.hpp b/include/mbgl/util/uv_detail.hpp
index 7d9e657f5c..099e08d4f1 100644
--- a/include/mbgl/util/uv_detail.hpp
+++ b/include/mbgl/util/uv_detail.hpp
@@ -20,10 +20,26 @@ void close(T* handle) {
class thread : public mbgl::util::noncopyable {
public:
- inline operator uv_thread_t *() { return &t; }
+ inline thread(std::function<void ()> fn_)
+ : fn(fn_) {
+ if (uv_thread_create(&t, thread_cb, this) != 0) {
+ throw std::runtime_error("failed to initialize thread");
+ }
+ }
+
+ void join() {
+ if (uv_thread_join(&t) != 0) {
+ throw std::runtime_error("failed to join thred");
+ }
+ }
private:
+ static void thread_cb(void* data) {
+ reinterpret_cast<thread*>(data)->fn();
+ }
+
uv_thread_t t;
+ std::function<void ()> fn;
};
class loop : public mbgl::util::noncopyable {
diff --git a/src/map/map.cpp b/src/map/map.cpp
index 42e7a7dcff..4e588943da 100644
--- a/src/map/map.cpp
+++ b/src/map/map.cpp
@@ -87,7 +87,6 @@ using namespace mbgl;
Map::Map(View& view_)
: loop(std::make_unique<uv::loop>()),
- thread(std::make_unique<uv::thread>()),
view(view_),
#ifndef NDEBUG
main_thread(uv_thread_self()),
@@ -180,23 +179,25 @@ void Map::start() {
painter.cleanup();
});
- uv_thread_create(*thread, [](void *arg) {
- Map *map = static_cast<Map *>(arg);
+ thread = std::make_unique<uv::thread>([this]() {
#ifndef NDEBUG
- map->map_thread = uv_thread_self();
+ map_thread = uv_thread_self();
#endif
+
#ifdef __APPLE__
pthread_setname_np("Map");
#endif
- map->run();
+
+ run();
+
#ifndef NDEBUG
- map->map_thread = -1;
+ map_thread = -1;
#endif
// Make sure that the stop() function knows when to stop invoking the callback function.
- map->is_stopped = true;
- map->view.notify();
- }, this);
+ is_stopped = true;
+ view.notify();
+ });
}
void Map::stop(stop_callback cb, void *data) {
@@ -220,7 +221,7 @@ void Map::stop(stop_callback cb, void *data) {
// If a callback function was provided, this should return immediately because the thread has
// already finished executing.
- uv_thread_join(*thread);
+ thread->join();
async = false;
}