summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorIvo van Dongen <info@ivovandongen.nl>2017-06-08 12:53:14 +0300
committerIvo van Dongen <ivovandongen@users.noreply.github.com>2017-06-13 10:15:53 +0300
commit672ba516f225b231077baa0e3f6ae0749ee9c295 (patch)
tree08d3d8ae7c9ec2f3d22f6d46ab1d1cfcd80b5be1
parentf1598ff07e85608194fecffa7857acead5530371 (diff)
downloadqtlocation-mapboxgl-672ba516f225b231077baa0e3f6ae0749ee9c295.tar.gz
[core] mutex guard annotation manager for cross thread usage
-rw-r--r--src/mbgl/annotation/annotation_manager.cpp37
-rw-r--r--src/mbgl/annotation/annotation_manager.hpp5
2 files changed, 32 insertions, 10 deletions
diff --git a/src/mbgl/annotation/annotation_manager.cpp b/src/mbgl/annotation/annotation_manager.cpp
index f40b9176fb..05b1b13d2d 100644
--- a/src/mbgl/annotation/annotation_manager.cpp
+++ b/src/mbgl/annotation/annotation_manager.cpp
@@ -22,6 +22,7 @@ AnnotationManager::AnnotationManager() = default;
AnnotationManager::~AnnotationManager() = default;
AnnotationID AnnotationManager::addAnnotation(const Annotation& annotation, const uint8_t maxZoom) {
+ std::lock_guard<std::mutex> lock(mutex);
AnnotationID id = nextID++;
Annotation::visit(annotation, [&] (const auto& annotation_) {
this->add(id, annotation_, maxZoom);
@@ -30,21 +31,15 @@ AnnotationID AnnotationManager::addAnnotation(const Annotation& annotation, cons
}
Update AnnotationManager::updateAnnotation(const AnnotationID& id, const Annotation& annotation, const uint8_t maxZoom) {
+ std::lock_guard<std::mutex> lock(mutex);
return Annotation::visit(annotation, [&] (const auto& annotation_) {
return this->update(id, annotation_, maxZoom);
});
}
void AnnotationManager::removeAnnotation(const AnnotationID& id) {
- if (symbolAnnotations.find(id) != symbolAnnotations.end()) {
- symbolTree.remove(symbolAnnotations.at(id));
- symbolAnnotations.erase(id);
- } else if (shapeAnnotations.find(id) != shapeAnnotations.end()) {
- obsoleteShapeAnnotationLayers.insert(shapeAnnotations.at(id)->layerID);
- shapeAnnotations.erase(id);
- } else {
- assert(false); // Should never happen
- }
+ std::lock_guard<std::mutex> lock(mutex);
+ remove(id);
}
void AnnotationManager::add(const AnnotationID& id, const SymbolAnnotation& annotation, const uint8_t) {
@@ -97,6 +92,7 @@ Update AnnotationManager::update(const AnnotationID& id, const LineAnnotation& a
assert(false); // Attempt to update a non-existent shape annotation
return Update::Nothing;
}
+
removeAndAdd(id, annotation, maxZoom);
return Update::AnnotationData | Update::AnnotationStyle;
}
@@ -107,17 +103,30 @@ Update AnnotationManager::update(const AnnotationID& id, const FillAnnotation& a
assert(false); // Attempt to update a non-existent shape annotation
return Update::Nothing;
}
+
removeAndAdd(id, annotation, maxZoom);
return Update::AnnotationData | Update::AnnotationStyle;
}
void AnnotationManager::removeAndAdd(const AnnotationID& id, const Annotation& annotation, const uint8_t maxZoom) {
- removeAnnotation(id);
+ remove(id);
Annotation::visit(annotation, [&] (const auto& annotation_) {
this->add(id, annotation_, maxZoom);
});
}
+void AnnotationManager::remove(const AnnotationID& id) {
+ if (symbolAnnotations.find(id) != symbolAnnotations.end()) {
+ symbolTree.remove(symbolAnnotations.at(id));
+ symbolAnnotations.erase(id);
+ } else if (shapeAnnotations.find(id) != shapeAnnotations.end()) {
+ obsoleteShapeAnnotationLayers.insert(shapeAnnotations.at(id)->layerID);
+ shapeAnnotations.erase(id);
+ } else {
+ assert(false); // Should never happen
+ }
+}
+
std::unique_ptr<AnnotationTileData> AnnotationManager::getTileData(const CanonicalTileID& tileID) {
if (symbolAnnotations.empty() && shapeAnnotations.empty())
return nullptr;
@@ -155,6 +164,8 @@ void AnnotationManager::updateStyle(Style& style) {
style.addLayer(std::move(layer));
}
+ std::lock_guard<std::mutex> lock(mutex);
+
for (const auto& shape : shapeAnnotations) {
shape.second->updateStyle(style);
}
@@ -186,17 +197,20 @@ void AnnotationManager::updateStyle(Style& style) {
}
void AnnotationManager::updateData() {
+ std::lock_guard<std::mutex> lock(mutex);
for (auto& tile : tiles) {
tile->setData(getTileData(tile->id.canonical));
}
}
void AnnotationManager::addTile(AnnotationTile& tile) {
+ std::lock_guard<std::mutex> lock(mutex);
tiles.insert(&tile);
tile.setData(getTileData(tile.id.canonical));
}
void AnnotationManager::removeTile(AnnotationTile& tile) {
+ std::lock_guard<std::mutex> lock(mutex);
tiles.erase(&tile);
}
@@ -207,6 +221,7 @@ static std::string prefixedImageID(const std::string& id) {
}
void AnnotationManager::addImage(std::unique_ptr<style::Image> image) {
+ std::lock_guard<std::mutex> lock(mutex);
const std::string id = prefixedImageID(image->getID());
images.erase(id);
images.emplace(id,
@@ -215,12 +230,14 @@ void AnnotationManager::addImage(std::unique_ptr<style::Image> image) {
}
void AnnotationManager::removeImage(const std::string& id_) {
+ std::lock_guard<std::mutex> lock(mutex);
const std::string id = prefixedImageID(id_);
images.erase(id);
obsoleteImages.insert(id);
}
double AnnotationManager::getTopOffsetPixelsForImage(const std::string& id_) {
+ std::lock_guard<std::mutex> lock(mutex);
const std::string id = prefixedImageID(id_);
auto it = images.find(id);
return it != images.end() ? -(it->second.getImage().size.height / it->second.getPixelRatio()) / 2 : 0;
diff --git a/src/mbgl/annotation/annotation_manager.hpp b/src/mbgl/annotation/annotation_manager.hpp
index 36f18e1aae..c2ed5ba89f 100644
--- a/src/mbgl/annotation/annotation_manager.hpp
+++ b/src/mbgl/annotation/annotation_manager.hpp
@@ -6,6 +6,7 @@
#include <mbgl/map/update.hpp>
#include <mbgl/util/noncopyable.hpp>
+#include <mutex>
#include <string>
#include <vector>
#include <unordered_set>
@@ -56,8 +57,12 @@ private:
void removeAndAdd(const AnnotationID&, const Annotation&, const uint8_t);
+ void remove(const AnnotationID&);
+
std::unique_ptr<AnnotationTileData> getTileData(const CanonicalTileID&);
+ std::mutex mutex;
+
AnnotationID nextID = 0;
using SymbolAnnotationTree = boost::geometry::index::rtree<std::shared_ptr<const SymbolAnnotationImpl>, boost::geometry::index::rstar<16, 4>>;