Skip to content
This repository has been archived by the owner on Aug 8, 2023. It is now read-only.

Commit

Permalink
[core] mutex guard annotation manager for cross thread usage
Browse files Browse the repository at this point in the history
  • Loading branch information
ivovandongen committed Jun 12, 2017
1 parent f59eb82 commit 2d525de
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 10 deletions.
37 changes: 27 additions & 10 deletions src/mbgl/annotation/annotation_manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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) {
Expand Down Expand Up @@ -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;
}
Expand All @@ -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;
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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);
}

Expand All @@ -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,
Expand All @@ -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;
Expand Down
5 changes: 5 additions & 0 deletions src/mbgl/annotation/annotation_manager.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include <mbgl/map/update.hpp>
#include <mbgl/util/noncopyable.hpp>

#include <mutex>
#include <string>
#include <vector>
#include <unordered_set>
Expand Down Expand Up @@ -56,8 +57,12 @@ class AnnotationManager : private util::noncopyable {

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>>;
Expand Down

0 comments on commit 2d525de

Please sign in to comment.