1
0
Fork 0
mirror of https://github.com/OpenMW/openmw.git synced 2025-01-19 22: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,21 +180,23 @@ namespace DetourNavigator
if (object == mWaterTilesPositions.end()) if (object == mWaterTilesPositions.end())
return std::nullopt; return std::nullopt;
std::optional<Water> result; std::optional<Water> result;
{
const auto worldspaceTiles = mWorldspaceTiles.lock();
for (const auto& tilePosition : object->second) for (const auto& tilePosition : object->second)
{ {
const auto locked = mWorldspaceTiles.lock(); const auto tile = worldspaceTiles->mTiles.find(tilePosition);
const auto tile = locked->mTiles.find(tilePosition); if (tile == worldspaceTiles->mTiles.end())
if (tile == locked->mTiles.end())
continue; continue;
const auto tileResult = tile->second->removeWater(cellPosition); const auto tileResult = tile->second->removeWater(cellPosition);
if (tile->second->isEmpty()) if (tile->second->isEmpty())
{ {
locked->mTiles.erase(tile); worldspaceTiles->mTiles.erase(tile);
++mTilesGeneration; ++mTilesGeneration;
} }
if (tileResult && !result) if (tileResult && !result)
result = tileResult; result = tileResult;
} }
}
mWaterTilesPositions.erase(object); mWaterTilesPositions.erase(object);
if (result) if (result)
++mRevision; ++mRevision;
@ -214,15 +216,16 @@ namespace DetourNavigator
bool result = false; bool result = false;
{
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->addHeightfield(cellPosition, cellSize, shape)) if (tile->second->addHeightfield(cellPosition, cellSize, shape))
@ -231,6 +234,7 @@ namespace DetourNavigator
result = true; result = true;
} }
}); });
}
if (result) if (result)
++mRevision; ++mRevision;
@ -244,21 +248,23 @@ namespace DetourNavigator
if (object == mHeightfieldTilesPositions.end()) if (object == mHeightfieldTilesPositions.end())
return std::nullopt; return std::nullopt;
std::optional<SizedHeightfieldShape> result; std::optional<SizedHeightfieldShape> result;
{
const auto worldspaceTiles = mWorldspaceTiles.lock();
for (const auto& tilePosition : object->second) for (const auto& tilePosition : object->second)
{ {
const auto locked = mWorldspaceTiles.lock(); const auto tile = worldspaceTiles->mTiles.find(tilePosition);
const auto tile = locked->mTiles.find(tilePosition); if (tile == worldspaceTiles->mTiles.end())
if (tile == locked->mTiles.end())
continue; continue;
const auto tileResult = tile->second->removeHeightfield(cellPosition); const auto tileResult = tile->second->removeHeightfield(cellPosition);
if (tile->second->isEmpty()) if (tile->second->isEmpty())
{ {
locked->mTiles.erase(tile); worldspaceTiles->mTiles.erase(tile);
++mTilesGeneration; ++mTilesGeneration;
} }
if (tileResult && !result) if (tileResult && !result)
result = tileResult; result = tileResult;
} }
}
mHeightfieldTilesPositions.erase(object); mHeightfieldTilesPositions.erase(object);
if (result) if (result)
++mRevision; ++mRevision;

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,