Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

improve: safety when getting a tile #1781

Merged
merged 3 commits into from
Nov 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/map/mapcache.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,8 @@ std::shared_ptr<Tile> MapCache::getOrCreateTileFromCache(const std::unique_ptr<F
return floor->getTile(x, y);
}

std::unique_lock l(floor->getMutex());

const uint8_t z = floor->getZ();

auto map = static_cast<Map*>(this);
Expand Down
7 changes: 7 additions & 0 deletions src/map/mapcache.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ struct Floor {
z(z) {};

std::shared_ptr<Tile> getTile(uint16_t x, uint16_t y) const {
std::shared_lock sl(mutex);
return tiles[x & FLOOR_MASK][y & FLOOR_MASK].first;
}

Expand All @@ -94,6 +95,7 @@ struct Floor {
}

std::shared_ptr<BasicTile> getTileCache(uint16_t x, uint16_t y) const {
std::shared_lock sl(mutex);
return tiles[x & FLOOR_MASK][y & FLOOR_MASK].second;
}

Expand All @@ -105,8 +107,13 @@ struct Floor {
return z;
}

auto &getMutex() const {
return mutex;
}

private:
std::pair<std::shared_ptr<Tile>, std::shared_ptr<BasicTile>> tiles[FLOOR_SIZE][FLOOR_SIZE] = {};
mutable std::shared_mutex mutex;
uint8_t z { 0 };
};

Expand Down
Loading