diff options
author | John Firebaugh <john.firebaugh@gmail.com> | 2015-04-13 16:41:37 -0700 |
---|---|---|
committer | John Firebaugh <john.firebaugh@gmail.com> | 2015-04-15 20:38:25 -0700 |
commit | 1eaf6b8c18d52fb060bb70a76a516c8a7c56a3fa (patch) | |
tree | c03669cce16494bf720be33fb26d68a6242e7c23 /platform/default | |
parent | 1066504008ab9d779fcc007d3956d5966b7892e8 (diff) | |
download | qtlocation-mapboxgl-1eaf6b8c18d52fb060bb70a76a516c8a7c56a3fa.tar.gz |
Stricter Thread<> interface
Implements a Thread<>::invoke interface that only permits calling
member functions with bound arguments.
Also introduce invokeWithResult, which accepts a callback and invokes
it, passing the result, on the invoking thread's RunLoop. This closes
the loop of two evented threads doing message passing.
Diffstat (limited to 'platform/default')
-rw-r--r-- | platform/default/sqlite_cache.cpp | 21 | ||||
-rw-r--r-- | platform/default/sqlite_cache_impl.hpp | 9 |
2 files changed, 11 insertions, 19 deletions
diff --git a/platform/default/sqlite_cache.cpp b/platform/default/sqlite_cache.cpp index 958cdc1ac1..e73618f2b4 100644 --- a/platform/default/sqlite_cache.cpp +++ b/platform/default/sqlite_cache.cpp @@ -5,7 +5,6 @@ #include <mbgl/util/compression.hpp> #include <mbgl/util/io.hpp> #include <mbgl/util/thread.hpp> -#include <mbgl/util/run_loop.hpp> #include <mbgl/platform/log.hpp> #include "sqlite3.hpp" @@ -62,16 +61,14 @@ std::string unifyMapboxURLs(const std::string &url) { using namespace mapbox::sqlite; -SQLiteCache::SQLiteCache(const std::string& path_) : thread(util::make_unique<util::Thread<Impl>>(path_)) { +SQLiteCache::SQLiteCache(const std::string& path_) + : thread(util::make_unique<util::Thread<Impl>>("SQLite Cache", path_)) { } SQLiteCache::~SQLiteCache() = default; SQLiteCache::Impl::Impl(const std::string& path_) : path(path_) { -#ifdef __APPLE__ - pthread_setname_np("SQLite Cache"); -#endif } SQLiteCache::Impl::~Impl() { @@ -134,10 +131,10 @@ void SQLiteCache::get(const Resource &resource, Callback callback) { // Will try to load the URL from the SQLite database and call the callback when done. // Note that the callback is probably going to invoked from another thread, so the caller // must make sure that it can run in that thread. - (*thread)->invoke([=] { (*thread)->processGet(resource, callback); }); + thread->invokeWithResult(&Impl::processGet, callback, resource); } -void SQLiteCache::Impl::processGet(const Resource &resource, Callback callback) { +std::unique_ptr<Response> SQLiteCache::Impl::processGet(const Resource &resource) { try { // This is called in the SQLite event loop. if (!db) { @@ -170,14 +167,14 @@ void SQLiteCache::Impl::processGet(const Resource &resource, Callback callback) if (getStmt->get<int>(5)) { // == compressed response->data = util::decompress(response->data); } - callback(std::move(response)); + return std::move(response); } else { // There is no data. - callback(nullptr); + return nullptr; } } catch (mapbox::sqlite::Exception& ex) { Log::Error(Event::Database, ex.code, ex.what()); - callback(nullptr); + return nullptr; } } @@ -186,9 +183,9 @@ void SQLiteCache::put(const Resource &resource, std::shared_ptr<const Response> // storing a new response or updating the currently stored response, potentially setting a new // expiry date. if (hint == Hint::Full) { - (*thread)->invoke([=] { (*thread)->processPut(resource, response); }); + thread->invoke(&Impl::processPut, resource, std::move(response)); } else if (hint == Hint::Refresh) { - (*thread)->invoke([=] { (*thread)->processRefresh(resource, response->expires); }); + thread->invoke(&Impl::processRefresh, resource, int64_t(response->expires)); } } diff --git a/platform/default/sqlite_cache_impl.hpp b/platform/default/sqlite_cache_impl.hpp index a194f9e782..b13c7c2cd3 100644 --- a/platform/default/sqlite_cache_impl.hpp +++ b/platform/default/sqlite_cache_impl.hpp @@ -2,7 +2,6 @@ #define MBGL_STORAGE_DEFAULT_SQLITE_CACHE_IMPL #include <mbgl/storage/sqlite_cache.hpp> -#include <mbgl/util/run_loop.hpp> namespace mapbox { namespace sqlite { @@ -13,15 +12,12 @@ class Statement; namespace mbgl { -class SQLiteCache::Impl : public util::RunLoop { - friend class util::Thread<SQLiteCache::Impl>; - +class SQLiteCache::Impl { public: Impl(const std::string &path = ":memory:"); ~Impl(); -public: - void processGet(const Resource& resource, Callback callback); + std::unique_ptr<Response> processGet(const Resource&); void processPut(const Resource& resource, std::shared_ptr<const Response> response); void processRefresh(const Resource& resource, int64_t expires); @@ -29,7 +25,6 @@ private: void createDatabase(); void createSchema(); -private: const std::string path; std::unique_ptr<::mapbox::sqlite::Database> db; std::unique_ptr<::mapbox::sqlite::Statement> getStmt; |