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

Commit

Permalink
[core] Add a custom deleter for the TileData
Browse files Browse the repository at this point in the history
We cannot simply delete the TileData at will because
it might block. The custom deleter makes sure that
it will only delete the object when it is non-blocking.
  • Loading branch information
tmpsantos committed Jan 28, 2016
1 parent 13e7087 commit a74e11b
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 9 deletions.
22 changes: 13 additions & 9 deletions src/mbgl/map/source.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -267,9 +267,11 @@ TileData::State Source::addTile(const TileID& tileID, const StyleUpdateParameter

// If we don't find working tile data, we're just going to load it.
if (type == SourceType::Raster) {
auto tileData = std::make_shared<RasterTileData>(normalizedID,
parameters.texturePool,
parameters.worker);
auto tileData = std::shared_ptr<RasterTileData>(new RasterTileData{
normalizedID,
parameters.texturePool,
parameters.worker
}, [this](TileData* data) { tileDataDeleter.add(data); });
tileData->request(util::templateTileURL(info->tiles.at(0), normalizedID, parameters.pixelRatio), callback);
newTile->data = tileData;
} else {
Expand All @@ -286,12 +288,14 @@ TileData::State Source::addTile(const TileID& tileID, const StyleUpdateParameter
return TileData::State::invalid;
}

newTile->data = std::make_shared<VectorTileData>(normalizedID,
std::move(monitor),
id,
parameters.style,
parameters.mode,
callback);
newTile->data = std::shared_ptr<VectorTileData>(new VectorTileData{
normalizedID,
std::move(monitor),
id,
parameters.style,
parameters.mode,
callback
}, [this](TileData* data) { tileDataDeleter.add(data); });
}

tileDataMap.emplace(newTile->data->id, newTile->data);
Expand Down
3 changes: 3 additions & 0 deletions src/mbgl/map/source.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#define MBGL_MAP_SOURCE

#include <mbgl/map/tile_cache.hpp>
#include <mbgl/map/tile_data_deleter.hpp>
#include <mbgl/map/source_info.hpp>

#include <mbgl/util/mat4.hpp>
Expand Down Expand Up @@ -111,6 +112,8 @@ class Source : private util::noncopyable {

Observer nullObserver;
Observer* observer = &nullObserver;

TileDataDeleter tileDataDeleter;
};

} // namespace mbgl
Expand Down
34 changes: 34 additions & 0 deletions src/mbgl/map/tile_data_deleter.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#include <mbgl/map/tile_data_deleter.hpp>

#include <mbgl/map/tile_data.hpp>

namespace mbgl {

TileDataDeleter::~TileDataDeleter() {
// Force the canceling of all pending TileData
// before destroying. We have assert()s on Debug
// mode on TileData to make sure it doesn't block
// at the destructor unless explicitly canceled.
for (auto& entry : tileDataMap) {
entry.first->tryCancel(true);
}
}

void TileDataDeleter::add(TileData* data) {
std::unique_ptr<TileData> tileData(data);

// This custom deleter will try to cancel the task if it is
// non-blocking, otherwise we keep it and subscribe to the
// event when the work is completed. When the event triggers
// we delete the TileData because this time it won't block.
if (!tileData->tryCancel()) {
data->setObserver(this);
tileDataMap.emplace(data, std::move(tileData));
}
}

void TileDataDeleter::onTileDataWorkCompleted(TileData* data) {
tileDataMap.erase(data);
}

} // namespace mbgl
29 changes: 29 additions & 0 deletions src/mbgl/map/tile_data_deleter.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#ifndef MBGL_MAP_TILE_DATA_DELETER
#define MBGL_MAP_TILE_DATA_DELETER

#include <mbgl/map/tile_data.hpp>
#include <mbgl/util/noncopyable.hpp>

#include <memory>
#include <unordered_map>

namespace mbgl {

class TileData;

class TileDataDeleter : public TileData::Observer, private util::noncopyable {
public:
virtual ~TileDataDeleter();

void add(TileData*);

// TileData::Observer implementation.
void onTileDataWorkCompleted(TileData*) override;

private:
std::unordered_map<TileData*, std::unique_ptr<TileData>> tileDataMap;
};

} // namespace mbgl

#endif

0 comments on commit a74e11b

Please sign in to comment.