This repository has been archived by the owner on Aug 8, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[core] Add a custom deleter for the TileData
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
Showing
4 changed files
with
74 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |