diff --git a/include/mbgl/storage/asset_request_baton.hpp b/include/mbgl/storage/asset_request_baton.hpp index b9c337259..7f7bd69c6 100644 --- a/include/mbgl/storage/asset_request_baton.hpp +++ b/include/mbgl/storage/asset_request_baton.hpp @@ -2,29 +2,27 @@ #define MBGL_STORAGE_ASSET_REQUEST_BATON #include - -#include +#include namespace mbgl { struct AssetRequestBaton { AssetRequestBaton(AssetRequest *request_, const std::string &path, uv_loop_t *loop); - ~AssetRequestBaton(); - const unsigned long thread_id; + const unsigned long threadId; AssetRequest *request = nullptr; - std::unique_ptr async_run; + std::unique_ptr asyncRun; std::string path; bool canceled = false; void cancel(); - static void notify_error(uv_async_t *async, const int code, const char *message); - static void cleanup(uv_async_t *async); + static void notify_error(AssetRequestBaton *ptr, const int code, const char *message); + static void cleanup(AssetRequestBaton *ptr); // IMPLEMENT THIS PLATFORM SPECIFIC FUNCTION: // Called to load the asset. Platform-specific implementation. - static void run(uv_async_t *async); + static void run(AssetRequestBaton *ptr); }; diff --git a/include/mbgl/storage/file_request.hpp b/include/mbgl/storage/file_request.hpp index 2f883728f..3de2d5b60 100644 --- a/include/mbgl/storage/file_request.hpp +++ b/include/mbgl/storage/file_request.hpp @@ -24,4 +24,4 @@ private: } -#endif \ No newline at end of file +#endif diff --git a/include/mbgl/storage/file_request_baton.hpp b/include/mbgl/storage/file_request_baton.hpp index 0f1968ca1..1283038dd 100644 --- a/include/mbgl/storage/file_request_baton.hpp +++ b/include/mbgl/storage/file_request_baton.hpp @@ -19,7 +19,7 @@ struct FileRequestBaton { static void notify_error(uv_fs_t *req); static void cleanup(uv_fs_t *req); - const unsigned long thread_id; + const unsigned long threadId; FileRequest *request = nullptr; uv_fs_t req; uv_file fd = -1; diff --git a/include/mbgl/storage/http_request_baton.hpp b/include/mbgl/storage/http_request_baton.hpp index 279784ec2..c7c80466b 100644 --- a/include/mbgl/storage/http_request_baton.hpp +++ b/include/mbgl/storage/http_request_baton.hpp @@ -47,7 +47,7 @@ struct HTTPRequestBaton { HTTPRequestBaton(const std::string &path); ~HTTPRequestBaton(); - const unsigned long thread_id; + const unsigned long threadId; const std::string path; HTTPRequest *request = nullptr; diff --git a/platform/android/asset_request_baton_libzip.cpp b/platform/android/asset_request_baton_libzip.cpp index a492079c2..1d2f055bf 100644 --- a/platform/android/asset_request_baton_libzip.cpp +++ b/platform/android/asset_request_baton_libzip.cpp @@ -5,18 +5,21 @@ #include +#include +// NOTE a bug in the Android NDK breaks std::errno +// See https://code.google.com/p/android/issues/detail?id=72349 +// After this is fixed change usage errno to std::errno #include namespace mbgl { -void AssetRequestBaton::run(uv_async_t *async) { - AssetRequestBaton *ptr = (AssetRequestBaton *)async->data; - assert(ptr->thread_id == uv_thread_self()); +void AssetRequestBaton::run(AssetRequestBaton *ptr) { + assert(ptr->threadId == uv_thread_self()); if (ptr->canceled || !ptr->request) { // Either the AssetRequest object has been destructed, or the // request was canceled. - cleanup(async); + cleanup(ptr); return; } @@ -27,8 +30,8 @@ void AssetRequestBaton::run(uv_async_t *async) { const int message_size = zip_error_to_str(nullptr, 0, error, errno); const std::unique_ptr message = boost::make_unique(message_size); zip_error_to_str(message.get(), 0, error, errno); - notify_error(async, 500, message.get()); - cleanup(async); + notify_error(ptr, 500, message.get()); + cleanup(ptr); return; } @@ -38,10 +41,10 @@ void AssetRequestBaton::run(uv_async_t *async) { // Opening the asset failed or was canceled. We already have an open file handle // though, which we'll have to close. zip_error_get(apk, &error, nullptr); - notify_error(async, error == ZIP_ER_NOENT ? 404 : 500, zip_strerror(apk)); + notify_error(ptr, error == ZIP_ER_NOENT ? 404 : 500, zip_strerror(apk)); zip_close(apk); apk = nullptr; - cleanup(async); + cleanup(ptr); return; } @@ -49,12 +52,12 @@ void AssetRequestBaton::run(uv_async_t *async) { if ((zip_stat(apk, apk_file_path.c_str(), ZIP_FL_NOCASE, &stat) != 0) || ptr->canceled || !ptr->request) { // Stating failed or was canceled. We already have an open file handle // though, which we'll have to close. - notify_error(async, 500, zip_strerror(apk)); + notify_error(ptr, 500, zip_strerror(apk)); zip_fclose(apk_file); apk_file = nullptr; zip_close(apk); apk = nullptr; - cleanup(async); + cleanup(ptr); return; } @@ -63,12 +66,12 @@ void AssetRequestBaton::run(uv_async_t *async) { if (static_cast(zip_fread(apk_file, reinterpret_cast(data.get()), stat.size)) != stat.size || ptr->canceled || !ptr->request) { // Reading failed or was canceled. We already have an open file handle // though, which we'll have to close. - notify_error(async, 500, zip_file_strerror(apk_file)); + notify_error(ptr, 500, zip_file_strerror(apk_file)); zip_fclose(apk_file); apk_file = nullptr; zip_close(apk); apk = nullptr; - cleanup(async); + cleanup(ptr); return; } @@ -90,7 +93,7 @@ void AssetRequestBaton::run(uv_async_t *async) { } apk = nullptr; - cleanup(async); + cleanup(ptr); } } diff --git a/platform/darwin/http_request_baton_cocoa.mm b/platform/darwin/http_request_baton_cocoa.mm index 03fa3bb82..1c54dbdc1 100644 --- a/platform/darwin/http_request_baton_cocoa.mm +++ b/platform/darwin/http_request_baton_cocoa.mm @@ -15,7 +15,7 @@ dispatch_once_t request_initialize = 0; NSURLSession *session = nullptr; void HTTPRequestBaton::start(const util::ptr &ptr) { - assert(uv_thread_self() == ptr->thread_id); + assert(uv_thread_self() == ptr->threadId); // Starts the request. util::ptr baton = ptr; @@ -131,7 +131,7 @@ void HTTPRequestBaton::start(const util::ptr &ptr) { } void HTTPRequestBaton::stop(const util::ptr &ptr) { - assert(uv_thread_self() == ptr->thread_id); + assert(uv_thread_self() == ptr->threadId); assert(ptr->ptr); NSURLSessionDataTask *task = CFBridgingRelease(ptr->ptr); diff --git a/platform/default/asset_request_baton_noop.cpp b/platform/default/asset_request_baton_noop.cpp index e0d5755a7..45083a50f 100644 --- a/platform/default/asset_request_baton_noop.cpp +++ b/platform/default/asset_request_baton_noop.cpp @@ -4,19 +4,19 @@ namespace mbgl { -void AssetRequestBaton::run(uv_async_t *async) { - AssetRequestBaton *ptr = (AssetRequestBaton *)async->data; - assert(ptr->thread_id == uv_thread_self()); +void AssetRequestBaton::run(AssetRequestBaton *ptr) { + assert(ptr->threadId == uv_thread_self()); if (ptr->canceled || !ptr->request) { // Either the AssetRequest object has been destructed, or the // request was canceled. - cleanup(async); + cleanup(ptr); return; } - notify_error(async, 500, "Assets not implemented on this platform."); - cleanup(async); + // Just return a 500 error until implemented properly + notify_error(ptr, 500, "Assets not implemented on this platform."); + cleanup(ptr); } } diff --git a/platform/default/http_request_baton_curl.cpp b/platform/default/http_request_baton_curl.cpp index a5b0edbbf..931f8955e 100644 --- a/platform/default/http_request_baton_curl.cpp +++ b/platform/default/http_request_baton_curl.cpp @@ -595,14 +595,14 @@ void create_thread() { // This function must be run from the main thread (== where the HTTPRequestBaton was created) void HTTPRequestBaton::start(const util::ptr &ptr) { - assert(uv_thread_self() == ptr->thread_id); + assert(uv_thread_self() == ptr->threadId); uv_once(&once, create_thread); uv_messenger_send(&start_messenger, new util::ptr(ptr)); } // This function must be run from the main thread (== where the HTTPRequestBaton was created) void HTTPRequestBaton::stop(const util::ptr &ptr) { - assert(uv_thread_self() == ptr->thread_id); + assert(uv_thread_self() == ptr->threadId); uv_once(&once, create_thread); uv_messenger_send(&stop_messenger, new util::ptr(ptr)); } diff --git a/src/map/map.cpp b/src/map/map.cpp index 5cbe04a72..9c0a08af1 100644 --- a/src/map/map.cpp +++ b/src/map/map.cpp @@ -22,6 +22,7 @@ #include #include #include +#include #include #include @@ -627,8 +628,7 @@ void Map::setAppliedClasses(const std::vector &classes) { } } else { - std::lock_guard - d::lock_guard lock(appliedClassesMutex); + std::lock_guard lock(appliedClassesMutex); appliedClasses = std::make_unique>(classes); } } diff --git a/src/storage/asset_request_baton.cpp b/src/storage/asset_request_baton.cpp index baefb3002..851751658 100644 --- a/src/storage/asset_request_baton.cpp +++ b/src/storage/asset_request_baton.cpp @@ -1,44 +1,31 @@ #include #include #include +#include +#include + +#include namespace mbgl { AssetRequestBaton::AssetRequestBaton(AssetRequest *request_, const std::string &path_, uv_loop_t *loop) - : thread_id(uv_thread_self()), + : threadId(uv_thread_self()), request(request_), - async_run(new uv_async_t()), path(path_) { -// Iron out the differences between libuv 0.10 and 0.11 -#ifdef UV_ASYNC_CALLBACK -#error Cannot overwrite UV_ASYNC_CALLBACK -#endif -#if UV_VERSION_MAJOR == 0 && UV_VERSION_MINOR <= 10 -#define UV_ASYNC_CALLBACK(name) [](uv_async_t *a, int) { return AssetRequestBaton::name(a); } -#else -#define UV_ASYNC_CALLBACK(name) name -#endif + asyncRun = std::make_unique(loop, [this]() { + run(this); + }); - uv_async_init(loop, async_run.get(), UV_ASYNC_CALLBACK(run)); - async_run->data = this; - -#undef UV_ASYNC_CALLBACK - - uv_async_send(async_run.get()); -} - -AssetRequestBaton::~AssetRequestBaton() { - uv_close((uv_handle_t *)async_run.get(), nullptr); + asyncRun->send(); } void AssetRequestBaton::cancel() { canceled = true; } -void AssetRequestBaton::notify_error(uv_async_t *async, const int code, const char *message) { - AssetRequestBaton *ptr = (AssetRequestBaton *)async->data; - assert(ptr->thread_id == uv_thread_self()); +void AssetRequestBaton::notify_error(AssetRequestBaton *ptr, const int code, const char *message) { + assert(ptr->threadId == uv_thread_self()); if (ptr->request && !ptr->canceled) { ptr->request->response = std::unique_ptr(new Response); @@ -48,15 +35,17 @@ void AssetRequestBaton::notify_error(uv_async_t *async, const int code, const ch } } -void AssetRequestBaton::cleanup(uv_async_t *async) { - AssetRequestBaton *ptr = (AssetRequestBaton *)async->data; - assert(ptr->thread_id == uv_thread_self()); +void AssetRequestBaton::cleanup(AssetRequestBaton *ptr) { + assert(ptr->threadId == uv_thread_self()); if (ptr->request) { ptr->request->ptr = nullptr; } delete ptr; + ptr = nullptr; + + ptr->asyncRun.reset(); } } diff --git a/src/storage/file_request_baton.cpp b/src/storage/file_request_baton.cpp index 991853b28..c30a9364f 100644 --- a/src/storage/file_request_baton.cpp +++ b/src/storage/file_request_baton.cpp @@ -7,7 +7,7 @@ namespace mbgl { FileRequestBaton::FileRequestBaton(FileRequest *request_, const std::string &path, uv_loop_t *loop) - : thread_id(uv_thread_self()), request(request_) { + : threadId(uv_thread_self()), request(request_) { req.data = this; uv_fs_open(loop, &req, path.c_str(), O_RDONLY, S_IRUSR, file_opened); } @@ -25,7 +25,7 @@ void FileRequestBaton::cancel() { } void FileRequestBaton::notify_error(uv_fs_t *req) { - FileRequestBaton *ptr = (FileRequestBaton *)req->data; + FileRequestBaton *ptr = reinterpret_cast(req->data); assert(ptr->thread_id == uv_thread_self()); if (ptr->request && req->result < 0 && !ptr->canceled && req->result != UV_ECANCELED) { @@ -41,7 +41,7 @@ void FileRequestBaton::notify_error(uv_fs_t *req) { } void FileRequestBaton::file_opened(uv_fs_t *req) { - FileRequestBaton *ptr = (FileRequestBaton *)req->data; + FileRequestBaton *ptr = reinterpret_cast(req->data); assert(ptr->thread_id == uv_thread_self()); if (req->result < 0) { @@ -66,7 +66,7 @@ void FileRequestBaton::file_opened(uv_fs_t *req) { } void FileRequestBaton::file_stated(uv_fs_t *req) { - FileRequestBaton *ptr = (FileRequestBaton *)req->data; + FileRequestBaton *ptr = reinterpret_cast(req->data); assert(ptr->thread_id == uv_thread_self()); if (req->result != 0 || ptr->canceled || !ptr->request) { @@ -113,7 +113,7 @@ void FileRequestBaton::file_stated(uv_fs_t *req) { } void FileRequestBaton::file_read(uv_fs_t *req) { - FileRequestBaton *ptr = (FileRequestBaton *)req->data; + FileRequestBaton *ptr = reinterpret_cast(req->data); assert(ptr->thread_id == uv_thread_self()); if (req->result < 0 || ptr->canceled || !ptr->request) { @@ -135,7 +135,7 @@ void FileRequestBaton::file_read(uv_fs_t *req) { } void FileRequestBaton::file_closed(uv_fs_t *req) { - assert(((FileRequestBaton *)req->data)->thread_id == uv_thread_self()); + assert(reinterpret_cast(req->data)->thread_id == uv_thread_self()); if (req->result < 0) { // Closing the file failed. But there isn't anything we can do. @@ -145,7 +145,7 @@ void FileRequestBaton::file_closed(uv_fs_t *req) { } void FileRequestBaton::cleanup(uv_fs_t *req) { - FileRequestBaton *ptr = (FileRequestBaton *)req->data; + FileRequestBaton *ptr = reinterpret_cast(req->data); assert(ptr->thread_id == uv_thread_self()); if (ptr->request) { @@ -154,6 +154,7 @@ void FileRequestBaton::cleanup(uv_fs_t *req) { uv_fs_req_cleanup(req); delete ptr; + ptr = nullptr; } } diff --git a/src/storage/http_request_baton.cpp b/src/storage/http_request_baton.cpp index 02edae748..e1ae42232 100644 --- a/src/storage/http_request_baton.cpp +++ b/src/storage/http_request_baton.cpp @@ -3,7 +3,7 @@ namespace mbgl { -HTTPRequestBaton::HTTPRequestBaton(const std::string &path_) : thread_id(uv_thread_self()), path(path_) { +HTTPRequestBaton::HTTPRequestBaton(const std::string &path_) : threadId(uv_thread_self()), path(path_) { } HTTPRequestBaton::~HTTPRequestBaton() {