1
0
Fork 0
mirror of https://github.com/OpenMW/openmw.git synced 2025-01-19 19:53:53 +00:00

Increase mutex scope in TileCachedRecastMeshManager functions

Which are called from the main thread.
This commit is contained in:
elsid 2022-08-06 18:22:32 +02:00
parent e1bed86d7e
commit 2885885331
No known key found for this signature in database
GPG key ID: 4DE04C198CBA7625
2 changed files with 49 additions and 43 deletions

View file

@ -149,15 +149,15 @@ namespace DetourNavigator
else else
{ {
const btVector3 shift = Misc::Convert::toBullet(getWaterShift3d(cellPosition, cellSize, level)); const btVector3 shift = Misc::Convert::toBullet(getWaterShift3d(cellPosition, cellSize, level));
const auto worldspaceTiles = mWorldspaceTiles.lock();
getTilesPositions(makeTilesPositionsRange(cellSize, shift, mSettings), getTilesPositions(makeTilesPositionsRange(cellSize, shift, mSettings),
[&] (const TilePosition& tilePosition) [&] (const TilePosition& tilePosition)
{ {
const auto locked = mWorldspaceTiles.lock(); auto tile = worldspaceTiles->mTiles.find(tilePosition);
auto tile = locked->mTiles.find(tilePosition); if (tile == worldspaceTiles->mTiles.end())
if (tile == locked->mTiles.end())
{ {
const TileBounds tileBounds = makeRealTileBoundsWithBorder(mSettings, tilePosition); const TileBounds tileBounds = makeRealTileBoundsWithBorder(mSettings, tilePosition);
tile = locked->mTiles.emplace_hint(tile, tilePosition, tile = worldspaceTiles->mTiles.emplace_hint(tile, tilePosition,
std::make_shared<CachedRecastMeshManager>(tileBounds, mTilesGeneration)); std::make_shared<CachedRecastMeshManager>(tileBounds, mTilesGeneration));
} }
if (tile->second->addWater(cellPosition, cellSize, level)) if (tile->second->addWater(cellPosition, cellSize, level))
@ -180,20 +180,22 @@ namespace DetourNavigator
if (object == mWaterTilesPositions.end()) if (object == mWaterTilesPositions.end())
return std::nullopt; return std::nullopt;
std::optional<Water> result; std::optional<Water> result;
for (const auto& tilePosition : object->second)
{ {
const auto locked = mWorldspaceTiles.lock(); const auto worldspaceTiles = mWorldspaceTiles.lock();
const auto tile = locked->mTiles.find(tilePosition); for (const auto& tilePosition : object->second)
if (tile == locked->mTiles.end())
continue;
const auto tileResult = tile->second->removeWater(cellPosition);
if (tile->second->isEmpty())
{ {
locked->mTiles.erase(tile); const auto tile = worldspaceTiles->mTiles.find(tilePosition);
++mTilesGeneration; if (tile == worldspaceTiles->mTiles.end())
continue;
const auto tileResult = tile->second->removeWater(cellPosition);
if (tile->second->isEmpty())
{
worldspaceTiles->mTiles.erase(tile);
++mTilesGeneration;
}
if (tileResult && !result)
result = tileResult;
} }
if (tileResult && !result)
result = tileResult;
} }
mWaterTilesPositions.erase(object); mWaterTilesPositions.erase(object);
if (result) if (result)
@ -214,23 +216,25 @@ namespace DetourNavigator
bool result = false; bool result = false;
getTilesPositions(makeTilesPositionsRange(cellSize, shift, mSettings), {
[&] (const TilePosition& tilePosition) const auto worldspaceTiles = mWorldspaceTiles.lock();
{ getTilesPositions(makeTilesPositionsRange(cellSize, shift, mSettings),
const auto locked = mWorldspaceTiles.lock(); [&] (const TilePosition& tilePosition)
auto tile = locked->mTiles.find(tilePosition);
if (tile == locked->mTiles.end())
{ {
const TileBounds tileBounds = makeRealTileBoundsWithBorder(mSettings, tilePosition); auto tile = worldspaceTiles->mTiles.find(tilePosition);
tile = locked->mTiles.emplace_hint(tile, tilePosition, if (tile == worldspaceTiles->mTiles.end())
std::make_shared<CachedRecastMeshManager>(tileBounds, mTilesGeneration)); {
} const TileBounds tileBounds = makeRealTileBoundsWithBorder(mSettings, tilePosition);
if (tile->second->addHeightfield(cellPosition, cellSize, shape)) tile = worldspaceTiles->mTiles.emplace_hint(tile, tilePosition,
{ std::make_shared<CachedRecastMeshManager>(tileBounds, mTilesGeneration));
tilesPositions.push_back(tilePosition); }
result = true; if (tile->second->addHeightfield(cellPosition, cellSize, shape))
} {
}); tilesPositions.push_back(tilePosition);
result = true;
}
});
}
if (result) if (result)
++mRevision; ++mRevision;
@ -244,20 +248,22 @@ namespace DetourNavigator
if (object == mHeightfieldTilesPositions.end()) if (object == mHeightfieldTilesPositions.end())
return std::nullopt; return std::nullopt;
std::optional<SizedHeightfieldShape> result; std::optional<SizedHeightfieldShape> result;
for (const auto& tilePosition : object->second)
{ {
const auto locked = mWorldspaceTiles.lock(); const auto worldspaceTiles = mWorldspaceTiles.lock();
const auto tile = locked->mTiles.find(tilePosition); for (const auto& tilePosition : object->second)
if (tile == locked->mTiles.end())
continue;
const auto tileResult = tile->second->removeHeightfield(cellPosition);
if (tile->second->isEmpty())
{ {
locked->mTiles.erase(tile); const auto tile = worldspaceTiles->mTiles.find(tilePosition);
++mTilesGeneration; if (tile == worldspaceTiles->mTiles.end())
continue;
const auto tileResult = tile->second->removeHeightfield(cellPosition);
if (tile->second->isEmpty())
{
worldspaceTiles->mTiles.erase(tile);
++mTilesGeneration;
}
if (tileResult && !result)
result = tileResult;
} }
if (tileResult && !result)
result = tileResult;
} }
mHeightfieldTilesPositions.erase(object); mHeightfieldTilesPositions.erase(object);
if (result) if (result)

View file

@ -165,7 +165,7 @@ namespace DetourNavigator
bool addTile(const ObjectId id, const CollisionShape& shape, const btTransform& transform, bool addTile(const ObjectId id, const CollisionShape& shape, const btTransform& transform,
const AreaType areaType, const TilePosition& tilePosition, TilesMap& tiles); const AreaType areaType, const TilePosition& tilePosition, TilesMap& tiles);
bool updateTile(const ObjectId id, const btTransform& transform, const AreaType areaType, static bool updateTile(const ObjectId id, const btTransform& transform, const AreaType areaType,
const TilePosition& tilePosition, TilesMap& tiles); const TilePosition& tilePosition, TilesMap& tiles);
std::optional<RemovedRecastMeshObject> removeTile(const ObjectId id, const TilePosition& tilePosition, std::optional<RemovedRecastMeshObject> removeTile(const ObjectId id, const TilePosition& tilePosition,