diff options
author | Ivo van Dongen <info@ivovandongen.nl> | 2017-05-26 18:40:49 +0300 |
---|---|---|
committer | Ivo van Dongen <ivovandongen@users.noreply.github.com> | 2017-05-27 10:05:36 +0300 |
commit | 5cf0fc0f8bb3f66fd7844cae73efca1a4c2d94f5 (patch) | |
tree | 7eeaf0d37926bcbda1dc4dea07d79dfbfa9c41d0 | |
parent | 89129379f18b913a7c2b7a7ff99644e277e9d3ff (diff) | |
download | qtlocation-mapboxgl-5cf0fc0f8bb3f66fd7844cae73efca1a4c2d94f5.tar.gz |
[core] default file source - ensure thread safety for cached values
-rw-r--r-- | include/mbgl/storage/default_file_source.hpp | 9 | ||||
-rw-r--r-- | platform/default/default_file_source.cpp | 18 |
2 files changed, 21 insertions, 6 deletions
diff --git a/include/mbgl/storage/default_file_source.hpp b/include/mbgl/storage/default_file_source.hpp index 1e60d2bc5a..f929334f28 100644 --- a/include/mbgl/storage/default_file_source.hpp +++ b/include/mbgl/storage/default_file_source.hpp @@ -5,6 +5,7 @@ #include <mbgl/util/constants.hpp> #include <vector> +#include <mutex> namespace mbgl { @@ -34,10 +35,10 @@ public: } void setAPIBaseURL(const std::string&); - std::string getAPIBaseURL() const; + std::string getAPIBaseURL(); void setAccessToken(const std::string&); - std::string getAccessToken() const; + std::string getAccessToken(); void setResourceTransform(std::function<std::string(Resource::Kind, std::string&&)>); @@ -143,7 +144,11 @@ private: // Shared so destruction is done on this thread const std::shared_ptr<FileSource> assetFileSource; const std::unique_ptr<util::Thread<Impl>> thread; + + std::mutex cachedBaseURLMutex; std::string cachedBaseURL = mbgl::util::API_BASE_URL; + + std::mutex cachedAccessTokenMutex; std::string cachedAccessToken; }; diff --git a/platform/default/default_file_source.cpp b/platform/default/default_file_source.cpp index 142be5228f..adb3f6a83d 100644 --- a/platform/default/default_file_source.cpp +++ b/platform/default/default_file_source.cpp @@ -197,19 +197,29 @@ DefaultFileSource::~DefaultFileSource() = default; void DefaultFileSource::setAPIBaseURL(const std::string& baseURL) { thread->invoke(&Impl::setAPIBaseURL, baseURL); - cachedBaseURL = baseURL; + + { + std::lock_guard<std::mutex> lock(cachedBaseURLMutex); + cachedBaseURL = baseURL; + } } -std::string DefaultFileSource::getAPIBaseURL() const { +std::string DefaultFileSource::getAPIBaseURL() { + std::lock_guard<std::mutex> lock(cachedBaseURLMutex); return cachedBaseURL; } void DefaultFileSource::setAccessToken(const std::string& accessToken) { thread->invoke(&Impl::setAccessToken, accessToken); - cachedAccessToken = accessToken; + + { + std::lock_guard<std::mutex> lock(cachedAccessTokenMutex); + cachedAccessToken = accessToken; + } } -std::string DefaultFileSource::getAccessToken() const { +std::string DefaultFileSource::getAccessToken() { + std::lock_guard<std::mutex> lock(cachedAccessTokenMutex); return cachedAccessToken; } |