Use uv::async and various other fixes
This commit is contained in:
@@ -2,29 +2,27 @@
|
||||
#define MBGL_STORAGE_ASSET_REQUEST_BATON
|
||||
|
||||
#include <mbgl/storage/asset_request.hpp>
|
||||
|
||||
#include <uv.h>
|
||||
#include <mbgl/util/uv.hpp>
|
||||
|
||||
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<uv_async_t> async_run;
|
||||
std::unique_ptr<uv::async> 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);
|
||||
|
||||
};
|
||||
|
||||
|
||||
@@ -24,4 +24,4 @@ private:
|
||||
|
||||
}
|
||||
|
||||
#endif
|
||||
#endif
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -5,18 +5,21 @@
|
||||
|
||||
#include <boost/make_unique.hpp>
|
||||
|
||||
#include <cerrno>
|
||||
// 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 <zip.h>
|
||||
|
||||
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<char[]> message = boost::make_unique<char[]>(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_uint64_t>(zip_fread(apk_file, reinterpret_cast<void *>(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);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -15,7 +15,7 @@ dispatch_once_t request_initialize = 0;
|
||||
NSURLSession *session = nullptr;
|
||||
|
||||
void HTTPRequestBaton::start(const util::ptr<HTTPRequestBaton> &ptr) {
|
||||
assert(uv_thread_self() == ptr->thread_id);
|
||||
assert(uv_thread_self() == ptr->threadId);
|
||||
|
||||
// Starts the request.
|
||||
util::ptr<HTTPRequestBaton> baton = ptr;
|
||||
@@ -131,7 +131,7 @@ void HTTPRequestBaton::start(const util::ptr<HTTPRequestBaton> &ptr) {
|
||||
}
|
||||
|
||||
void HTTPRequestBaton::stop(const util::ptr<HTTPRequestBaton> &ptr) {
|
||||
assert(uv_thread_self() == ptr->thread_id);
|
||||
assert(uv_thread_self() == ptr->threadId);
|
||||
assert(ptr->ptr);
|
||||
|
||||
NSURLSessionDataTask *task = CFBridgingRelease(ptr->ptr);
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -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<HTTPRequestBaton> &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<HTTPRequestBaton>(ptr));
|
||||
}
|
||||
|
||||
// This function must be run from the main thread (== where the HTTPRequestBaton was created)
|
||||
void HTTPRequestBaton::stop(const util::ptr<HTTPRequestBaton> &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<HTTPRequestBaton>(ptr));
|
||||
}
|
||||
|
||||
@@ -22,6 +22,7 @@
|
||||
#include <mbgl/storage/file_source.hpp>
|
||||
#include <mbgl/platform/log.hpp>
|
||||
#include <mbgl/util/string.hpp>
|
||||
#include <mbgl/util/uv.hpp>
|
||||
|
||||
#include <algorithm>
|
||||
#include <iostream>
|
||||
@@ -627,8 +628,7 @@ void Map::setAppliedClasses(const std::vector<std::string> &classes) {
|
||||
}
|
||||
}
|
||||
else {
|
||||
std::lock_guard
|
||||
d::lock_guard<std::mutex> lock(appliedClassesMutex);
|
||||
std::lock_guard<std::mutex> lock(appliedClassesMutex);
|
||||
appliedClasses = std::make_unique<std::vector<std::string>>(classes);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,44 +1,31 @@
|
||||
#include <mbgl/storage/asset_request_baton.hpp>
|
||||
#include <mbgl/storage/asset_request.hpp>
|
||||
#include <mbgl/storage/response.hpp>
|
||||
#include <mbgl/util/std.hpp>
|
||||
#include <mbgl/util/uv_detail.hpp>
|
||||
|
||||
#include <uv.h>
|
||||
|
||||
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<uv::async>(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<Response>(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();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -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<FileRequestBaton *>(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<FileRequestBaton *>(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<FileRequestBaton *>(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<FileRequestBaton *>(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<FileRequestBaton *>(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<FileRequestBaton *>(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;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -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() {
|
||||
|
||||
Reference in New Issue
Block a user