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

Commit aa9adbb

Browse files
tmpsantoskkaefer
authored andcommitted
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.
1 parent b5facd3 commit aa9adbb

14 files changed

+43
-48
lines changed

.clang-format

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,15 @@ Standard: Cpp11
22
IndentWidth: 4
33
AccessModifierOffset: -4
44
UseTab: Never
5-
BinPackParameters: true
5+
BinPackParameters: false
66
AllowShortIfStatementsOnASingleLine: false
77
AllowShortLoopsOnASingleLine: false
88
AllowShortBlocksOnASingleLine: false
99
AllowShortFunctionsOnASingleLine: false
1010
ConstructorInitializerAllOnOneLineOrOnePerLine: true
1111
AlwaysBreakTemplateDeclarations: true
1212
NamespaceIndentation: None
13-
PointerBindsToType: false
13+
PointerBindsToType: true
1414
SpacesInParentheses: false
1515
BreakBeforeBraces: Attach
1616
ColumnLimit: 100

src/mbgl/map/environment.cpp

+7-5
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ class ThreadInfoStore {
3535
// FIXME: We should never need to overwrite a thread here and we only allow
3636
// this today because on the Static mode, the Map thread and the Main thread
3737
// are same. Replace this with emplace() when this gets fixed.
38-
threadSet[std::this_thread::get_id()] = ThreadInfo{env, type, name};
38+
threadSet[std::this_thread::get_id()] = ThreadInfo{ env, type, name };
3939
}
4040

4141
void unregisterThread() {
@@ -80,7 +80,7 @@ Environment::Scope::~Scope() {
8080
threadInfoStore.unregisterThread();
8181
}
8282

83-
Environment::Environment(FileSource &fs) : fileSource(fs), loop(uv_loop_new()) {
83+
Environment::Environment(FileSource& fs) : fileSource(fs), loop(uv_loop_new()) {
8484
}
8585

8686
Environment& Environment::Get() {
@@ -102,16 +102,18 @@ std::string Environment::threadName() {
102102
return threadInfoStore.getThreadInfo().name;
103103
}
104104

105-
void Environment::requestAsync(const Resource &resource, std::function<void(const Response &)> callback) {
105+
void Environment::requestAsync(const Resource& resource,
106+
std::function<void(const Response&)> callback) {
106107
fileSource.request(resource, *this, std::move(callback));
107108
}
108109

109-
Request *Environment::request(const Resource &resource, std::function<void(const Response &)> callback) {
110+
Request* Environment::request(const Resource& resource,
111+
std::function<void(const Response&)> callback) {
110112
assert(currentlyOn(ThreadType::Map));
111113
return fileSource.request(resource, loop, *this, std::move(callback));
112114
}
113115

114-
void Environment::cancelRequest(Request *req) {
116+
void Environment::cancelRequest(Request* req) {
115117
assert(currentlyOn(ThreadType::Map));
116118
fileSource.cancel(req);
117119
}

src/mbgl/map/environment.hpp

+7-6
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ enum class ThreadType : uint8_t {
2020
Unknown = 0,
2121
Main = 1 << 0,
2222
Map = 1 << 1,
23+
TileWorker = 1 << 2,
2324
};
2425

2526
class Environment final : private util::noncopyable {
@@ -33,25 +34,25 @@ class Environment final : private util::noncopyable {
3334
std::thread::id id;
3435
};
3536

36-
Environment(FileSource &);
37+
Environment(FileSource&);
3738

3839
static Environment& Get();
3940
static bool inScope();
4041
static bool currentlyOn(ThreadType);
4142
static std::string threadName();
4243

43-
void requestAsync(const Resource &, std::function<void(const Response &)>);
44-
Request *request(const Resource &, std::function<void(const Response &)>);
45-
void cancelRequest(Request *);
44+
void requestAsync(const Resource&, std::function<void(const Response&)>);
45+
Request* request(const Resource&, std::function<void(const Response&)>);
46+
void cancelRequest(Request*);
4647

4748
// Request to terminate the environment.
4849
void terminate();
4950

5051
private:
51-
FileSource &fileSource;
52+
FileSource& fileSource;
5253

5354
public:
54-
uv_loop_t *const loop;
55+
uv_loop_t* const loop;
5556
};
5657

5758
}

src/mbgl/map/live_tile_data.cpp

+2-3
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,9 @@ LiveTileData::LiveTileData(Tile::ID const& id_,
1515
GlyphStore& glyphStore_,
1616
SpriteAtlas& spriteAtlas_,
1717
util::ptr<Sprite> sprite_,
18-
const SourceInfo& source_,
19-
Environment& env_)
18+
const SourceInfo& source_)
2019
: VectorTileData::VectorTileData(id_, mapMaxZoom, style_, glyphAtlas_, glyphStore_,
21-
spriteAtlas_, sprite_, source_, env_),
20+
spriteAtlas_, sprite_, source_),
2221
annotationManager(annotationManager_) {
2322
// live features are always ready
2423
state = State::loaded;

src/mbgl/map/live_tile_data.hpp

+1-2
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,7 @@ class LiveTileData : public VectorTileData {
1717
GlyphStore&,
1818
SpriteAtlas&,
1919
util::ptr<Sprite>,
20-
const SourceInfo&,
21-
Environment&);
20+
const SourceInfo&);
2221
~LiveTileData();
2322

2423
void parse() override;

src/mbgl/map/map.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -694,7 +694,7 @@ void Map::updateSources(const util::ptr<StyleLayerGroup> &group) {
694694
void Map::updateTiles() {
695695
assert(Environment::currentlyOn(ThreadType::Map));
696696
for (const auto &source : activeSources) {
697-
source->source->update(*this, *env, getWorker(), style, *glyphAtlas, *glyphStore,
697+
source->source->update(*this, getWorker(), style, *glyphAtlas, *glyphStore,
698698
*spriteAtlas, getSprite(), *texturePool, [this]() {
699699
assert(Environment::currentlyOn(ThreadType::Map));
700700
triggerUpdate();

src/mbgl/map/raster_tile_data.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
using namespace mbgl;
66

77
RasterTileData::RasterTileData(Tile::ID const &id_, TexturePool &texturePool,
8-
const SourceInfo &source_, Environment &env_)
9-
: TileData(id_, source_, env_), bucket(texturePool, layout) {
8+
const SourceInfo &source_)
9+
: TileData(id_, source_), bucket(texturePool, layout) {
1010
}
1111

1212
RasterTileData::~RasterTileData() {

src/mbgl/map/raster_tile_data.hpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ class RasterTileData : public TileData {
1717
friend class TileParser;
1818

1919
public:
20-
RasterTileData(Tile::ID const &id, TexturePool &, const SourceInfo &, Environment &);
20+
RasterTileData(Tile::ID const &id, TexturePool &, const SourceInfo &);
2121
~RasterTileData();
2222

2323
void parse() override;

src/mbgl/map/source.cpp

+9-14
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ TileData::State Source::hasTile(const Tile::ID& id) {
155155
return TileData::State::invalid;
156156
}
157157

158-
TileData::State Source::addTile(Map &map, Environment &env, uv::worker &worker,
158+
TileData::State Source::addTile(Map &map, uv::worker &worker,
159159
util::ptr<Style> style, GlyphAtlas &glyphAtlas,
160160
GlyphStore &glyphStore, SpriteAtlas &spriteAtlas,
161161
util::ptr<Sprite> sprite, TexturePool &texturePool,
@@ -187,22 +187,18 @@ TileData::State Source::addTile(Map &map, Environment &env, uv::worker &worker,
187187
if (!new_tile.data) {
188188
// If we don't find working tile data, we're just going to load it.
189189
if (info.type == SourceType::Vector) {
190-
new_tile.data = std::make_shared<VectorTileData>(normalized_id, map.getMaxZoom(), style,
191-
glyphAtlas, glyphStore,
192-
spriteAtlas, sprite,
193-
info, env);
190+
new_tile.data =
191+
std::make_shared<VectorTileData>(normalized_id, map.getMaxZoom(), style, glyphAtlas,
192+
glyphStore, spriteAtlas, sprite, info);
194193
new_tile.data->request(worker, map.getState().getPixelRatio(), callback);
195194
} else if (info.type == SourceType::Raster) {
196-
new_tile.data = std::make_shared<RasterTileData>(normalized_id, texturePool, info, env);
195+
new_tile.data = std::make_shared<RasterTileData>(normalized_id, texturePool, info);
197196
new_tile.data->request(worker, map.getState().getPixelRatio(), callback);
198197
} else if (info.type == SourceType::Annotations) {
199198
AnnotationManager& annotationManager = map.getAnnotationManager();
200-
new_tile.data = std::make_shared<LiveTileData>(normalized_id,
201-
annotationManager,
202-
map.getMaxZoom(), style,
203-
glyphAtlas, glyphStore,
204-
spriteAtlas, sprite,
205-
info, env);
199+
new_tile.data = std::make_shared<LiveTileData>(normalized_id, annotationManager,
200+
map.getMaxZoom(), style, glyphAtlas,
201+
glyphStore, spriteAtlas, sprite, info);
206202
new_tile.data->reparse(worker, callback);
207203
} else {
208204
throw std::runtime_error("source type not implemented");
@@ -293,7 +289,6 @@ bool Source::findLoadedParent(const Tile::ID& id, int32_t minCoveringZoom, std::
293289
}
294290

295291
void Source::update(Map &map,
296-
Environment &env,
297292
uv::worker &worker,
298293
util::ptr<Style> style,
299294
GlyphAtlas &glyphAtlas,
@@ -322,7 +317,7 @@ void Source::update(Map &map,
322317

323318
// Add existing child/parent tiles if the actual tile is not yet loaded
324319
for (const Tile::ID& id : required) {
325-
const TileData::State state = addTile(map, env, worker, style, glyphAtlas, glyphStore,
320+
const TileData::State state = addTile(map, worker, style, glyphAtlas, glyphStore,
326321
spriteAtlas, sprite, texturePool, id, callback);
327322

328323
if (state != TileData::State::parsed) {

src/mbgl/map/source.hpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ class Source : public std::enable_shared_from_this<Source>, private util::noncop
3535
Source(SourceInfo&);
3636

3737
void load(Map &, Environment &);
38-
void update(Map &, Environment &, uv::worker &, util::ptr<Style>, GlyphAtlas &, GlyphStore &,
38+
void update(Map &, uv::worker &, util::ptr<Style>, GlyphAtlas &, GlyphStore &,
3939
SpriteAtlas &, util::ptr<Sprite>, TexturePool &, std::function<void()> callback);
4040
void invalidateTiles(Map&, std::vector<Tile::ID>&);
4141

@@ -56,7 +56,7 @@ class Source : public std::enable_shared_from_this<Source>, private util::noncop
5656
int32_t coveringZoomLevel(const TransformState&) const;
5757
std::forward_list<Tile::ID> coveringTiles(const TransformState&) const;
5858

59-
TileData::State addTile(Map &, Environment &, uv::worker &, util::ptr<Style>, GlyphAtlas &,
59+
TileData::State addTile(Map &, uv::worker &, util::ptr<Style>, GlyphAtlas &,
6060
GlyphStore &, SpriteAtlas &, util::ptr<Sprite>, TexturePool &,
6161
const Tile::ID &, std::function<void()> callback);
6262

src/mbgl/map/tile_data.cpp

+4-3
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,12 @@
1212

1313
using namespace mbgl;
1414

15-
TileData::TileData(Tile::ID const& id_, const SourceInfo& source_, Environment& env_)
15+
TileData::TileData(Tile::ID const& id_, const SourceInfo& source_)
1616
: id(id_),
1717
name(id),
1818
state(State::initial),
1919
source(source_),
20-
env(env_),
20+
env(Environment::Get()),
2121
debugBucket(debugFontBuffer) {
2222
// Initialize tile debug coordinates
2323
debugFontBuffer.addText(name.c_str(), 50, 200, 5);
@@ -96,7 +96,8 @@ void TileData::reparse(uv::worker& worker, std::function<void()> callback)
9696
// the after work handler
9797
new uv::work<util::ptr<TileData>>(
9898
worker,
99-
[](util::ptr<TileData>& tile) {
99+
[this](util::ptr<TileData>& tile) {
100+
Environment::Scope scope(env, ThreadType::TileWorker, "TileWorker_" + tile->name);
100101
tile->parse();
101102
},
102103
[callback](util::ptr<TileData>&) {

src/mbgl/map/tile_data.hpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ class TileData : public std::enable_shared_from_this<TileData>,
4848
};
4949

5050
public:
51-
TileData(Tile::ID const &id, const SourceInfo &, Environment &);
51+
TileData(Tile::ID const &id, const SourceInfo &);
5252
~TileData();
5353

5454
void request(uv::worker&, float pixelRatio, std::function<void ()> callback);
@@ -72,7 +72,7 @@ class TileData : public std::enable_shared_from_this<TileData>,
7272

7373
public:
7474
const SourceInfo& source;
75-
Environment &env;
75+
Environment& env;
7676

7777
protected:
7878
Request *req = nullptr;

src/mbgl/map/vector_tile_data.cpp

+2-3
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,8 @@ VectorTileData::VectorTileData(Tile::ID const& id_,
1717
GlyphStore& glyphStore_,
1818
SpriteAtlas& spriteAtlas_,
1919
util::ptr<Sprite> sprite_,
20-
const SourceInfo& source_,
21-
Environment& env_)
22-
: TileData(id_, source_, env_),
20+
const SourceInfo& source_)
21+
: TileData(id_, source_),
2322
glyphAtlas(glyphAtlas_),
2423
glyphStore(glyphStore_),
2524
spriteAtlas(spriteAtlas_),

src/mbgl/map/vector_tile_data.hpp

+1-2
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,7 @@ class VectorTileData : public TileData {
3737
GlyphStore&,
3838
SpriteAtlas&,
3939
util::ptr<Sprite>,
40-
const SourceInfo&,
41-
Environment&);
40+
const SourceInfo&);
4241
~VectorTileData();
4342

4443
void parse() override;

0 commit comments

Comments
 (0)