From 6b48dfa463afb7f07a2fa3b2538fe860d5e03e8f Mon Sep 17 00:00:00 2001 From: Chomp Date: Sat, 8 Mar 2025 09:55:36 +0000 Subject: [PATCH] Made locks more consistent across solution --- .../SPTarkov.Common/Extensions/ObjectExtensions.cs | 2 +- .../SPTarkov.Common/Extensions/StringExtensions.cs | 2 +- .../Context/ApplicationContext.cs | 10 +++++----- .../Servers/Ws/SptWebSocketConnectionHandler.cs | 2 +- .../Services/BotEquipmentModPoolService.cs | 8 ++++---- .../Services/BotLootCacheService.cs | 14 +++++++------- .../Services/BotNameService.cs | 6 +++--- .../SPTarkov.Server.Core/Utils/ImporterUtil.cs | 6 +++--- .../Utils/RagfairOfferHolder.cs | 8 ++++---- 9 files changed, 29 insertions(+), 29 deletions(-) diff --git a/Libraries/SPTarkov.Common/Extensions/ObjectExtensions.cs b/Libraries/SPTarkov.Common/Extensions/ObjectExtensions.cs index c485155f..e4bcf04f 100644 --- a/Libraries/SPTarkov.Common/Extensions/ObjectExtensions.cs +++ b/Libraries/SPTarkov.Common/Extensions/ObjectExtensions.cs @@ -6,7 +6,7 @@ namespace SPTarkov.Common.Extensions; public static class ObjectExtensions { private static readonly Dictionary> _indexedProperties = new(); - private static readonly object _indexedPropertiesLockObject = new(); + private static readonly Lock _indexedPropertiesLockObject = new(); private static bool TryGetCachedProperty(Type type, string key, out PropertyInfo cachedProperty) { diff --git a/Libraries/SPTarkov.Common/Extensions/StringExtensions.cs b/Libraries/SPTarkov.Common/Extensions/StringExtensions.cs index 0c13f39b..4910e4a5 100644 --- a/Libraries/SPTarkov.Common/Extensions/StringExtensions.cs +++ b/Libraries/SPTarkov.Common/Extensions/StringExtensions.cs @@ -6,7 +6,7 @@ namespace SPTarkov.Common.Extensions; public static class StringExtensions { private static readonly Dictionary RegexCache = new(); - private static readonly object RegexCacheLock = new(); + private static readonly Lock RegexCacheLock = new(); public static string RegexReplace(this string source, [StringSyntax(StringSyntaxAttribute.Regex)] string regexString, string newValue) { diff --git a/Libraries/SPTarkov.Server.Core/Context/ApplicationContext.cs b/Libraries/SPTarkov.Server.Core/Context/ApplicationContext.cs index 626e7d75..c7308ba7 100644 --- a/Libraries/SPTarkov.Server.Core/Context/ApplicationContext.cs +++ b/Libraries/SPTarkov.Server.Core/Context/ApplicationContext.cs @@ -7,11 +7,11 @@ public class ApplicationContext { protected const short MaxSavedValues = 10; protected readonly Dictionary> variables = new(); - private readonly Lock variablesLock = new(); + private readonly Lock _lockObject = new(); public ContextVariable? GetLatestValue(ContextVariableType type) { - lock (variablesLock) + lock (_lockObject) { if (variables.TryGetValue(type, out var savedValues)) { @@ -24,7 +24,7 @@ public class ApplicationContext public ICollection GetValues(ContextVariableType type) { - lock (variablesLock) + lock (_lockObject) { var values = new List(); if (variables.TryGetValue(type, out var savedValues)) @@ -38,7 +38,7 @@ public class ApplicationContext public void AddValue(ContextVariableType type, object value) { - lock (variablesLock) + lock (_lockObject) { if (!variables.TryGetValue(type, out var savedValues)) { @@ -57,7 +57,7 @@ public class ApplicationContext public void ClearValues(ContextVariableType type) { - lock (variablesLock) + lock (_lockObject) { variables.Remove(type); } diff --git a/Libraries/SPTarkov.Server.Core/Servers/Ws/SptWebSocketConnectionHandler.cs b/Libraries/SPTarkov.Server.Core/Servers/Ws/SptWebSocketConnectionHandler.cs index 08a7244e..6c5bfd78 100644 --- a/Libraries/SPTarkov.Server.Core/Servers/Ws/SptWebSocketConnectionHandler.cs +++ b/Libraries/SPTarkov.Server.Core/Servers/Ws/SptWebSocketConnectionHandler.cs @@ -22,7 +22,7 @@ public class SptWebSocketConnectionHandler( ) : IWebSocketConnectionHandler { protected WsPing _defaultNotification = new(); - protected Lock _lockObject = new(); + protected readonly Lock _lockObject = new(); protected Dictionary _sockets = new(); public string GetHookUrl() diff --git a/Libraries/SPTarkov.Server.Core/Services/BotEquipmentModPoolService.cs b/Libraries/SPTarkov.Server.Core/Services/BotEquipmentModPoolService.cs index c65b429d..e09be281 100644 --- a/Libraries/SPTarkov.Server.Core/Services/BotEquipmentModPoolService.cs +++ b/Libraries/SPTarkov.Server.Core/Services/BotEquipmentModPoolService.cs @@ -12,7 +12,7 @@ namespace SPTarkov.Server.Core.Services; [Injectable(InjectionType.Singleton)] public class BotEquipmentModPoolService { - private readonly Lock _lock = new(); + private readonly Lock _lockObject = new(); protected bool _armorPoolGenerated; protected BotConfig _botConfig; protected ConfigServer _configServer; @@ -121,7 +121,7 @@ public class BotEquipmentModPoolService private bool SetContainsTpl(HashSet itemSet, string tpl) { - lock (_lock) + lock (_lockObject) { return itemSet.Contains(tpl); } @@ -129,7 +129,7 @@ public class BotEquipmentModPoolService private bool AddTplToSet(HashSet itemSet, string itemToAddTpl) { - lock (_lock) + lock (_lockObject) { return itemSet.Add(itemToAddTpl); } @@ -137,7 +137,7 @@ public class BotEquipmentModPoolService private bool InitSetInDict(ConcurrentDictionary> dictionary, string slotName) { - lock (_lock) + lock (_lockObject) { return dictionary.TryAdd(slotName, []); } diff --git a/Libraries/SPTarkov.Server.Core/Services/BotLootCacheService.cs b/Libraries/SPTarkov.Server.Core/Services/BotLootCacheService.cs index ad8ef662..1d983733 100644 --- a/Libraries/SPTarkov.Server.Core/Services/BotLootCacheService.cs +++ b/Libraries/SPTarkov.Server.Core/Services/BotLootCacheService.cs @@ -20,7 +20,7 @@ public class BotLootCacheService( ICloner _cloner ) { - protected object _lock = new(); + protected readonly Lock _lockObject = new(); protected Dictionary _lootCache = new(); /// @@ -28,7 +28,7 @@ public class BotLootCacheService( /// public void ClearCache() { - lock (_lock) + lock (_lockObject) { _lootCache.Clear(); } @@ -49,7 +49,7 @@ public class BotLootCacheService( BotType botJsonTemplate, MinMax? itemPriceMinMax = null) { - lock (_lock) + lock (_lockObject) { if (!BotRoleExistsInCache(botRole)) { @@ -60,7 +60,7 @@ public class BotLootCacheService( Dictionary result = null; BotLootCache botRoleCache; - lock (_lock) + lock (_lockObject) { botRoleCache = _lootCache[botRole]; } @@ -448,7 +448,7 @@ public class BotLootCacheService( } BotLootCache cacheForRole; - lock (_lock) + lock (_lockObject) { cacheForRole = _lootCache[botRole]; @@ -544,7 +544,7 @@ public class BotLootCacheService( /// true if they exist protected bool BotRoleExistsInCache(string botRole) { - lock (_lock) + lock (_lockObject) { return _lootCache.ContainsKey(botRole); } @@ -556,7 +556,7 @@ public class BotLootCacheService( /// Bot role to hydrate protected void InitCacheForBotRole(string botRole) { - lock (_lock) + lock (_lockObject) { _lootCache.Add( botRole, diff --git a/Libraries/SPTarkov.Server.Core/Services/BotNameService.cs b/Libraries/SPTarkov.Server.Core/Services/BotNameService.cs index 43e580e2..ab5f6dc5 100644 --- a/Libraries/SPTarkov.Server.Core/Services/BotNameService.cs +++ b/Libraries/SPTarkov.Server.Core/Services/BotNameService.cs @@ -21,7 +21,7 @@ public class BotNameService( ) { protected BotConfig _botConfig = _configServer.GetConfig(); - protected object _lock = new(); + protected readonly Lock _lockObject = new(); protected HashSet _usedNameCache = new(); /// @@ -113,7 +113,7 @@ public class BotNameService( private bool AddNameToCache(string name) { - lock (_lock) + lock (_lockObject) { return _usedNameCache.Add(name); } @@ -121,7 +121,7 @@ public class BotNameService( protected bool CacheContainsName(string name) { - lock (_lock) + lock (_lockObject) { return _usedNameCache.Contains(name); } diff --git a/Libraries/SPTarkov.Server.Core/Utils/ImporterUtil.cs b/Libraries/SPTarkov.Server.Core/Utils/ImporterUtil.cs index cbcea964..a8faeb11 100644 --- a/Libraries/SPTarkov.Server.Core/Utils/ImporterUtil.cs +++ b/Libraries/SPTarkov.Server.Core/Utils/ImporterUtil.cs @@ -52,7 +52,7 @@ public class ImporterUtil ) { var tasks = new List(); - var dictionaryLock = new object(); + var dictionaryLock = new Lock(); var result = Activator.CreateInstance(loadedType); // get all filepaths @@ -93,7 +93,7 @@ public class ImporterUtil Action? onReadCallback, Action? onObjectDeserialized, object result, - object dictionaryLock + Lock dictionaryLock ) { try @@ -135,7 +135,7 @@ public class ImporterUtil string directory, Type loadedType, object result, - object dictionaryLock + Lock dictionaryLock ) { try diff --git a/Libraries/SPTarkov.Server.Core/Utils/RagfairOfferHolder.cs b/Libraries/SPTarkov.Server.Core/Utils/RagfairOfferHolder.cs index adfebe43..5ae5b2a9 100644 --- a/Libraries/SPTarkov.Server.Core/Utils/RagfairOfferHolder.cs +++ b/Libraries/SPTarkov.Server.Core/Utils/RagfairOfferHolder.cs @@ -20,14 +20,14 @@ public class RagfairOfferHolder( { protected int _maxOffersPerTemplate = configServer.GetConfig().Dynamic.OfferItemCount.Max; protected Dictionary _offersById = new(); - protected object _offersByIdLock = new(); + protected readonly Lock _offersByIdLock = new(); protected Dictionary> _offersByTemplate = new(); // key = tplId, value = list of offerIds - protected object _offersByTemplateLock = new(); + protected readonly Lock _offersByTemplateLock = new(); protected Dictionary> _offersByTrader = new(); // key = traderId, value = list of offerIds - protected object _offersByTraderLock = new(); + protected readonly Lock _offersByTraderLock = new(); protected HashSet _expiredOfferIds = []; - protected object _expiredOfferIdsLock = new(); + protected readonly Lock _expiredOfferIdsLock = new(); /// /// Get a ragfair offer by its id