Scope the Tile worker thread to an Environment

We are also removing the code that is passing the Environment around
and instead, we get now the Environment in the TileData using
Environment::Get().

When processing the job, the work will be temporally registered to
the Environment as "TileWorker_0/0/0" which can be used for logging
in the future. At the end of the job, it gets unregistered
automatically from the Environment.
This commit is contained in:
Thiago Marcos P. Santos
2015-03-13 19:04:55 +02:00
committed by Konstantin Käfer
parent b5facd35bc
commit aa9adbb8f8
14 changed files with 43 additions and 48 deletions

View File

@@ -2,7 +2,7 @@ Standard: Cpp11
IndentWidth: 4
AccessModifierOffset: -4
UseTab: Never
BinPackParameters: true
BinPackParameters: false
AllowShortIfStatementsOnASingleLine: false
AllowShortLoopsOnASingleLine: false
AllowShortBlocksOnASingleLine: false
@@ -10,7 +10,7 @@ AllowShortFunctionsOnASingleLine: false
ConstructorInitializerAllOnOneLineOrOnePerLine: true
AlwaysBreakTemplateDeclarations: true
NamespaceIndentation: None
PointerBindsToType: false
PointerBindsToType: true
SpacesInParentheses: false
BreakBeforeBraces: Attach
ColumnLimit: 100

View File

@@ -35,7 +35,7 @@ public:
// FIXME: We should never need to overwrite a thread here and we only allow
// this today because on the Static mode, the Map thread and the Main thread
// are same. Replace this with emplace() when this gets fixed.
threadSet[std::this_thread::get_id()] = ThreadInfo{env, type, name};
threadSet[std::this_thread::get_id()] = ThreadInfo{ env, type, name };
}
void unregisterThread() {
@@ -80,7 +80,7 @@ Environment::Scope::~Scope() {
threadInfoStore.unregisterThread();
}
Environment::Environment(FileSource &fs) : fileSource(fs), loop(uv_loop_new()) {
Environment::Environment(FileSource& fs) : fileSource(fs), loop(uv_loop_new()) {
}
Environment& Environment::Get() {
@@ -102,16 +102,18 @@ std::string Environment::threadName() {
return threadInfoStore.getThreadInfo().name;
}
void Environment::requestAsync(const Resource &resource, std::function<void(const Response &)> callback) {
void Environment::requestAsync(const Resource& resource,
std::function<void(const Response&)> callback) {
fileSource.request(resource, *this, std::move(callback));
}
Request *Environment::request(const Resource &resource, std::function<void(const Response &)> callback) {
Request* Environment::request(const Resource& resource,
std::function<void(const Response&)> callback) {
assert(currentlyOn(ThreadType::Map));
return fileSource.request(resource, loop, *this, std::move(callback));
}
void Environment::cancelRequest(Request *req) {
void Environment::cancelRequest(Request* req) {
assert(currentlyOn(ThreadType::Map));
fileSource.cancel(req);
}

View File

@@ -20,6 +20,7 @@ enum class ThreadType : uint8_t {
Unknown = 0,
Main = 1 << 0,
Map = 1 << 1,
TileWorker = 1 << 2,
};
class Environment final : private util::noncopyable {
@@ -33,25 +34,25 @@ public:
std::thread::id id;
};
Environment(FileSource &);
Environment(FileSource&);
static Environment& Get();
static bool inScope();
static bool currentlyOn(ThreadType);
static std::string threadName();
void requestAsync(const Resource &, std::function<void(const Response &)>);
Request *request(const Resource &, std::function<void(const Response &)>);
void cancelRequest(Request *);
void requestAsync(const Resource&, std::function<void(const Response&)>);
Request* request(const Resource&, std::function<void(const Response&)>);
void cancelRequest(Request*);
// Request to terminate the environment.
void terminate();
private:
FileSource &fileSource;
FileSource& fileSource;
public:
uv_loop_t *const loop;
uv_loop_t* const loop;
};
}

View File

@@ -15,10 +15,9 @@ LiveTileData::LiveTileData(Tile::ID const& id_,
GlyphStore& glyphStore_,
SpriteAtlas& spriteAtlas_,
util::ptr<Sprite> sprite_,
const SourceInfo& source_,
Environment& env_)
const SourceInfo& source_)
: VectorTileData::VectorTileData(id_, mapMaxZoom, style_, glyphAtlas_, glyphStore_,
spriteAtlas_, sprite_, source_, env_),
spriteAtlas_, sprite_, source_),
annotationManager(annotationManager_) {
// live features are always ready
state = State::loaded;

View File

@@ -17,8 +17,7 @@ public:
GlyphStore&,
SpriteAtlas&,
util::ptr<Sprite>,
const SourceInfo&,
Environment&);
const SourceInfo&);
~LiveTileData();
void parse() override;

View File

@@ -694,7 +694,7 @@ void Map::updateSources(const util::ptr<StyleLayerGroup> &group) {
void Map::updateTiles() {
assert(Environment::currentlyOn(ThreadType::Map));
for (const auto &source : activeSources) {
source->source->update(*this, *env, getWorker(), style, *glyphAtlas, *glyphStore,
source->source->update(*this, getWorker(), style, *glyphAtlas, *glyphStore,
*spriteAtlas, getSprite(), *texturePool, [this]() {
assert(Environment::currentlyOn(ThreadType::Map));
triggerUpdate();

View File

@@ -5,8 +5,8 @@
using namespace mbgl;
RasterTileData::RasterTileData(Tile::ID const &id_, TexturePool &texturePool,
const SourceInfo &source_, Environment &env_)
: TileData(id_, source_, env_), bucket(texturePool, layout) {
const SourceInfo &source_)
: TileData(id_, source_), bucket(texturePool, layout) {
}
RasterTileData::~RasterTileData() {

View File

@@ -17,7 +17,7 @@ class RasterTileData : public TileData {
friend class TileParser;
public:
RasterTileData(Tile::ID const &id, TexturePool &, const SourceInfo &, Environment &);
RasterTileData(Tile::ID const &id, TexturePool &, const SourceInfo &);
~RasterTileData();
void parse() override;

View File

@@ -155,7 +155,7 @@ TileData::State Source::hasTile(const Tile::ID& id) {
return TileData::State::invalid;
}
TileData::State Source::addTile(Map &map, Environment &env, uv::worker &worker,
TileData::State Source::addTile(Map &map, uv::worker &worker,
util::ptr<Style> style, GlyphAtlas &glyphAtlas,
GlyphStore &glyphStore, SpriteAtlas &spriteAtlas,
util::ptr<Sprite> sprite, TexturePool &texturePool,
@@ -187,22 +187,18 @@ TileData::State Source::addTile(Map &map, Environment &env, uv::worker &worker,
if (!new_tile.data) {
// If we don't find working tile data, we're just going to load it.
if (info.type == SourceType::Vector) {
new_tile.data = std::make_shared<VectorTileData>(normalized_id, map.getMaxZoom(), style,
glyphAtlas, glyphStore,
spriteAtlas, sprite,
info, env);
new_tile.data =
std::make_shared<VectorTileData>(normalized_id, map.getMaxZoom(), style, glyphAtlas,
glyphStore, spriteAtlas, sprite, info);
new_tile.data->request(worker, map.getState().getPixelRatio(), callback);
} else if (info.type == SourceType::Raster) {
new_tile.data = std::make_shared<RasterTileData>(normalized_id, texturePool, info, env);
new_tile.data = std::make_shared<RasterTileData>(normalized_id, texturePool, info);
new_tile.data->request(worker, map.getState().getPixelRatio(), callback);
} else if (info.type == SourceType::Annotations) {
AnnotationManager& annotationManager = map.getAnnotationManager();
new_tile.data = std::make_shared<LiveTileData>(normalized_id,
annotationManager,
map.getMaxZoom(), style,
glyphAtlas, glyphStore,
spriteAtlas, sprite,
info, env);
new_tile.data = std::make_shared<LiveTileData>(normalized_id, annotationManager,
map.getMaxZoom(), style, glyphAtlas,
glyphStore, spriteAtlas, sprite, info);
new_tile.data->reparse(worker, callback);
} else {
throw std::runtime_error("source type not implemented");
@@ -293,7 +289,6 @@ bool Source::findLoadedParent(const Tile::ID& id, int32_t minCoveringZoom, std::
}
void Source::update(Map &map,
Environment &env,
uv::worker &worker,
util::ptr<Style> style,
GlyphAtlas &glyphAtlas,
@@ -322,7 +317,7 @@ void Source::update(Map &map,
// Add existing child/parent tiles if the actual tile is not yet loaded
for (const Tile::ID& id : required) {
const TileData::State state = addTile(map, env, worker, style, glyphAtlas, glyphStore,
const TileData::State state = addTile(map, worker, style, glyphAtlas, glyphStore,
spriteAtlas, sprite, texturePool, id, callback);
if (state != TileData::State::parsed) {

View File

@@ -35,7 +35,7 @@ public:
Source(SourceInfo&);
void load(Map &, Environment &);
void update(Map &, Environment &, uv::worker &, util::ptr<Style>, GlyphAtlas &, GlyphStore &,
void update(Map &, uv::worker &, util::ptr<Style>, GlyphAtlas &, GlyphStore &,
SpriteAtlas &, util::ptr<Sprite>, TexturePool &, std::function<void()> callback);
void invalidateTiles(Map&, std::vector<Tile::ID>&);
@@ -56,7 +56,7 @@ private:
int32_t coveringZoomLevel(const TransformState&) const;
std::forward_list<Tile::ID> coveringTiles(const TransformState&) const;
TileData::State addTile(Map &, Environment &, uv::worker &, util::ptr<Style>, GlyphAtlas &,
TileData::State addTile(Map &, uv::worker &, util::ptr<Style>, GlyphAtlas &,
GlyphStore &, SpriteAtlas &, util::ptr<Sprite>, TexturePool &,
const Tile::ID &, std::function<void()> callback);

View File

@@ -12,12 +12,12 @@
using namespace mbgl;
TileData::TileData(Tile::ID const& id_, const SourceInfo& source_, Environment& env_)
TileData::TileData(Tile::ID const& id_, const SourceInfo& source_)
: id(id_),
name(id),
state(State::initial),
source(source_),
env(env_),
env(Environment::Get()),
debugBucket(debugFontBuffer) {
// Initialize tile debug coordinates
debugFontBuffer.addText(name.c_str(), 50, 200, 5);
@@ -96,7 +96,8 @@ void TileData::reparse(uv::worker& worker, std::function<void()> callback)
// the after work handler
new uv::work<util::ptr<TileData>>(
worker,
[](util::ptr<TileData>& tile) {
[this](util::ptr<TileData>& tile) {
Environment::Scope scope(env, ThreadType::TileWorker, "TileWorker_" + tile->name);
tile->parse();
},
[callback](util::ptr<TileData>&) {

View File

@@ -48,7 +48,7 @@ public:
};
public:
TileData(Tile::ID const &id, const SourceInfo &, Environment &);
TileData(Tile::ID const &id, const SourceInfo &);
~TileData();
void request(uv::worker&, float pixelRatio, std::function<void ()> callback);
@@ -72,7 +72,7 @@ public:
public:
const SourceInfo& source;
Environment &env;
Environment& env;
protected:
Request *req = nullptr;

View File

@@ -17,9 +17,8 @@ VectorTileData::VectorTileData(Tile::ID const& id_,
GlyphStore& glyphStore_,
SpriteAtlas& spriteAtlas_,
util::ptr<Sprite> sprite_,
const SourceInfo& source_,
Environment& env_)
: TileData(id_, source_, env_),
const SourceInfo& source_)
: TileData(id_, source_),
glyphAtlas(glyphAtlas_),
glyphStore(glyphStore_),
spriteAtlas(spriteAtlas_),

View File

@@ -37,8 +37,7 @@ public:
GlyphStore&,
SpriteAtlas&,
util::ptr<Sprite>,
const SourceInfo&,
Environment&);
const SourceInfo&);
~VectorTileData();
void parse() override;