diff options
author | Konstantin Käfer <mail@kkaefer.com> | 2015-01-29 18:30:46 +0100 |
---|---|---|
committer | Konstantin Käfer <mail@kkaefer.com> | 2015-02-04 10:49:09 +0100 |
commit | 29baacf3a5bb773d94d08d16b81c3cda45a44eb6 (patch) | |
tree | 1dc3ca456151138ee5e8b7cf88b3afcecc3df1db /include/mbgl/storage | |
parent | 3d51e116a84ee168975bcee8377e9156f77e2731 (diff) | |
download | qtlocation-mapboxgl-29baacf3a5bb773d94d08d16b81c3cda45a44eb6.tar.gz |
refactor makefile
Diffstat (limited to 'include/mbgl/storage')
-rw-r--r-- | include/mbgl/storage/asset_request_baton.hpp | 38 | ||||
-rw-r--r-- | include/mbgl/storage/default/http_context.hpp | 61 | ||||
-rw-r--r-- | include/mbgl/storage/default/request.hpp | 4 | ||||
-rw-r--r-- | include/mbgl/storage/default/shared_request_base.hpp | 3 | ||||
-rw-r--r-- | include/mbgl/storage/default/thread_context.hpp | 78 | ||||
-rw-r--r-- | include/mbgl/storage/default_file_source.hpp (renamed from include/mbgl/storage/default/default_file_source.hpp) | 0 |
6 files changed, 91 insertions, 93 deletions
diff --git a/include/mbgl/storage/asset_request_baton.hpp b/include/mbgl/storage/asset_request_baton.hpp deleted file mode 100644 index 2cbb6c51c0..0000000000 --- a/include/mbgl/storage/asset_request_baton.hpp +++ /dev/null @@ -1,38 +0,0 @@ -#ifndef MBGL_STORAGE_ASSET_REQUEST_BATON -#define MBGL_STORAGE_ASSET_REQUEST_BATON - -#include <mbgl/util/uv.hpp> -#include <thread> - -#include <uv.h> - -namespace mbgl { - -class AssetRequest; - -struct AssetRequestBaton { - AssetRequestBaton(AssetRequest *request_, const std::string &path, uv_loop_t *loop); - - const std::thread::id threadId; - AssetRequest *request = nullptr; - std::unique_ptr<uv::async> asyncRun; - std::string path; - bool canceled = false; - - void cancel(); - static void notifyError(AssetRequestBaton *ptr, const int code, const char *message); - static void notifySuccess(AssetRequestBaton *ptr, const std::string body); - static void cleanup(AssetRequestBaton *ptr); - - // IMPLEMENT THIS PLATFORM SPECIFIC FUNCTION: - - // Called to load the asset. Platform-specific implementation. - static void run(AssetRequestBaton *ptr); - -}; - - -} - - -#endif diff --git a/include/mbgl/storage/default/http_context.hpp b/include/mbgl/storage/default/http_context.hpp index 18f17ef0cb..6b9518dab3 100644 --- a/include/mbgl/storage/default/http_context.hpp +++ b/include/mbgl/storage/default/http_context.hpp @@ -1,17 +1,9 @@ #ifndef MBGL_STORAGE_DEFAULT_HTTP_CONTEXT #define MBGL_STORAGE_DEFAULT_HTTP_CONTEXT +#include "thread_context.hpp" #include <mbgl/storage/network_status.hpp> -#include <mbgl/util/noncopyable.hpp> -#include <mbgl/util/std.hpp> -#include <mbgl/util/util.hpp> -#include <mbgl/util/uv.hpp> -#include <uv.h> -#include <pthread.h> - -#include <map> -#include <cassert> #include <set> namespace mbgl { @@ -23,18 +15,7 @@ class HTTPRequest; // triggers immediate retries on all requests waiting for network status changes. template <typename Context> -class HTTPContext : private util::noncopyable { -protected: - MBGL_STORE_THREAD(tid) - using Map = std::map<uv_loop_t *, std::unique_ptr<Context>>; - -public: - static Context *Get(uv_loop_t *loop); - -private: - static pthread_key_t key; - static pthread_once_t once; - +class HTTPContext : public ThreadContext<Context> { public: HTTPContext(uv_loop_t *loop); ~HTTPContext(); @@ -43,8 +24,6 @@ public: void removeRequest(HTTPRequest *baton); public: - uv_loop_t *loop; - // Will be fired when the network status becomes reachable. uv_async_t *reachability = nullptr; @@ -54,35 +33,15 @@ public: }; template <typename Context> -Context *HTTPContext<Context>::Get(uv_loop_t *loop) { - pthread_once(&once, []() { - pthread_key_create(&key, [](void *ptr) { - assert(ptr); - delete reinterpret_cast<Map *>(ptr); - }); - }); - auto contexts = reinterpret_cast<Map *>(pthread_getspecific(key)); - if (!contexts) { - contexts = new Map(); - pthread_setspecific(key, contexts); - } - - // Now find a HTTPContext that matches the requested loop. - auto it = contexts->find(loop); - if (it == contexts->end()) { - auto result = contexts->emplace(loop, util::make_unique<Context>(loop)); - assert(result.second); // Make sure it was actually inserted. - return result.first->second.get(); - } else { - return it->second.get(); - } -} - -template <typename Context> -HTTPContext<Context>::HTTPContext(uv_loop_t *loop_) : loop(loop_) { +HTTPContext<Context>::HTTPContext(uv_loop_t *loop_) + : ThreadContext<Context>(loop_) { reachability = new uv_async_t; reachability->data = this; - uv_async_init(loop, reachability, [](uv_async_t *async, int) { +#if UV_VERSION_MAJOR == 0 && UV_VERSION_MINOR <= 10 + uv_async_init(loop_, reachability, [](uv_async_t *async, int) { +#else + uv_async_init(loop_, reachability, [](uv_async_t *async) { +#endif for (auto request : reinterpret_cast<Context *>(async->data)->requests) { request->retryImmediately(); } @@ -94,7 +53,7 @@ HTTPContext<Context>::HTTPContext(uv_loop_t *loop_) : loop(loop_) { template <typename Context> HTTPContext<Context>::~HTTPContext() { - MBGL_VERIFY_THREAD(tid); + MBGL_VERIFY_THREAD(HTTPContext<Context>::tid); assert(requests.empty()); diff --git a/include/mbgl/storage/default/request.hpp b/include/mbgl/storage/default/request.hpp index 528a49f748..81ed14a568 100644 --- a/include/mbgl/storage/default/request.hpp +++ b/include/mbgl/storage/default/request.hpp @@ -33,8 +33,8 @@ public: void cancel(); private: - static void notifyCallback(uv_async_t *async, int); - static void cancelCallback(uv_async_t *async, int); + static void notifyCallback(uv_async_t *async); + static void cancelCallback(uv_async_t *async); private: uv_async_t *notify_async = nullptr; diff --git a/include/mbgl/storage/default/shared_request_base.hpp b/include/mbgl/storage/default/shared_request_base.hpp index 8591d66ba3..a0f3e8d4e3 100644 --- a/include/mbgl/storage/default/shared_request_base.hpp +++ b/include/mbgl/storage/default/shared_request_base.hpp @@ -3,11 +3,10 @@ #include <mbgl/storage/resource.hpp> #include <mbgl/storage/file_cache.hpp> +#include <mbgl/storage/default_file_source.hpp> #include <mbgl/util/util.hpp> #include <mbgl/util/noncopyable.hpp> -#include "default_file_source.hpp" - #include <string> #include <set> #include <cassert> diff --git a/include/mbgl/storage/default/thread_context.hpp b/include/mbgl/storage/default/thread_context.hpp new file mode 100644 index 0000000000..763c83a25b --- /dev/null +++ b/include/mbgl/storage/default/thread_context.hpp @@ -0,0 +1,78 @@ +#ifndef MBGL_STORAGE_DEFAULT_THREAD_CONTEXT +#define MBGL_STORAGE_DEFAULT_THREAD_CONTEXT + +#include <mbgl/util/noncopyable.hpp> +#include <mbgl/util/std.hpp> +#include <mbgl/util/util.hpp> +#include <mbgl/util/uv.hpp> + +#include <uv.h> +#include <pthread.h> + +#include <map> +#include <cassert> + +namespace mbgl { + +// This is a template class that provides a per-thread and per-loop Context object. It can be used +// by implementations to store global state. + +template <typename Context> +class ThreadContext : private util::noncopyable { +protected: + MBGL_STORE_THREAD(tid) + using Map = std::map<uv_loop_t *, std::unique_ptr<Context>>; + +public: + static Context *Get(uv_loop_t *loop); + +private: + static pthread_key_t key; + static pthread_once_t once; + +public: + ThreadContext(uv_loop_t *loop); + ~ThreadContext(); + +public: + uv_loop_t *loop; +}; + +template <typename Context> +Context *ThreadContext<Context>::Get(uv_loop_t *loop) { + pthread_once(&once, []() { + pthread_key_create(&key, [](void *ptr) { + assert(ptr); + delete reinterpret_cast<Map *>(ptr); + }); + }); + auto contexts = reinterpret_cast<Map *>(pthread_getspecific(key)); + if (!contexts) { + contexts = new Map(); + pthread_setspecific(key, contexts); + } + + // Now find a ThreadContext that matches the requested loop. + auto it = contexts->find(loop); + if (it == contexts->end()) { + auto result = contexts->emplace(loop, util::make_unique<Context>(loop)); + assert(result.second); // Make sure it was actually inserted. + return result.first->second.get(); + } else { + return it->second.get(); + } +} + +template <typename Context> +ThreadContext<Context>::ThreadContext(uv_loop_t *loop_) : loop(loop_) { +} + +template <typename Context> +ThreadContext<Context>::~ThreadContext() { + MBGL_VERIFY_THREAD(tid); +} + + +} + +#endif diff --git a/include/mbgl/storage/default/default_file_source.hpp b/include/mbgl/storage/default_file_source.hpp index 21048e99e5..21048e99e5 100644 --- a/include/mbgl/storage/default/default_file_source.hpp +++ b/include/mbgl/storage/default_file_source.hpp |