Core/Instances: Sprinkle some thread safety on InstanceLockMgr

Port From (https://github.com/TrinityCore/TrinityCore/commit/0be72f68c33617d78a4139f3e667b03d0a22a435)
This commit is contained in:
hondacrx
2022-10-04 16:57:02 -04:00
parent 2b09e4227b
commit 8d2770081b
@@ -36,6 +36,7 @@ namespace Game.Maps
public class InstanceLockManager : Singleton<InstanceLockManager> public class InstanceLockManager : Singleton<InstanceLockManager>
{ {
object _lockObject = new();
Dictionary<ObjectGuid, Dictionary<InstanceLockKey, InstanceLock>> _temporaryInstanceLocksByPlayer = new(); // locks stored here before any boss gets killed Dictionary<ObjectGuid, Dictionary<InstanceLockKey, InstanceLock>> _temporaryInstanceLocksByPlayer = new(); // locks stored here before any boss gets killed
Dictionary<ObjectGuid, Dictionary<InstanceLockKey, InstanceLock>> _instanceLocksByPlayer = new(); Dictionary<ObjectGuid, Dictionary<InstanceLockKey, InstanceLock>> _instanceLocksByPlayer = new();
Dictionary<uint, SharedInstanceLockData> _instanceLockDataById = new(); Dictionary<uint, SharedInstanceLockData> _instanceLockDataById = new();
@@ -150,6 +151,7 @@ namespace Game.Maps
public InstanceLock FindActiveInstanceLock(ObjectGuid playerGuid, MapDb2Entries entries) public InstanceLock FindActiveInstanceLock(ObjectGuid playerGuid, MapDb2Entries entries)
{ {
lock(_lockObject)
return FindActiveInstanceLock(playerGuid, entries, false, true); return FindActiveInstanceLock(playerGuid, entries, false, true);
} }
@@ -199,6 +201,8 @@ namespace Game.Maps
{ {
InstanceLock instanceLock = FindActiveInstanceLock(playerGuid, entries, true, true); InstanceLock instanceLock = FindActiveInstanceLock(playerGuid, entries, true, true);
if (instanceLock == null) if (instanceLock == null)
{
lock (_lockObject)
{ {
// Move lock from temporary storage if it exists there // Move lock from temporary storage if it exists there
// This is to avoid destroying expired locks before any boss is killed in a fresh lock // This is to avoid destroying expired locks before any boss is killed in a fresh lock
@@ -221,6 +225,7 @@ namespace Game.Maps
} }
} }
} }
}
if (instanceLock == null) if (instanceLock == null)
{ {
@@ -237,7 +242,9 @@ namespace Game.Maps
instanceLock = new InstanceLock(entries.MapDifficulty.MapID, (Difficulty)entries.MapDifficulty.DifficultyID, instanceLock = new InstanceLock(entries.MapDifficulty.MapID, (Difficulty)entries.MapDifficulty.DifficultyID,
GetNextResetTime(entries), updateEvent.InstanceId); GetNextResetTime(entries), updateEvent.InstanceId);
lock(_lockObject)
_instanceLocksByPlayer[playerGuid][entries.GetKey()] = instanceLock; _instanceLocksByPlayer[playerGuid][entries.GetKey()] = instanceLock;
Log.outDebug(LogFilter.Instance, $"[{entries.Map.Id}-{entries.Map.MapName[Global.WorldMgr.GetDefaultDbcLocale()]} | " + Log.outDebug(LogFilter.Instance, $"[{entries.Map.Id}-{entries.Map.MapName[Global.WorldMgr.GetDefaultDbcLocale()]} | " +
$"{entries.MapDifficulty.DifficultyID}-{CliDB.DifficultyStorage.LookupByKey(entries.MapDifficulty.DifficultyID).Name}] Created new instance lock for {playerGuid} in instance {updateEvent.InstanceId}"); $"{entries.MapDifficulty.DifficultyID}-{CliDB.DifficultyStorage.LookupByKey(entries.MapDifficulty.DifficultyID).Name}] Created new instance lock for {playerGuid} in instance {updateEvent.InstanceId}");
} }