From ad30ba2cd145846b1c8a6c8bd26b427d48dde60f Mon Sep 17 00:00:00 2001 From: Alexander Shalamov Date: Thu, 2 May 2019 11:40:23 +0200 Subject: [core] Guard access to CustomTileLoader's data members with mutex --- src/mbgl/style/custom_tile_loader.cpp | 10 +++++++--- src/mbgl/style/custom_tile_loader.hpp | 3 ++- 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/src/mbgl/style/custom_tile_loader.cpp b/src/mbgl/style/custom_tile_loader.cpp index 48fd5ae9e2..a266e60cfc 100644 --- a/src/mbgl/style/custom_tile_loader.cpp +++ b/src/mbgl/style/custom_tile_loader.cpp @@ -11,6 +11,7 @@ CustomTileLoader::CustomTileLoader(const TileFunction& fetchTileFn, const TileFu } void CustomTileLoader::fetchTile(const OverscaledTileID& tileID, ActorRef tileRef) { + std::lock_guard guard(dataMutex); auto cachedTileData = dataCache.find(tileID.canonical); if (cachedTileData != dataCache.end()) { tileRef.invoke(&CustomGeometryTile::setTileData, *(cachedTileData->second)); @@ -34,12 +35,14 @@ void CustomTileLoader::fetchTile(const OverscaledTileID& tileID, ActorRef guard(dataMutex); if (tileCallbackMap.find(tileID.canonical) != tileCallbackMap.end()) { invokeTileCancel(tileID.canonical); } } void CustomTileLoader::removeTile(const OverscaledTileID& tileID) { + std::lock_guard guard(dataMutex); auto tileCallbacks = tileCallbackMap.find(tileID.canonical); if (tileCallbacks == tileCallbackMap.end()) return; for (auto iter = tileCallbacks->second.begin(); iter != tileCallbacks->second.end(); iter++) { @@ -56,10 +59,10 @@ void CustomTileLoader::removeTile(const OverscaledTileID& tileID) { } void CustomTileLoader::setTileData(const CanonicalTileID& tileID, const GeoJSON& data) { - + std::lock_guard guard(dataMutex); auto iter = tileCallbackMap.find(tileID); if (iter == tileCallbackMap.end()) return; - auto dataPtr = std::make_unique(std::move(data)); + auto dataPtr = std::make_unique(data); for (auto tuple : iter->second) { auto actor = std::get<2>(tuple); actor.invoke(&CustomGeometryTile::setTileData, *dataPtr); @@ -68,6 +71,7 @@ void CustomTileLoader::setTileData(const CanonicalTileID& tileID, const GeoJSON& } void CustomTileLoader::invalidateTile(const CanonicalTileID& tileID) { + std::lock_guard guard(dataMutex); auto tileCallbacks = tileCallbackMap.find(tileID); if (tileCallbacks == tileCallbackMap.end()) { return; } for (auto& iter : tileCallbacks->second) { @@ -80,8 +84,8 @@ void CustomTileLoader::invalidateTile(const CanonicalTileID& tileID) { } void CustomTileLoader::invalidateRegion(const LatLngBounds& bounds, Range ) { + std::lock_guard guard(dataMutex); std::map tileRanges; - for (auto& idtuple : tileCallbackMap) { auto zoom = idtuple.first.z; auto tileRange = tileRanges.find(zoom); diff --git a/src/mbgl/style/custom_tile_loader.hpp b/src/mbgl/style/custom_tile_loader.hpp index 335d8c6143..7d2d5cffe6 100644 --- a/src/mbgl/style/custom_tile_loader.hpp +++ b/src/mbgl/style/custom_tile_loader.hpp @@ -6,6 +6,7 @@ #include #include +#include namespace mbgl { @@ -38,7 +39,7 @@ private: std::unordered_map> tileCallbackMap; // Keep around a cache of tile data to serve back for wrapped and over-zooomed tiles std::map> dataCache; - + std::mutex dataMutex; }; } // namespace style -- cgit v1.2.1