diff --git a/Source/ACE.Database/Models/Shard/CharacterExtensions.cs b/Source/ACE.Database/Models/Shard/CharacterExtensions.cs index a37d40fc20..dbb9e11591 100644 --- a/Source/ACE.Database/Models/Shard/CharacterExtensions.cs +++ b/Source/ACE.Database/Models/Shard/CharacterExtensions.cs @@ -11,62 +11,41 @@ public static class CharacterExtensions // CharacterPropertiesContract // ===================================== - public static List GetContracts(this Character character, ReaderWriterLockSlim rwLock) + public static List GetContracts(this Character character, Object rwLock) { - rwLock.EnterReadLock(); - try + lock (rwLock) { return character.CharacterPropertiesContractRegistry.ToList(); } - finally - { - rwLock.ExitReadLock(); - } } - public static int GetContractsCount(this Character character, ReaderWriterLockSlim rwLock) + public static int GetContractsCount(this Character character, Object rwLock) { - rwLock.EnterReadLock(); - try + lock (rwLock) { return character.CharacterPropertiesContractRegistry.Count; } - finally - { - rwLock.ExitReadLock(); - } } - public static List GetContractsIds(this Character character, ReaderWriterLockSlim rwLock) + public static List GetContractsIds(this Character character, Object rwLock) { - rwLock.EnterReadLock(); - try + lock (rwLock) { return character.CharacterPropertiesContractRegistry.Select(r => r.ContractId).ToList(); } - finally - { - rwLock.ExitReadLock(); - } } - public static CharacterPropertiesContractRegistry GetContract(this Character character, uint contractId, ReaderWriterLockSlim rwLock) + public static CharacterPropertiesContractRegistry GetContract(this Character character, uint contractId, Object rwLock) { - rwLock.EnterReadLock(); - try + lock (rwLock) { return character.CharacterPropertiesContractRegistry.FirstOrDefault(c => c.ContractId == contractId); } - finally - { - rwLock.ExitReadLock(); - } } - public static CharacterPropertiesContractRegistry GetOrCreateContract(this Character character, uint contractId, ReaderWriterLockSlim rwLock, out bool contractWasCreated) + public static CharacterPropertiesContractRegistry GetOrCreateContract(this Character character, uint contractId, Object rwLock, out bool contractWasCreated) { - rwLock.EnterWriteLock(); - try + lock (rwLock) { var entity = character.CharacterPropertiesContractRegistry.FirstOrDefault(c => c.ContractId == contractId); @@ -86,16 +65,11 @@ public static CharacterPropertiesContractRegistry GetOrCreateContract(this Chara return entity; } - finally - { - rwLock.ExitWriteLock(); - } } - public static bool EraseContract(this Character character, uint contractId, out CharacterPropertiesContractRegistry contractErased, ReaderWriterLockSlim rwLock) + public static bool EraseContract(this Character character, uint contractId, out CharacterPropertiesContractRegistry contractErased, Object rwLock) { - rwLock.EnterWriteLock(); - try + lock (rwLock) { contractErased = character.CharacterPropertiesContractRegistry.FirstOrDefault(c => c.ContractId == contractId); @@ -106,25 +80,16 @@ public static bool EraseContract(this Character character, uint contractId, out return true; } - finally - { - rwLock.ExitWriteLock(); - } } - public static void EraseAllContracts(this Character character, out List contractsErased, ReaderWriterLockSlim rwLock) + public static void EraseAllContracts(this Character character, out List contractsErased, Object rwLock) { - rwLock.EnterWriteLock(); - try + lock (rwLock) { contractsErased = character.CharacterPropertiesContractRegistry.ToList(); character.CharacterPropertiesContractRegistry.Clear(); } - finally - { - rwLock.ExitWriteLock(); - } } @@ -132,36 +97,25 @@ public static void EraseAllContracts(this Character character, out List i.SpellComponentId == wcid); } - finally - { - rwLock.ExitReadLock(); - } } - public static List GetFillComponents(this Character character, ReaderWriterLockSlim rwLock) + public static List GetFillComponents(this Character character, Object rwLock) { - rwLock.EnterReadLock(); - try + lock (rwLock) { return character.CharacterPropertiesFillCompBook.ToList(); } - finally - { - rwLock.ExitReadLock(); - } } - public static CharacterPropertiesFillCompBook AddFillComponent(this Character character, uint wcid, uint amount, ReaderWriterLockSlim rwLock, out bool alreadyExists) + public static CharacterPropertiesFillCompBook AddFillComponent(this Character character, uint wcid, uint amount, Object rwLock, out bool alreadyExists) { - rwLock.EnterUpgradeableReadLock(); - try + lock (rwLock) { var entity = character.CharacterPropertiesFillCompBook.FirstOrDefault(i => i.SpellComponentId == wcid); if (entity != null) @@ -170,51 +124,27 @@ public static CharacterPropertiesFillCompBook AddFillComponent(this Character ch return entity; } - rwLock.EnterWriteLock(); - try - { entity = new CharacterPropertiesFillCompBook { CharacterId = character.Id, SpellComponentId = (int)wcid, QuantityToRebuy = (int)amount }; character.CharacterPropertiesFillCompBook.Add(entity); alreadyExists = false; return entity; - } - finally - { - rwLock.ExitWriteLock(); - } - } - finally - { - rwLock.ExitUpgradeableReadLock(); } } - public static bool TryRemoveFillComponent(this Character character, uint wcid, out CharacterPropertiesFillCompBook entity, ReaderWriterLockSlim rwLock) + public static bool TryRemoveFillComponent(this Character character, uint wcid, out CharacterPropertiesFillCompBook entity, Object rwLock) { - rwLock.EnterUpgradeableReadLock(); - try + lock (rwLock) { entity = character.CharacterPropertiesFillCompBook.FirstOrDefault(i => i.SpellComponentId == wcid); if (entity != null) { - rwLock.EnterWriteLock(); - try - { character.CharacterPropertiesFillCompBook.Remove(entity); entity.Character = null; return true; - } - finally - { - rwLock.ExitWriteLock(); - } + } return false; } - finally - { - rwLock.ExitUpgradeableReadLock(); - } } @@ -222,23 +152,17 @@ public static bool TryRemoveFillComponent(this Character character, uint wcid, o // CharacterPropertiesFriendList // ===================================== - public static List GetFriends(this Character character, ReaderWriterLockSlim rwLock) + public static List GetFriends(this Character character, Object rwLock) { - rwLock.EnterReadLock(); - try + lock (rwLock) { return character.CharacterPropertiesFriendList.ToList(); } - finally - { - rwLock.ExitReadLock(); - } } - public static bool HasAsFriend(this Character character, uint friendId, ReaderWriterLockSlim rwLock) + public static bool HasAsFriend(this Character character, uint friendId, Object rwLock) { - rwLock.EnterReadLock(); - try + lock (rwLock) { foreach (var record in character.CharacterPropertiesFriendList) { @@ -248,16 +172,11 @@ public static bool HasAsFriend(this Character character, uint friendId, ReaderWr return false; } - finally - { - rwLock.ExitReadLock(); - } } - public static CharacterPropertiesFriendList AddFriend(this Character character, uint friendId, ReaderWriterLockSlim rwLock, out bool friendAlreadyExists) + public static CharacterPropertiesFriendList AddFriend(this Character character, uint friendId, Object rwLock, out bool friendAlreadyExists) { - rwLock.EnterUpgradeableReadLock(); - try + lock (rwLock) { var entity = character.CharacterPropertiesFriendList.FirstOrDefault(x => x.FriendId == friendId); if (entity != null) @@ -266,59 +185,32 @@ public static CharacterPropertiesFriendList AddFriend(this Character character, return entity; } - rwLock.EnterWriteLock(); - try - { entity = new CharacterPropertiesFriendList { CharacterId = character.Id, FriendId = friendId, Character = character}; character.CharacterPropertiesFriendList.Add(entity); friendAlreadyExists = false; return entity; - } - finally - { - rwLock.ExitWriteLock(); - } - } - finally - { - rwLock.ExitUpgradeableReadLock(); } } - public static bool TryRemoveFriend(this Character character, uint friendId, out CharacterPropertiesFriendList entity, ReaderWriterLockSlim rwLock) + public static bool TryRemoveFriend(this Character character, uint friendId, out CharacterPropertiesFriendList entity, Object rwLock) { - rwLock.EnterUpgradeableReadLock(); - try + lock (rwLock) { entity = character.CharacterPropertiesFriendList.FirstOrDefault(x => x.FriendId == friendId); if (entity != null) { - rwLock.EnterWriteLock(); - try - { character.CharacterPropertiesFriendList.Remove(entity); entity.Character = null; return true; - } - finally - { - rwLock.ExitWriteLock(); - } } return false; } - finally - { - rwLock.ExitUpgradeableReadLock(); - } } - public static bool ClearAllFriends(this Character character, ReaderWriterLockSlim rwLock) + public static bool ClearAllFriends(this Character character, Object rwLock) { - rwLock.EnterUpgradeableReadLock(); - try + lock (rwLock) { - rwLock.EnterWriteLock(); try { character.CharacterPropertiesFriendList.Clear(); @@ -328,14 +220,6 @@ public static bool ClearAllFriends(this Character character, ReaderWriterLockSli { return false; } - finally - { - rwLock.ExitWriteLock(); - } - } - finally - { - rwLock.ExitUpgradeableReadLock(); } } @@ -344,36 +228,25 @@ public static bool ClearAllFriends(this Character character, ReaderWriterLockSli // CharacterPropertiesQuestRegistry // ===================================== - public static List GetQuests(this Character character, ReaderWriterLockSlim rwLock) + public static List GetQuests(this Character character, Object rwLock) { - rwLock.EnterReadLock(); - try + lock (rwLock) { return character.CharacterPropertiesQuestRegistry.ToList(); } - finally - { - rwLock.ExitReadLock(); - } } - public static CharacterPropertiesQuestRegistry GetQuest(this Character character, string questName, ReaderWriterLockSlim rwLock) + public static CharacterPropertiesQuestRegistry GetQuest(this Character character, string questName, Object rwLock) { - rwLock.EnterReadLock(); - try + lock (rwLock) { return character.CharacterPropertiesQuestRegistry.FirstOrDefault(q => q.QuestName.Equals(questName, StringComparison.OrdinalIgnoreCase)); } - finally - { - rwLock.ExitReadLock(); - } } - public static CharacterPropertiesQuestRegistry GetOrCreateQuest(this Character character, string questName, ReaderWriterLockSlim rwLock, out bool questRegistryWasCreated) + public static CharacterPropertiesQuestRegistry GetOrCreateQuest(this Character character, string questName, Object rwLock, out bool questRegistryWasCreated) { - rwLock.EnterWriteLock(); - try + lock (rwLock) { var entity = character.CharacterPropertiesQuestRegistry.FirstOrDefault(q => q.QuestName.Equals(questName, StringComparison.OrdinalIgnoreCase)); @@ -393,16 +266,11 @@ public static CharacterPropertiesQuestRegistry GetOrCreateQuest(this Character c return entity; } - finally - { - rwLock.ExitWriteLock(); - } } - public static bool EraseQuest(this Character character, string questName, ReaderWriterLockSlim rwLock) + public static bool EraseQuest(this Character character, string questName, Object rwLock) { - rwLock.EnterWriteLock(); - try + lock (rwLock) { var entity = character.CharacterPropertiesQuestRegistry.FirstOrDefault(q => q.QuestName.Equals(questName, StringComparison.OrdinalIgnoreCase)); @@ -413,25 +281,16 @@ public static bool EraseQuest(this Character character, string questName, Reader return true; } - finally - { - rwLock.ExitWriteLock(); - } } - public static void EraseAllQuests(this Character character, out List questNamesErased, ReaderWriterLockSlim rwLock) + public static void EraseAllQuests(this Character character, out List questNamesErased, Object rwLock) { - rwLock.EnterWriteLock(); - try + lock (rwLock) { questNamesErased = character.CharacterPropertiesQuestRegistry.Select(r => r.QuestName).ToList(); character.CharacterPropertiesQuestRegistry.Clear(); } - finally - { - rwLock.ExitWriteLock(); - } } @@ -439,27 +298,20 @@ public static void EraseAllQuests(this Character character, out List que // CharacterPropertiesShortcutBar // ===================================== - public static List GetShortcuts(this Character character, ReaderWriterLockSlim rwLock) + public static List GetShortcuts(this Character character, Object rwLock) { - rwLock.EnterReadLock(); - try + lock (rwLock) { return character.CharacterPropertiesShortcutBar.ToList(); } - finally - { - rwLock.ExitReadLock(); - } } - public static void AddOrUpdateShortcut(this Character character, uint index, uint objectId, ReaderWriterLockSlim rwLock) + public static void AddOrUpdateShortcut(this Character character, uint index, uint objectId, Object rwLock) { - rwLock.EnterUpgradeableReadLock(); - try + lock (rwLock) { var entity = character.CharacterPropertiesShortcutBar.FirstOrDefault(x => x.ShortcutBarIndex == index + 1); - rwLock.EnterWriteLock(); - try + { if (entity == null) { @@ -469,44 +321,23 @@ public static void AddOrUpdateShortcut(this Character character, uint index, uin else entity.ShortcutObjectId = objectId; } - finally - { - rwLock.ExitWriteLock(); - } - } - finally - { - rwLock.ExitUpgradeableReadLock(); } } - public static bool TryRemoveShortcut(this Character character, uint index, out CharacterPropertiesShortcutBar entity, ReaderWriterLockSlim rwLock) + public static bool TryRemoveShortcut(this Character character, uint index, out CharacterPropertiesShortcutBar entity, Object rwLock) { - rwLock.EnterUpgradeableReadLock(); - try + lock (rwLock) { entity = character.CharacterPropertiesShortcutBar.FirstOrDefault(x => x.ShortcutBarIndex == index + 1); if (entity != null) { - rwLock.EnterWriteLock(); - try - { character.CharacterPropertiesShortcutBar.Remove(entity); entity.Character = null; return true; - } - finally - { - rwLock.ExitWriteLock(); - } } return false; } - finally - { - rwLock.ExitUpgradeableReadLock(); - } } @@ -514,33 +345,25 @@ public static bool TryRemoveShortcut(this Character character, uint index, out C // CharacterPropertiesSpellBar // ===================================== - public static List GetSpellsInBar(this Character character, int barNumber, ReaderWriterLockSlim rwLock) + public static List GetSpellsInBar(this Character character, int barNumber, Object rwLock) { - rwLock.EnterReadLock(); - try + lock (rwLock) { return character.CharacterPropertiesSpellBar.Where(x => x.SpellBarNumber == barNumber + 1).OrderBy(x => x.SpellBarIndex).ToList(); } - finally - { - rwLock.ExitReadLock(); - } } /// /// This will incrememt all existing spells on the same barNumber at or after indexInBar by one before adding the new spell. /// - public static bool AddSpellToBar(this Character character, uint barNumber, uint indexInBar, uint spell, ReaderWriterLockSlim rwLock) + public static bool AddSpellToBar(this Character character, uint barNumber, uint indexInBar, uint spell, Object rwLock) { - rwLock.EnterUpgradeableReadLock(); - try + lock (rwLock) { var entity = character.CharacterPropertiesSpellBar.FirstOrDefault(x => x.SpellBarNumber == barNumber + 1 && x.SpellId == spell); + if (entity == null) { - rwLock.EnterWriteLock(); - try - { var spellCountInThisBar = character.CharacterPropertiesSpellBar.Count(x => x.SpellBarNumber == barNumber + 1); //Console.WriteLine($"Character.AddSpellToBar.Entry: barNumber = {barNumber} ({barNumber + 1}) | indexInBar = {indexInBar} ({indexInBar + 1}) | spell = {spell} | spellCountInThisBar = {spellCountInThisBar}"); @@ -565,36 +388,24 @@ public static bool AddSpellToBar(this Character character, uint barNumber, uint //Console.WriteLine($"Character.AddSpellToBar.Add: barNumber = {barNumber + 1} | indexInBar = {indexInBar + 1} | spell = {spell}"); return true; - } - finally - { - rwLock.ExitWriteLock(); - } } return false; } - finally - { - rwLock.ExitUpgradeableReadLock(); - } } /// /// This will decrement all existing spells on the same barNumber after indexInBar by one after removing the existing spell. /// - public static bool TryRemoveSpellFromBar(this Character character, uint barNumber, uint spell, out CharacterPropertiesSpellBar entity, ReaderWriterLockSlim rwLock) + public static bool TryRemoveSpellFromBar(this Character character, uint barNumber, uint spell, out CharacterPropertiesSpellBar entity, Object rwLock) { - rwLock.EnterUpgradeableReadLock(); - try + lock (rwLock) { //Console.WriteLine($"Character.TryRemoveSpellFromBar.Entry: barNumber = {barNumber} ({barNumber + 1}) | spell = {spell}"); entity = character.CharacterPropertiesSpellBar.FirstOrDefault(x => x.SpellBarNumber == barNumber + 1 && x.SpellId == spell); + if (entity != null) { - rwLock.EnterWriteLock(); - try - { //Console.WriteLine($"Character.TryRemoveSpellFromBar.Remove: SpellBarNumber = {entity.SpellBarNumber} | SpellBarIndex = {entity.SpellBarIndex} | SpellId = {entity.SpellId}"); character.CharacterPropertiesSpellBar.Remove(entity); entity.Character = null; @@ -610,46 +421,29 @@ public static bool TryRemoveSpellFromBar(this Character character, uint barNumbe } return true; - } - finally - { - rwLock.ExitWriteLock(); - } } return false; } - finally - { - rwLock.ExitUpgradeableReadLock(); - } } // ===================================== // CharacterPropertiesSquelch // ===================================== - public static List GetSquelches(this Character character, ReaderWriterLockSlim rwLock) + public static List GetSquelches(this Character character, Object rwLock) { - rwLock.EnterReadLock(); - try + lock (rwLock) { return character.CharacterPropertiesSquelch.ToList(); } - finally - { - rwLock.ExitReadLock(); - } } - public static void AddOrUpdateSquelch(this Character character, uint squelchCharacterId, uint squelchAccountId, uint type, ReaderWriterLockSlim rwLock) + public static void AddOrUpdateSquelch(this Character character, uint squelchCharacterId, uint squelchAccountId, uint type, Object rwLock) { - rwLock.EnterUpgradeableReadLock(); - try + lock (rwLock) { var entity = character.CharacterPropertiesSquelch.FirstOrDefault(x => x.SquelchCharacterId == squelchCharacterId); - rwLock.EnterWriteLock(); - try - { + if (entity == null) { entity = new CharacterPropertiesSquelch { CharacterId = character.Id, SquelchCharacterId = squelchCharacterId, SquelchAccountId = squelchAccountId, Type = type, Character = character }; @@ -660,68 +454,41 @@ public static void AddOrUpdateSquelch(this Character character, uint squelchChar entity.SquelchAccountId = squelchAccountId; entity.Type = type; } - } - finally - { - rwLock.ExitWriteLock(); - } - } - finally - { - rwLock.ExitUpgradeableReadLock(); } } - public static bool TryRemoveSquelch(this Character character, uint squelchCharacterId, uint squelchAccountId, out CharacterPropertiesSquelch entity, ReaderWriterLockSlim rwLock) + public static bool TryRemoveSquelch(this Character character, uint squelchCharacterId, uint squelchAccountId, out CharacterPropertiesSquelch entity, Object rwLock) { - rwLock.EnterUpgradeableReadLock(); - try + lock (rwLock) { entity = character.CharacterPropertiesSquelch.FirstOrDefault(x => x.SquelchCharacterId == squelchCharacterId && x.SquelchAccountId == squelchAccountId); if (entity != null) { - rwLock.EnterWriteLock(); - try - { character.CharacterPropertiesSquelch.Remove(entity); entity.Character = null; return true; - } - finally - { - rwLock.ExitWriteLock(); - } + } return false; } - finally - { - rwLock.ExitUpgradeableReadLock(); - } } // ===================================== // CharacterPropertiesTitleBook // ===================================== - public static List GetTitles(this Character character, ReaderWriterLockSlim rwLock) + public static List GetTitles(this Character character, Object rwLock) { - rwLock.EnterReadLock(); - try + lock (rwLock) { return character.CharacterPropertiesTitleBook.ToList(); } - finally - { - rwLock.ExitReadLock(); - } } - public static void AddTitleToRegistry(this Character character, uint title, ReaderWriterLockSlim rwLock, out bool titleAlreadyExists, out int numCharacterTitles) + public static void AddTitleToRegistry(this Character character, uint title, Object rwLock, out bool titleAlreadyExists, out int numCharacterTitles) { - rwLock.EnterUpgradeableReadLock(); - try + lock (rwLock) { var entity = character.CharacterPropertiesTitleBook.FirstOrDefault(x => x.TitleId == title); if (entity != null) @@ -731,22 +498,10 @@ public static void AddTitleToRegistry(this Character character, uint title, Read return; } - rwLock.EnterWriteLock(); - try - { entity = new CharacterPropertiesTitleBook { CharacterId = character.Id, TitleId = title, Character = character }; character.CharacterPropertiesTitleBook.Add(entity); titleAlreadyExists = false; numCharacterTitles = character.CharacterPropertiesTitleBook.Count; - } - finally - { - rwLock.ExitWriteLock(); - } - } - finally - { - rwLock.ExitUpgradeableReadLock(); } } } diff --git a/Source/ACE.Database/SerializedShardDatabase.cs b/Source/ACE.Database/SerializedShardDatabase.cs index 01f8da80c1..ce0ae62a94 100644 --- a/Source/ACE.Database/SerializedShardDatabase.cs +++ b/Source/ACE.Database/SerializedShardDatabase.cs @@ -116,7 +116,7 @@ public void GetSequenceGaps(uint min, uint limitAvailableIDsReturned, Action callback) + public void SaveBiota(ACE.Entity.Models.Biota biota, Object rwLock, Action callback) { _queue.Add(new Task(() => { @@ -126,7 +126,7 @@ public void SaveBiota(ACE.Entity.Models.Biota biota, ReaderWriterLockSlim rwLock } - public void SaveBiotasInParallel(IEnumerable<(ACE.Entity.Models.Biota biota, ReaderWriterLockSlim rwLock)> biotas, Action callback, bool doNotAddToCache = false) + public void SaveBiotasInParallel(IEnumerable<(ACE.Entity.Models.Biota biota, Object rwLock)> biotas, Action callback, bool doNotAddToCache = false) { _queue.Add(new Task(() => { @@ -220,7 +220,7 @@ public void GetCharacter(uint characterId, Action callback) })); } - public void SaveCharacter(Character character, ReaderWriterLockSlim rwLock, Action callback) + public void SaveCharacter(Character character, Object rwLock, Action callback) { _queue.Add(new Task(() => { @@ -245,7 +245,7 @@ public void SetCharacterAccessLevelByName(string name, AccessLevel accessLevel, } - public void AddCharacterInParallel(ACE.Entity.Models.Biota biota, ReaderWriterLockSlim biotaLock, IEnumerable<(ACE.Entity.Models.Biota biota, ReaderWriterLockSlim rwLock)> possessions, Character character, ReaderWriterLockSlim characterLock, Action callback) + public void AddCharacterInParallel(ACE.Entity.Models.Biota biota, Object biotaLock, IEnumerable<(ACE.Entity.Models.Biota biota, Object rwLock)> possessions, Character character, Object characterLock, Action callback) { _queue.Add(new Task(() => { diff --git a/Source/ACE.Database/ShardDatabase.cs b/Source/ACE.Database/ShardDatabase.cs index 9d96b3cb43..a855814f5e 100644 --- a/Source/ACE.Database/ShardDatabase.cs +++ b/Source/ACE.Database/ShardDatabase.cs @@ -309,14 +309,13 @@ protected bool DoSaveBiota(ShardDbContext context, Biota biota) } } - public virtual bool SaveBiota(ACE.Entity.Models.Biota biota, ReaderWriterLockSlim rwLock, bool doNotAddToCache = false) + public virtual bool SaveBiota(ACE.Entity.Models.Biota biota, Object rwLock, bool doNotAddToCache = false) { using (var context = new ShardDbContext()) { var existingBiota = GetBiota(context, biota.Id, doNotAddToCache); - rwLock.EnterReadLock(); - try + lock (rwLock) { if (existingBiota == null) { @@ -329,16 +328,12 @@ public virtual bool SaveBiota(ACE.Entity.Models.Biota biota, ReaderWriterLockSli ACE.Database.Adapter.BiotaUpdater.UpdateDatabaseBiota(context, biota, existingBiota); } } - finally - { - rwLock.ExitReadLock(); - } return DoSaveBiota(context, existingBiota); } } - public bool SaveBiotasInParallel(IEnumerable<(ACE.Entity.Models.Biota biota, ReaderWriterLockSlim rwLock)> biotas, bool doNotAddToCache = false) + public bool SaveBiotasInParallel(IEnumerable<(ACE.Entity.Models.Biota biota, Object rwLock)> biotas, bool doNotAddToCache = false) { var result = true; @@ -635,12 +630,11 @@ public Character GetCharacterStubByGuid(uint guid) return result; } - public bool SaveCharacter(Character character, ReaderWriterLockSlim rwLock) + public bool SaveCharacter(Character character, Object rwLock) { if (CharacterContexts.TryGetValue(character, out var cachedContext)) { - rwLock.EnterReadLock(); - try + lock (rwLock) { Exception firstException = null; retry: @@ -668,18 +662,13 @@ public bool SaveCharacter(Character character, ReaderWriterLockSlim rwLock) return false; } } - finally - { - rwLock.ExitReadLock(); - } - } + } var context = new ShardDbContext(); CharacterContexts.Add(character, context); - rwLock.EnterReadLock(); - try + lock (rwLock) { context.Character.Add(character); @@ -709,14 +698,10 @@ public bool SaveCharacter(Character character, ReaderWriterLockSlim rwLock) return false; } } - finally - { - rwLock.ExitReadLock(); - } } - public bool AddCharacterInParallel(ACE.Entity.Models.Biota biota, ReaderWriterLockSlim biotaLock, IEnumerable<(ACE.Entity.Models.Biota biota, ReaderWriterLockSlim rwLock)> possessions, Character character, ReaderWriterLockSlim characterLock) + public bool AddCharacterInParallel(ACE.Entity.Models.Biota biota, Object biotaLock, IEnumerable<(ACE.Entity.Models.Biota biota, Object rwLock)> possessions, Character character, Object characterLock) { if (!SaveBiota(biota, biotaLock)) return false; // Biota save failed which mean Character fails. diff --git a/Source/ACE.Database/ShardDatabaseWithCaching.cs b/Source/ACE.Database/ShardDatabaseWithCaching.cs index 9c1bc0511a..598b5c1889 100644 --- a/Source/ACE.Database/ShardDatabaseWithCaching.cs +++ b/Source/ACE.Database/ShardDatabaseWithCaching.cs @@ -134,7 +134,7 @@ public override Biota GetBiota(uint id, bool doNotAddToCache = false) return base.GetBiota(id, doNotAddToCache); } - public override bool SaveBiota(ACE.Entity.Models.Biota biota, ReaderWriterLockSlim rwLock, bool doNotAddToCache = false) + public override bool SaveBiota(ACE.Entity.Models.Biota biota, Object rwLock, bool doNotAddToCache = false) { CacheObject cachedBiota; @@ -145,15 +145,10 @@ public override bool SaveBiota(ACE.Entity.Models.Biota biota, ReaderWriterLockSl { cachedBiota.LastSeen = DateTime.UtcNow; - rwLock.EnterReadLock(); - try + lock (rwLock) { ACE.Database.Adapter.BiotaUpdater.UpdateDatabaseBiota(cachedBiota.Context, biota, cachedBiota.CachedObject); } - finally - { - rwLock.ExitReadLock(); - } return DoSaveBiota(cachedBiota.Context, cachedBiota.CachedObject); } @@ -164,8 +159,7 @@ public override bool SaveBiota(ACE.Entity.Models.Biota biota, ReaderWriterLockSl var existingBiota = base.GetBiota(context, biota.Id, doNotAddToCache); - rwLock.EnterReadLock(); - try + lock (rwLock) { if (existingBiota == null) { @@ -178,10 +172,6 @@ public override bool SaveBiota(ACE.Entity.Models.Biota biota, ReaderWriterLockSl ACE.Database.Adapter.BiotaUpdater.UpdateDatabaseBiota(context, biota, existingBiota); } } - finally - { - rwLock.ExitReadLock(); - } if (DoSaveBiota(context, existingBiota)) { diff --git a/Source/ACE.Entity/Models/BiotaExtensions.cs b/Source/ACE.Entity/Models/BiotaExtensions.cs index b2b07c775c..ef54dade3c 100644 --- a/Source/ACE.Entity/Models/BiotaExtensions.cs +++ b/Source/ACE.Entity/Models/BiotaExtensions.cs @@ -15,175 +15,130 @@ public static class BiotaExtensions // Bool, DID, Float, IID, Int, Int64, String, Position // ===================================== - public static bool? GetProperty(this Biota biota, PropertyBool property, ReaderWriterLockSlim rwLock) + public static bool? GetProperty(this Biota biota, PropertyBool property, Object rwLock) { if (biota.PropertiesBool == null) return null; - rwLock.EnterReadLock(); - try + lock (rwLock) { if (biota.PropertiesBool.TryGetValue(property, out var value)) return value; return null; } - finally - { - rwLock.ExitReadLock(); - } } - public static uint? GetProperty(this Biota biota, PropertyDataId property, ReaderWriterLockSlim rwLock) + public static uint? GetProperty(this Biota biota, PropertyDataId property, Object rwLock) { if (biota.PropertiesDID == null) return null; - rwLock.EnterReadLock(); - try + lock (rwLock) { if (biota.PropertiesDID.TryGetValue(property, out var value)) return value; return null; } - finally - { - rwLock.ExitReadLock(); - } } - public static double? GetProperty(this Biota biota, PropertyFloat property, ReaderWriterLockSlim rwLock) + public static double? GetProperty(this Biota biota, PropertyFloat property, Object rwLock) { if (biota.PropertiesFloat == null) return null; - rwLock.EnterReadLock(); - try + lock (rwLock) { if (biota.PropertiesFloat.TryGetValue(property, out var value)) return value; return null; } - finally - { - rwLock.ExitReadLock(); - } } - public static uint? GetProperty(this Biota biota, PropertyInstanceId property, ReaderWriterLockSlim rwLock) + public static uint? GetProperty(this Biota biota, PropertyInstanceId property, Object rwLock) { if (biota.PropertiesIID == null) return null; - rwLock.EnterReadLock(); - try + lock (rwLock) { if (biota.PropertiesIID.TryGetValue(property, out var value)) return value; return null; } - finally - { - rwLock.ExitReadLock(); - } } - public static int? GetProperty(this Biota biota, PropertyInt property, ReaderWriterLockSlim rwLock) + public static int? GetProperty(this Biota biota, PropertyInt property, Object rwLock) { if (biota.PropertiesInt == null) return null; - rwLock.EnterReadLock(); - try + lock (rwLock) { if (biota.PropertiesInt.TryGetValue(property, out var value)) return value; return null; } - finally - { - rwLock.ExitReadLock(); - } } - public static long? GetProperty(this Biota biota, PropertyInt64 property, ReaderWriterLockSlim rwLock) + public static long? GetProperty(this Biota biota, PropertyInt64 property, Object rwLock) { if (biota.PropertiesInt64 == null) return null; - rwLock.EnterReadLock(); - try + lock (rwLock) { if (biota.PropertiesInt64.TryGetValue(property, out var value)) return value; return null; } - finally - { - rwLock.ExitReadLock(); - } } - public static string GetProperty(this Biota biota, PropertyString property, ReaderWriterLockSlim rwLock) + public static string GetProperty(this Biota biota, PropertyString property, Object rwLock) { if (biota.PropertiesString == null) return null; - rwLock.EnterReadLock(); - try + lock (rwLock) { if (biota.PropertiesString.TryGetValue(property, out var value)) return value; return null; } - finally - { - rwLock.ExitReadLock(); - } } - public static PropertiesPosition GetProperty(this Biota biota, PositionType property, ReaderWriterLockSlim rwLock) + public static PropertiesPosition GetProperty(this Biota biota, PositionType property, Object rwLock) { if (biota.PropertiesPosition == null) return null; - rwLock.EnterReadLock(); - try + lock (rwLock) { if (biota.PropertiesPosition.TryGetValue(property, out var value)) return value; return null; } - finally - { - rwLock.ExitReadLock(); - } } - public static Position GetPosition(this Biota biota, PositionType property, ReaderWriterLockSlim rwLock) + public static Position GetPosition(this Biota biota, PositionType property, Object rwLock) { if (biota.PropertiesPosition == null) return null; - rwLock.EnterReadLock(); - try + lock (rwLock) { if (biota.PropertiesPosition.TryGetValue(property, out var value)) return new Position(value.ObjCellId, value.PositionX, value.PositionY, value.PositionZ, value.RotationX, value.RotationY, value.RotationZ, value.RotationW, property == PositionType.RelativeDestination); return null; } - finally - { - rwLock.ExitReadLock(); - } } @@ -192,10 +147,9 @@ public static Position GetPosition(this Biota biota, PositionType property, Read // Bool, DID, Float, IID, Int, Int64, String, Position // ===================================== - public static void SetProperty(this Biota biota, PropertyBool property, bool value, ReaderWriterLockSlim rwLock, out bool changed) + public static void SetProperty(this Biota biota, PropertyBool property, bool value, Object rwLock, out bool changed) { - rwLock.EnterWriteLock(); - try + lock (rwLock) { if (biota.PropertiesBool == null) biota.PropertiesBool = new Dictionary(); @@ -205,16 +159,11 @@ public static void SetProperty(this Biota biota, PropertyBool property, bool val if (changed) biota.PropertiesBool[property] = value; } - finally - { - rwLock.ExitWriteLock(); - } } - public static void SetProperty(this Biota biota, PropertyDataId property, uint value, ReaderWriterLockSlim rwLock, out bool changed) + public static void SetProperty(this Biota biota, PropertyDataId property, uint value, Object rwLock, out bool changed) { - rwLock.EnterWriteLock(); - try + lock (rwLock) { if (biota.PropertiesDID == null) biota.PropertiesDID = new Dictionary(); @@ -224,16 +173,11 @@ public static void SetProperty(this Biota biota, PropertyDataId property, uint v if (changed) biota.PropertiesDID[property] = value; } - finally - { - rwLock.ExitWriteLock(); - } } - public static void SetProperty(this Biota biota, PropertyFloat property, double value, ReaderWriterLockSlim rwLock, out bool changed) + public static void SetProperty(this Biota biota, PropertyFloat property, double value, Object rwLock, out bool changed) { - rwLock.EnterWriteLock(); - try + lock (rwLock) { if (biota.PropertiesFloat == null) biota.PropertiesFloat = new Dictionary(); @@ -243,16 +187,11 @@ public static void SetProperty(this Biota biota, PropertyFloat property, double if (changed) biota.PropertiesFloat[property] = value; } - finally - { - rwLock.ExitWriteLock(); - } } - public static void SetProperty(this Biota biota, PropertyInstanceId property, uint value, ReaderWriterLockSlim rwLock, out bool changed) + public static void SetProperty(this Biota biota, PropertyInstanceId property, uint value, Object rwLock, out bool changed) { - rwLock.EnterWriteLock(); - try + lock (rwLock) { if (biota.PropertiesIID == null) biota.PropertiesIID = new Dictionary(); @@ -262,16 +201,11 @@ public static void SetProperty(this Biota biota, PropertyInstanceId property, ui if (changed) biota.PropertiesIID[property] = value; } - finally - { - rwLock.ExitWriteLock(); - } } - public static void SetProperty(this Biota biota, PropertyInt property, int value, ReaderWriterLockSlim rwLock, out bool changed) + public static void SetProperty(this Biota biota, PropertyInt property, int value, Object rwLock, out bool changed) { - rwLock.EnterWriteLock(); - try + lock (rwLock) { if (biota.PropertiesInt == null) biota.PropertiesInt = new Dictionary(); @@ -281,16 +215,11 @@ public static void SetProperty(this Biota biota, PropertyInt property, int value if (changed) biota.PropertiesInt[property] = value; } - finally - { - rwLock.ExitWriteLock(); - } } - public static void SetProperty(this Biota biota, PropertyInt64 property, long value, ReaderWriterLockSlim rwLock, out bool changed) + public static void SetProperty(this Biota biota, PropertyInt64 property, long value, Object rwLock, out bool changed) { - rwLock.EnterWriteLock(); - try + lock (rwLock) { if (biota.PropertiesInt64 == null) biota.PropertiesInt64 = new Dictionary(); @@ -300,16 +229,11 @@ public static void SetProperty(this Biota biota, PropertyInt64 property, long va if (changed) biota.PropertiesInt64[property] = value; } - finally - { - rwLock.ExitWriteLock(); - } } - public static void SetProperty(this Biota biota, PropertyString property, string value, ReaderWriterLockSlim rwLock, out bool changed) + public static void SetProperty(this Biota biota, PropertyString property, string value, Object rwLock, out bool changed) { - rwLock.EnterWriteLock(); - try + lock (rwLock) { if (biota.PropertiesString == null) biota.PropertiesString = new Dictionary(); @@ -319,32 +243,22 @@ public static void SetProperty(this Biota biota, PropertyString property, string if (changed) biota.PropertiesString[property] = value; } - finally - { - rwLock.ExitWriteLock(); - } } - public static void SetProperty(this Biota biota, PositionType property, PropertiesPosition value, ReaderWriterLockSlim rwLock) + public static void SetProperty(this Biota biota, PositionType property, PropertiesPosition value, Object rwLock) { - rwLock.EnterWriteLock(); - try + lock (rwLock) { if (biota.PropertiesPosition == null) biota.PropertiesPosition = new Dictionary(); biota.PropertiesPosition[property] = value; } - finally - { - rwLock.ExitWriteLock(); - } } - public static void SetPosition(this Biota biota, PositionType property, Position value, ReaderWriterLockSlim rwLock) + public static void SetPosition(this Biota biota, PositionType property, Position value, Object rwLock) { - rwLock.EnterWriteLock(); - try + lock (rwLock) { if (biota.PropertiesPosition == null) biota.PropertiesPosition = new Dictionary(); @@ -353,10 +267,6 @@ public static void SetPosition(this Biota biota, PositionType property, Position biota.PropertiesPosition[property] = entity; } - finally - { - rwLock.ExitWriteLock(); - } } @@ -365,132 +275,92 @@ public static void SetPosition(this Biota biota, PositionType property, Position // Bool, DID, Float, IID, Int, Int64, String, Position // ===================================== - public static bool TryRemoveProperty(this Biota biota, PropertyBool property, ReaderWriterLockSlim rwLock) + public static bool TryRemoveProperty(this Biota biota, PropertyBool property, Object rwLock) { if (biota.PropertiesBool == null) return false; - rwLock.EnterWriteLock(); - try + lock (rwLock) { return biota.PropertiesBool.Remove(property); } - finally - { - rwLock.ExitWriteLock(); - } } - public static bool TryRemoveProperty(this Biota biota, PropertyDataId property, ReaderWriterLockSlim rwLock) + public static bool TryRemoveProperty(this Biota biota, PropertyDataId property, Object rwLock) { if (biota.PropertiesDID == null) return false; - rwLock.EnterWriteLock(); - try + lock (rwLock) { return biota.PropertiesDID.Remove(property); } - finally - { - rwLock.ExitWriteLock(); - } } - public static bool TryRemoveProperty(this Biota biota, PropertyFloat property, ReaderWriterLockSlim rwLock) + public static bool TryRemoveProperty(this Biota biota, PropertyFloat property, Object rwLock) { if (biota.PropertiesFloat == null) return false; - rwLock.EnterWriteLock(); - try + lock (rwLock) { return biota.PropertiesFloat.Remove(property); } - finally - { - rwLock.ExitWriteLock(); - } } - public static bool TryRemoveProperty(this Biota biota, PropertyInstanceId property, ReaderWriterLockSlim rwLock) + public static bool TryRemoveProperty(this Biota biota, PropertyInstanceId property, Object rwLock) { if (biota.PropertiesIID == null) return false; - rwLock.EnterWriteLock(); - try + lock (rwLock) { return biota.PropertiesIID.Remove(property); } - finally - { - rwLock.ExitWriteLock(); - } } - public static bool TryRemoveProperty(this Biota biota, PropertyInt property, ReaderWriterLockSlim rwLock) + public static bool TryRemoveProperty(this Biota biota, PropertyInt property, Object rwLock) { if (biota.PropertiesInt == null) return false; - rwLock.EnterWriteLock(); - try + lock (rwLock) { return biota.PropertiesInt.Remove(property); } - finally - { - rwLock.ExitWriteLock(); - } } - public static bool TryRemoveProperty(this Biota biota, PropertyInt64 property, ReaderWriterLockSlim rwLock) + public static bool TryRemoveProperty(this Biota biota, PropertyInt64 property, Object rwLock) { if (biota.PropertiesInt64 == null) return false; - rwLock.EnterWriteLock(); - try + lock (rwLock) { return biota.PropertiesInt64.Remove(property); } - finally - { - rwLock.ExitWriteLock(); - } } - public static bool TryRemoveProperty(this Biota biota, PropertyString property, ReaderWriterLockSlim rwLock) + public static bool TryRemoveProperty(this Biota biota, PropertyString property, Object rwLock) { if (biota.PropertiesString == null) return false; - rwLock.EnterWriteLock(); - try + lock (rwLock) { return biota.PropertiesString.Remove(property); } - finally - { - rwLock.ExitWriteLock(); - } } - public static bool TryRemoveProperty(this Biota biota, PositionType property, ReaderWriterLockSlim rwLock) + public static bool TryRemoveProperty(this Biota biota, PositionType property, Object rwLock) { if (biota.PropertiesPosition == null) return false; - rwLock.EnterWriteLock(); - try + lock (rwLock) { return biota.PropertiesPosition.Remove(property); } - finally - { - rwLock.ExitWriteLock(); - } } @@ -498,13 +368,12 @@ public static bool TryRemoveProperty(this Biota biota, PositionType property, Re // BiotaPropertiesSpellBook // ===================================== - public static Dictionary CloneSpells(this Biota biota, ReaderWriterLockSlim rwLock) + public static Dictionary CloneSpells(this Biota biota, Object rwLock) { if (biota.PropertiesSpellBook == null) return new Dictionary(); - rwLock.EnterReadLock(); - try + lock (rwLock) { var results = new Dictionary(); @@ -513,105 +382,77 @@ public static Dictionary CloneSpells(this Biota biota, ReaderWriterL return results; } - finally - { - rwLock.ExitReadLock(); - } } - public static bool HasKnownSpell(this Biota biota, ReaderWriterLockSlim rwLock) + public static bool HasKnownSpell(this Biota biota, Object rwLock) { if (biota.PropertiesSpellBook == null) return false; - rwLock.EnterReadLock(); - try + lock (rwLock) { return biota.PropertiesSpellBook.Count > 0; } - finally - { - rwLock.ExitReadLock(); - } } - public static List GetKnownSpellsIds(this Biota biota, ReaderWriterLockSlim rwLock) + public static List GetKnownSpellsIds(this Biota biota, Object rwLock) { if (biota.PropertiesSpellBook == null) return new List(); - rwLock.EnterReadLock(); - try + lock (rwLock) { if (biota.PropertiesSpellBook == null) return new List(); return new List(biota.PropertiesSpellBook.Keys); } - finally - { - rwLock.ExitReadLock(); - } } - public static List GetKnownSpellsIdsWhere(this Biota biota, Func predicate, ReaderWriterLockSlim rwLock) + public static List GetKnownSpellsIdsWhere(this Biota biota, Func predicate, Object rwLock) { if (biota.PropertiesSpellBook == null) return new List(); - rwLock.EnterReadLock(); - try + lock (rwLock) { if (biota.PropertiesSpellBook == null) return new List(); return biota.PropertiesSpellBook.Keys.Where(predicate).ToList(); } - finally - { - rwLock.ExitReadLock(); - } + } - public static List GetKnownSpellsProbabilities(this Biota biota, ReaderWriterLockSlim rwLock) + public static List GetKnownSpellsProbabilities(this Biota biota, Object rwLock) { if (biota.PropertiesSpellBook == null) return new List(); - rwLock.EnterReadLock(); - try + lock (rwLock) { if (biota.PropertiesSpellBook == null) return new List(); return new List(biota.PropertiesSpellBook.Values); } - finally - { - rwLock.ExitReadLock(); - } + } - public static bool SpellIsKnown(this Biota biota, int spell, ReaderWriterLockSlim rwLock) + public static bool SpellIsKnown(this Biota biota, int spell, Object rwLock) { if (biota.PropertiesSpellBook == null) return false; - rwLock.EnterReadLock(); - try + lock (rwLock) { return biota.PropertiesSpellBook.ContainsKey(spell); } - finally - { - rwLock.ExitReadLock(); - } } - public static float GetOrAddKnownSpell(this Biota biota, int spell, ReaderWriterLockSlim rwLock, out bool spellAdded, float probability = 2.0f) + public static float GetOrAddKnownSpell(this Biota biota, int spell, Object rwLock, out bool spellAdded, float probability = 2.0f) { - rwLock.EnterWriteLock(); - try + lock (rwLock) { if (biota.PropertiesSpellBook != null && biota.PropertiesSpellBook.TryGetValue(spell, out var value)) { @@ -627,19 +468,15 @@ public static float GetOrAddKnownSpell(this Biota biota, int spell, ReaderWriter return probability; } - finally - { - rwLock.ExitWriteLock(); - } + } - public static Dictionary GetMatchingSpells(this Biota biota, HashSet match, ReaderWriterLockSlim rwLock) + public static Dictionary GetMatchingSpells(this Biota biota, HashSet match, Object rwLock) { if (biota.PropertiesSpellBook == null) return new Dictionary(); - rwLock.EnterReadLock(); - try + lock (rwLock) { var results = new Dictionary(); @@ -651,42 +488,28 @@ public static Dictionary GetMatchingSpells(this Biota biota, HashSet return results; } - finally - { - rwLock.ExitReadLock(); - } } - public static bool TryRemoveKnownSpell(this Biota biota, int spell, ReaderWriterLockSlim rwLock) + public static bool TryRemoveKnownSpell(this Biota biota, int spell, Object rwLock) { if (biota.PropertiesSpellBook == null) return false; - rwLock.EnterWriteLock(); - try + lock (rwLock) { return biota.PropertiesSpellBook.Remove(spell); } - finally - { - rwLock.ExitWriteLock(); - } } - public static void ClearSpells(this Biota biota, ReaderWriterLockSlim rwLock) + public static void ClearSpells(this Biota biota, Object rwLock) { if (biota.PropertiesSpellBook == null) return; - rwLock.EnterWriteLock(); - try + lock (rwLock) { biota.PropertiesSpellBook?.Clear(); } - finally - { - rwLock.ExitWriteLock(); - } } @@ -694,13 +517,12 @@ public static void ClearSpells(this Biota biota, ReaderWriterLockSlim rwLock) // BiotaPropertiesSkill // ===================================== - public static PropertiesSkill GetSkill(this Biota biota, Skill skill, ReaderWriterLockSlim rwLock) + public static PropertiesSkill GetSkill(this Biota biota, Skill skill, Object rwLock) { if (biota.PropertiesSkill == null) return null; - rwLock.EnterReadLock(); - try + lock (rwLock) { if (biota.PropertiesSkill == null) return null; @@ -708,16 +530,11 @@ public static PropertiesSkill GetSkill(this Biota biota, Skill skill, ReaderWrit biota.PropertiesSkill.TryGetValue(skill, out var value); return value; } - finally - { - rwLock.ExitReadLock(); - } } - public static PropertiesSkill GetOrAddSkill(this Biota biota, Skill skill, ReaderWriterLockSlim rwLock, out bool skillAdded) + public static PropertiesSkill GetOrAddSkill(this Biota biota, Skill skill, Object rwLock, out bool skillAdded) { - rwLock.EnterWriteLock(); - try + lock (rwLock) { if (biota.PropertiesSkill != null && biota.PropertiesSkill.TryGetValue(skill, out var value)) { @@ -734,10 +551,6 @@ public static PropertiesSkill GetOrAddSkill(this Biota biota, Skill skill, Reade return entity; } - finally - { - rwLock.ExitWriteLock(); - } } @@ -745,42 +558,31 @@ public static PropertiesSkill GetOrAddSkill(this Biota biota, Skill skill, Reade // HousePermissions // ===================================== - public static Dictionary CloneHousePermissions(this Biota biota, ReaderWriterLockSlim rwLock) + public static Dictionary CloneHousePermissions(this Biota biota, Object rwLock) { - rwLock.EnterReadLock(); - try + lock (rwLock) { if (biota.HousePermissions == null) return new Dictionary(); return new Dictionary(biota.HousePermissions); } - finally - { - rwLock.ExitReadLock(); - } } - public static bool HasHouseGuest(this Biota biota, uint guestGuid, ReaderWriterLockSlim rwLock) + public static bool HasHouseGuest(this Biota biota, uint guestGuid, Object rwLock) { - rwLock.EnterReadLock(); - try + lock (rwLock) { if (biota.HousePermissions == null) return false; return biota.HousePermissions.ContainsKey(guestGuid); } - finally - { - rwLock.ExitReadLock(); - } } - public static bool? GetHouseGuestStoragePermission(this Biota biota, uint guestGuid, ReaderWriterLockSlim rwLock) + public static bool? GetHouseGuestStoragePermission(this Biota biota, uint guestGuid, Object rwLock) { - rwLock.EnterReadLock(); - try + lock (rwLock) { if (biota.HousePermissions == null) return null; @@ -790,42 +592,28 @@ public static bool HasHouseGuest(this Biota biota, uint guestGuid, ReaderWriterL return value; } - finally - { - rwLock.ExitReadLock(); - } } - public static void AddOrUpdateHouseGuest(this Biota biota, uint guestGuid, bool storage, ReaderWriterLockSlim rwLock) + public static void AddOrUpdateHouseGuest(this Biota biota, uint guestGuid, bool storage, Object rwLock) { - rwLock.EnterWriteLock(); - try + lock (rwLock) { if (biota.HousePermissions == null) biota.HousePermissions = new Dictionary(); biota.HousePermissions[guestGuid] = storage; } - finally - { - rwLock.ExitWriteLock(); - } } - public static bool RemoveHouseGuest(this Biota biota, uint guestGuid, ReaderWriterLockSlim rwLock) + public static bool RemoveHouseGuest(this Biota biota, uint guestGuid, Object rwLock) { - rwLock.EnterWriteLock(); - try + lock (rwLock) { if (biota.HousePermissions == null) return false; return biota.HousePermissions.Remove(guestGuid); } - finally - { - rwLock.ExitWriteLock(); - } } diff --git a/Source/ACE.Entity/Models/PropertiesAllegianceExtensions.cs b/Source/ACE.Entity/Models/PropertiesAllegianceExtensions.cs index 144d7e4d4c..04fff065cb 100644 --- a/Source/ACE.Entity/Models/PropertiesAllegianceExtensions.cs +++ b/Source/ACE.Entity/Models/PropertiesAllegianceExtensions.cs @@ -7,60 +7,44 @@ namespace ACE.Entity.Models { public static class PropertiesAllegianceExtensions { - public static Dictionary GetApprovedVassals(this IDictionary value, ReaderWriterLockSlim rwLock) + public static Dictionary GetApprovedVassals(this IDictionary value, Object rwLock) { - rwLock.EnterReadLock(); - try + lock (rwLock) { if (value == null) return new Dictionary(); return value.Where(i => i.Value.ApprovedVassal).ToDictionary(i => i.Key, i => i.Value); } - finally - { - rwLock.ExitReadLock(); - } } - public static Dictionary GetBanList(this IDictionary value, ReaderWriterLockSlim rwLock) + public static Dictionary GetBanList(this IDictionary value, Object rwLock) { if (value == null) return new Dictionary(); - rwLock.EnterReadLock(); - try + lock (rwLock) { return value.Where(i => i.Value.Banned).ToDictionary(i => i.Key, i => i.Value); } - finally - { - rwLock.ExitReadLock(); - } } - public static PropertiesAllegiance GetFirstOrDefaultByCharacterId(this IDictionary value, uint characterId, ReaderWriterLockSlim rwLock) + public static PropertiesAllegiance GetFirstOrDefaultByCharacterId(this IDictionary value, uint characterId, Object rwLock) { if (value == null) return null; - rwLock.EnterReadLock(); - try + lock (rwLock) { value.TryGetValue(characterId, out var entity); return entity; } - finally - { - rwLock.ExitReadLock(); - } } - public static void AddOrUpdateAllegiance(this IDictionary value, uint characterId, bool isBanned, bool approvedVassal, ReaderWriterLockSlim rwLock) + public static void AddOrUpdateAllegiance(this IDictionary value, uint characterId, bool isBanned, bool approvedVassal, Object rwLock) { - rwLock.EnterWriteLock(); - try + lock (rwLock) { if (!value.TryGetValue(characterId, out var entity)) { @@ -72,26 +56,17 @@ public static void AddOrUpdateAllegiance(this IDictionary value, uint characterId, ReaderWriterLockSlim rwLock) + public static bool TryRemoveAllegiance(this IDictionary value, uint characterId, Object rwLock) { if (value == null) return false; - rwLock.EnterWriteLock(); - try + lock (rwLock) { return value.Remove(characterId); } - finally - { - rwLock.ExitWriteLock(); - } } } } diff --git a/Source/ACE.Entity/Models/PropertiesAnimPartExtensions.cs b/Source/ACE.Entity/Models/PropertiesAnimPartExtensions.cs index 5aba26a795..f8d231cc5d 100644 --- a/Source/ACE.Entity/Models/PropertiesAnimPartExtensions.cs +++ b/Source/ACE.Entity/Models/PropertiesAnimPartExtensions.cs @@ -6,53 +6,38 @@ namespace ACE.Entity.Models { public static class PropertiesAnimPartExtensions { - public static int GetCount(this IList value, ReaderWriterLockSlim rwLock) + public static int GetCount(this IList value, Object rwLock) { if (value == null) return 0; - rwLock.EnterReadLock(); - try + lock (rwLock) { return value.Count; } - finally - { - rwLock.ExitReadLock(); - } } - public static List Clone(this IList value, ReaderWriterLockSlim rwLock) + public static List Clone(this IList value, Object rwLock) { if (value == null) return null; - rwLock.EnterReadLock(); - try + lock (rwLock) { return new List(value); } - finally - { - rwLock.ExitReadLock(); - } } - public static void CopyTo(this IList value, ICollection destination, ReaderWriterLockSlim rwLock) + public static void CopyTo(this IList value, ICollection destination, Object rwLock) { if (value == null) return; - rwLock.EnterReadLock(); - try + lock (rwLock) { foreach (var entry in value) destination.Add(entry); } - finally - { - rwLock.ExitReadLock(); - } } } } diff --git a/Source/ACE.Entity/Models/PropertiesBookPageDataExtensions.cs b/Source/ACE.Entity/Models/PropertiesBookPageDataExtensions.cs index d145b78343..16d8f16d4d 100644 --- a/Source/ACE.Entity/Models/PropertiesBookPageDataExtensions.cs +++ b/Source/ACE.Entity/Models/PropertiesBookPageDataExtensions.cs @@ -6,80 +6,59 @@ namespace ACE.Entity.Models { public static class PropertiesBookPageDataExtensions { - public static int GetPageCount(this IList value, ReaderWriterLockSlim rwLock) + public static int GetPageCount(this IList value, Object rwLock) { if (value == null) return 0; - rwLock.EnterReadLock(); - try + lock (rwLock) { return value.Count; } - finally - { - rwLock.ExitReadLock(); - } } - public static List Clone(this IList value, ReaderWriterLockSlim rwLock) + public static List Clone(this IList value, Object rwLock) { if (value == null) return null; - rwLock.EnterReadLock(); - try + lock (rwLock) { return new List(value); } - finally - { - rwLock.ExitReadLock(); - } } - public static PropertiesBookPageData GetPage(this IList value, int index, ReaderWriterLockSlim rwLock) + public static PropertiesBookPageData GetPage(this IList value, int index, Object rwLock) { if (value == null) return null; - rwLock.EnterReadLock(); - try + lock (rwLock) { if (value.Count <= index) return null; return value[index]; } - finally - { - rwLock.ExitReadLock(); - } } - public static void AddPage(this IList value, PropertiesBookPageData page, out int index, ReaderWriterLockSlim rwLock) + public static void AddPage(this IList value, PropertiesBookPageData page, out int index, Object rwLock) { - rwLock.EnterWriteLock(); - try + lock (rwLock) { value.Add(page); index = value.Count; } - finally - { - rwLock.ExitWriteLock(); - } } - public static bool RemovePage(this IList value, int index, ReaderWriterLockSlim rwLock) + public static bool RemovePage(this IList value, int index, Object rwLock) { if (value == null) return false; - rwLock.EnterWriteLock(); - try + lock (rwLock) { if (value.Count <= index) return false; @@ -88,10 +67,6 @@ public static bool RemovePage(this IList value, int inde return true; } - finally - { - rwLock.ExitWriteLock(); - } } } } diff --git a/Source/ACE.Entity/Models/PropertiesEnchantmentRegistryExtensions.cs b/Source/ACE.Entity/Models/PropertiesEnchantmentRegistryExtensions.cs index 9f2c68a6c5..90a372124b 100644 --- a/Source/ACE.Entity/Models/PropertiesEnchantmentRegistryExtensions.cs +++ b/Source/ACE.Entity/Models/PropertiesEnchantmentRegistryExtensions.cs @@ -9,62 +9,46 @@ namespace ACE.Entity.Models { public static class PropertiesEnchantmentRegistryExtensions { - public static List Clone(this ICollection value, ReaderWriterLockSlim rwLock) + public static List Clone(this ICollection value, Object rwLock) { if (value == null) return null; - rwLock.EnterReadLock(); - try + lock (rwLock) { return value.ToList(); } - finally - { - rwLock.ExitReadLock(); - } } - public static bool HasEnchantments(this ICollection value, ReaderWriterLockSlim rwLock) + public static bool HasEnchantments(this ICollection value, Object rwLock) { if (value == null) return false; - rwLock.EnterReadLock(); - try + lock (rwLock) { return value.Any(); } - finally - { - rwLock.ExitReadLock(); - } } - public static bool HasEnchantment(this ICollection value, uint spellId, ReaderWriterLockSlim rwLock) + public static bool HasEnchantment(this ICollection value, uint spellId, Object rwLock) { if (value == null) return false; - rwLock.EnterReadLock(); - try + lock (rwLock) { return value.Any(e => e.SpellId == spellId); } - finally - { - rwLock.ExitReadLock(); - } } - public static PropertiesEnchantmentRegistry GetEnchantmentBySpell(this ICollection value, int spellId, uint? casterGuid, ReaderWriterLockSlim rwLock) + public static PropertiesEnchantmentRegistry GetEnchantmentBySpell(this ICollection value, int spellId, uint? casterGuid, Object rwLock) { if (value == null) return null; - rwLock.EnterReadLock(); - try + lock (rwLock) { var results = value.Where(e => e.SpellId == spellId); @@ -73,58 +57,39 @@ public static PropertiesEnchantmentRegistry GetEnchantmentBySpell(this ICollecti return results.FirstOrDefault(); } - finally - { - rwLock.ExitReadLock(); - } - } + } - public static PropertiesEnchantmentRegistry GetEnchantmentBySpellSet(this ICollection value, int spellId, EquipmentSet spellSetId, ReaderWriterLockSlim rwLock) + public static PropertiesEnchantmentRegistry GetEnchantmentBySpellSet(this ICollection value, int spellId, EquipmentSet spellSetId, Object rwLock) { if (value == null) return null; - rwLock.EnterReadLock(); - try + lock (rwLock) { return value.FirstOrDefault(e => e.SpellId == spellId && e.SpellSetId == spellSetId); } - finally - { - rwLock.ExitReadLock(); - } } - public static List GetEnchantmentsByCategory(this ICollection value, SpellCategory spellCategory, ReaderWriterLockSlim rwLock) + public static List GetEnchantmentsByCategory(this ICollection value, SpellCategory spellCategory, Object rwLock) { if (value == null) return null; - rwLock.EnterReadLock(); - try + lock (rwLock) { return value.Where(e => e.SpellCategory == spellCategory).ToList(); } - finally - { - rwLock.ExitReadLock(); - } } - public static List GetEnchantmentsByStatModType(this ICollection value, EnchantmentTypeFlags statModType, ReaderWriterLockSlim rwLock) + public static List GetEnchantmentsByStatModType(this ICollection value, EnchantmentTypeFlags statModType, Object rwLock) { if (value == null) return null; - rwLock.EnterReadLock(); - try + lock (rwLock) { return value.Where(e => (e.StatModType & statModType) == statModType).ToList(); } - finally - { - rwLock.ExitReadLock(); - } } // this ensures level 8 item self spells always take precedence over level 8 item other spells @@ -138,13 +103,12 @@ public static List GetEnchantmentsByStatModType(t (int)SpellId.HermeticLinkSelf8, }; - public static List GetEnchantmentsTopLayer(this ICollection value, ReaderWriterLockSlim rwLock, HashSet setSpells) + public static List GetEnchantmentsTopLayer(this ICollection value, Object rwLock, HashSet setSpells) { if (value == null) return null; - rwLock.EnterReadLock(); - try + lock (rwLock) { var results = from e in value group e by e.SpellCategory @@ -156,22 +120,17 @@ select categories.OrderByDescending(c => c.PowerLevel) return results.ToList(); } - finally - { - rwLock.ExitReadLock(); - } } /// /// Returns the top layers in each spell category for a StatMod type /// - public static List GetEnchantmentsTopLayerByStatModType(this ICollection value, EnchantmentTypeFlags statModType, ReaderWriterLockSlim rwLock, HashSet setSpells) + public static List GetEnchantmentsTopLayerByStatModType(this ICollection value, EnchantmentTypeFlags statModType, Object rwLock, HashSet setSpells) { if (value == null) return null; - rwLock.EnterReadLock(); - try + lock (rwLock) { var valuesByStatModType = value.Where(e => (e.StatModType & statModType) == statModType); @@ -185,22 +144,17 @@ select categories.OrderByDescending(c => c.PowerLevel) return results.ToList(); } - finally - { - rwLock.ExitReadLock(); - } } /// /// Returns the top layers in each spell category for a StatMod type + key /// - public static List GetEnchantmentsTopLayerByStatModType(this ICollection value, EnchantmentTypeFlags statModType, uint statModKey, ReaderWriterLockSlim rwLock, HashSet setSpells, bool handleMultiple = false) + public static List GetEnchantmentsTopLayerByStatModType(this ICollection value, EnchantmentTypeFlags statModType, uint statModKey, Object rwLock, HashSet setSpells, bool handleMultiple = false) { if (value == null) return null; - rwLock.EnterReadLock(); - try + lock (rwLock) { var multipleStat = EnchantmentTypeFlags.Undef; @@ -230,19 +184,14 @@ select categories.OrderByDescending(c => c.PowerLevel) return results.ToList(); } - finally - { - rwLock.ExitReadLock(); - } } - public static List HeartBeatEnchantmentsAndReturnExpired(this ICollection value, double heartbeatInterval, ReaderWriterLockSlim rwLock) + public static List HeartBeatEnchantmentsAndReturnExpired(this ICollection value, double heartbeatInterval, Object rwLock) { if (value == null) return null; - rwLock.EnterReadLock(); - try + lock (rwLock) { var expired = new List(); @@ -257,32 +206,22 @@ public static List HeartBeatEnchantmentsAndReturn return expired; } - finally - { - rwLock.ExitReadLock(); - } } - public static void AddEnchantment(this ICollection value, PropertiesEnchantmentRegistry entity, ReaderWriterLockSlim rwLock) + public static void AddEnchantment(this ICollection value, PropertiesEnchantmentRegistry entity, Object rwLock) { - rwLock.EnterWriteLock(); - try + lock (rwLock) { value.Add(entity); } - finally - { - rwLock.ExitWriteLock(); - } } - public static bool TryRemoveEnchantment(this ICollection value, int spellId, uint casterObjectId, ReaderWriterLockSlim rwLock) + public static bool TryRemoveEnchantment(this ICollection value, int spellId, uint casterObjectId, Object rwLock) { if (value == null) return false; - rwLock.EnterWriteLock(); - try + lock (rwLock) { var entity = value.FirstOrDefault(x => x.SpellId == spellId && x.CasterObjectId == casterObjectId); @@ -295,29 +234,20 @@ public static bool TryRemoveEnchantment(this ICollection value, IEnumerable spellsToExclude, ReaderWriterLockSlim rwLock) + public static void RemoveAllEnchantments(this ICollection value, IEnumerable spellsToExclude, Object rwLock) { if (value == null) return; - rwLock.EnterWriteLock(); - try + lock (rwLock) { var enchantments = value.Where(e => !spellsToExclude.Contains(e.SpellId)).ToList(); foreach (var enchantment in enchantments) value.Remove(enchantment); } - finally - { - rwLock.ExitWriteLock(); - } } } } diff --git a/Source/ACE.Entity/Models/PropertiesPaletteExtensions.cs b/Source/ACE.Entity/Models/PropertiesPaletteExtensions.cs index b2596c5b56..2130a42181 100644 --- a/Source/ACE.Entity/Models/PropertiesPaletteExtensions.cs +++ b/Source/ACE.Entity/Models/PropertiesPaletteExtensions.cs @@ -6,53 +6,38 @@ namespace ACE.Entity.Models { public static class PropertiesPaletteExtensions { - public static int GetCount(this IList value, ReaderWriterLockSlim rwLock) + public static int GetCount(this IList value, Object rwLock) { if (value == null) return 0; - rwLock.EnterReadLock(); - try + lock (rwLock) { return value.Count; } - finally - { - rwLock.ExitReadLock(); - } } - public static List Clone(this IList value, ReaderWriterLockSlim rwLock) + public static List Clone(this IList value, Object rwLock) { if (value == null) return null; - rwLock.EnterReadLock(); - try + lock (rwLock) { return new List(value); } - finally - { - rwLock.ExitReadLock(); - } } - public static void CopyTo(this IList value, ICollection destination, ReaderWriterLockSlim rwLock) + public static void CopyTo(this IList value, ICollection destination, Object rwLock) { if (value == null) return; - rwLock.EnterReadLock(); - try + lock (rwLock) { foreach (var entry in value) destination.Add(entry); } - finally - { - rwLock.ExitReadLock(); - } } } } diff --git a/Source/ACE.Entity/Models/PropertiesTextureMapExtensions.cs b/Source/ACE.Entity/Models/PropertiesTextureMapExtensions.cs index 69407191ac..4a81f919f0 100644 --- a/Source/ACE.Entity/Models/PropertiesTextureMapExtensions.cs +++ b/Source/ACE.Entity/Models/PropertiesTextureMapExtensions.cs @@ -6,53 +6,38 @@ namespace ACE.Entity.Models { public static class PropertiesTextureMapExtensions { - public static int GetCount(this IList value, ReaderWriterLockSlim rwLock) + public static int GetCount(this IList value, Object rwLock) { if (value == null) return 0; - rwLock.EnterReadLock(); - try + lock (rwLock) { return value.Count; } - finally - { - rwLock.ExitReadLock(); - } } - public static List Clone(this IList value, ReaderWriterLockSlim rwLock) + public static List Clone(this IList value, Object rwLock) { if (value == null) return null; - rwLock.EnterReadLock(); - try + lock (rwLock) { return new List(value); } - finally - { - rwLock.ExitReadLock(); - } } - public static void CopyTo(this IList value, ICollection destination, ReaderWriterLockSlim rwLock) + public static void CopyTo(this IList value, ICollection destination, Object rwLock) { if (value == null) return; - rwLock.EnterReadLock(); - try + lock (rwLock) { foreach (var entry in value) destination.Add(entry); } - finally - { - rwLock.ExitReadLock(); - } } } } diff --git a/Source/ACE.Server/Command/Handlers/AdminCommands.cs b/Source/ACE.Server/Command/Handlers/AdminCommands.cs index 6a5b579f00..0501be566a 100644 --- a/Source/ACE.Server/Command/Handlers/AdminCommands.cs +++ b/Source/ACE.Server/Command/Handlers/AdminCommands.cs @@ -2125,7 +2125,7 @@ private static void DoCopyChar(Session session, string existingCharName, uint ex } var possessions = newPlayer.GetAllPossessions(); - var possessedBiotas = new Collection<(Biota biota, ReaderWriterLockSlim rwLock)>(); + var possessedBiotas = new Collection<(Biota biota, Object rwLock)>(); foreach (var possession in possessions) possessedBiotas.Add((possession.Biota, possession.BiotaDatabaseLock)); @@ -3525,7 +3525,7 @@ public static void HandleMorph(Session session, params string[] parameters) player.GenerateNewFace(); var possessions = player.GetAllPossessions(); - var possessedBiotas = new Collection<(Biota biota, ReaderWriterLockSlim rwLock)>(); + var possessedBiotas = new Collection<(Biota biota, Object rwLock)>(); foreach (var possession in possessions) possessedBiotas.Add((possession.Biota, possession.BiotaDatabaseLock)); diff --git a/Source/ACE.Server/Entity/Landblock.cs b/Source/ACE.Server/Entity/Landblock.cs index f192a5f1df..d22e7b75d5 100644 --- a/Source/ACE.Server/Entity/Landblock.cs +++ b/Source/ACE.Server/Entity/Landblock.cs @@ -1169,7 +1169,7 @@ public void DestroyAllNonPlayerObjects() private void SaveDB() { - var biotas = new Collection<(Biota biota, ReaderWriterLockSlim rwLock)>(); + var biotas = new Collection<(Biota biota, Object rwLock)>(); foreach (var wo in worldObjects.Values) { @@ -1180,7 +1180,7 @@ private void SaveDB() DatabaseManager.Shard.SaveBiotasInParallel(biotas, null); } - private void AddWorldObjectToBiotasSaveCollection(WorldObject wo, Collection<(Biota biota, ReaderWriterLockSlim rwLock)> biotas) + private void AddWorldObjectToBiotasSaveCollection(WorldObject wo, Collection<(Biota biota, Object rwLock)> biotas) { if (wo.ChangesDetected) { diff --git a/Source/ACE.Server/Entity/OfflinePlayer.cs b/Source/ACE.Server/Entity/OfflinePlayer.cs index a0e597dfe3..f6083f28ab 100644 --- a/Source/ACE.Server/Entity/OfflinePlayer.cs +++ b/Source/ACE.Server/Entity/OfflinePlayer.cs @@ -47,7 +47,7 @@ public OfflinePlayer(Biota biota) public bool ChangesDetected { get; set; } - public readonly ReaderWriterLockSlim BiotaDatabaseLock = new ReaderWriterLockSlim(); + public readonly Object BiotaDatabaseLock = new Object(); /// /// This will set the LastRequestedDatabaseSave to UtcNow and ChangesDetected to false. diff --git a/Source/ACE.Server/Managers/PlayerManager.cs b/Source/ACE.Server/Managers/PlayerManager.cs index f126e5c049..20347965ba 100644 --- a/Source/ACE.Server/Managers/PlayerManager.cs +++ b/Source/ACE.Server/Managers/PlayerManager.cs @@ -119,7 +119,7 @@ public static void SaveOfflinePlayersWithChanges() { lastDatabaseSave = DateTime.UtcNow; - var biotas = new Collection<(Biota biota, ReaderWriterLockSlim rwLock)>(); + var biotas = new Collection<(Biota biota, Object rwLock)>(); playersLock.EnterReadLock(); try diff --git a/Source/ACE.Server/Network/Handlers/CharacterHandler.cs b/Source/ACE.Server/Network/Handlers/CharacterHandler.cs index c3a62b28dc..3063d71a1a 100644 --- a/Source/ACE.Server/Network/Handlers/CharacterHandler.cs +++ b/Source/ACE.Server/Network/Handlers/CharacterHandler.cs @@ -1,3 +1,4 @@ +using System; using System.Collections.ObjectModel; using System.Linq; using System.Threading; @@ -153,7 +154,7 @@ private static void CharacterCreateEx(ClientMessage message, Session session) } var possessions = player.GetAllPossessions(); - var possessedBiotas = new Collection<(Biota biota, ReaderWriterLockSlim rwLock)>(); + var possessedBiotas = new Collection<(Biota biota, Object rwLock)>(); foreach (var possession in possessions) possessedBiotas.Add((possession.Biota, possession.BiotaDatabaseLock)); diff --git a/Source/ACE.Server/WorldObjects/Player_Character.cs b/Source/ACE.Server/WorldObjects/Player_Character.cs index 9af5be8d2f..96cd454c65 100644 --- a/Source/ACE.Server/WorldObjects/Player_Character.cs +++ b/Source/ACE.Server/WorldObjects/Player_Character.cs @@ -79,44 +79,29 @@ private void SetCharacterOptions2(CharacterOptions2 option, bool value) public void SetCharacterOptions1(int value) { - CharacterDatabaseLock.EnterWriteLock(); - try + lock (CharacterDatabaseLock) { Character.CharacterOptions1 = value; CharacterChangesDetected = true; } - finally - { - CharacterDatabaseLock.ExitWriteLock(); - } } public void SetCharacterOptions2(int value) { - CharacterDatabaseLock.EnterWriteLock(); - try + lock (CharacterDatabaseLock) { Character.CharacterOptions2 = value; CharacterChangesDetected = true; } - finally - { - CharacterDatabaseLock.ExitWriteLock(); - } } public void SetCharacterGameplayOptions(byte[] value) { - CharacterDatabaseLock.EnterWriteLock(); - try + lock (CharacterDatabaseLock) { Character.GameplayOptions = value; CharacterChangesDetected = true; } - finally - { - CharacterDatabaseLock.ExitWriteLock(); - } } diff --git a/Source/ACE.Server/WorldObjects/Player_Database.cs b/Source/ACE.Server/WorldObjects/Player_Database.cs index 0b40b67541..2150126c27 100644 --- a/Source/ACE.Server/WorldObjects/Player_Database.cs +++ b/Source/ACE.Server/WorldObjects/Player_Database.cs @@ -52,7 +52,7 @@ public long PlayerSaveIntervalSecs /// The critical thing is that the collections are not added to or removed from while Entity Framework is iterating over them. /// Mag-nus 2018-08-19 /// - public readonly ReaderWriterLockSlim CharacterDatabaseLock = new ReaderWriterLockSlim(); + public readonly Object CharacterDatabaseLock = new Object(); private void SetPropertiesAtLogOut() { @@ -82,7 +82,7 @@ public void SavePlayerToDatabase() if (CharacterChangesDetected) SaveCharacterToDatabase(); - var biotas = new Collection<(Biota biota, ReaderWriterLockSlim rwLock)>(); + var biotas = new Collection<(Biota biota, Object rwLock)>(); SaveBiotaToDatabase(false); biotas.Add((Biota, BiotaDatabaseLock)); diff --git a/Source/ACE.Server/WorldObjects/Player_Inventory.cs b/Source/ACE.Server/WorldObjects/Player_Inventory.cs index c66ffcc4ac..ab3eab4656 100644 --- a/Source/ACE.Server/WorldObjects/Player_Inventory.cs +++ b/Source/ACE.Server/WorldObjects/Player_Inventory.cs @@ -167,7 +167,7 @@ public bool TryConsumeFromInventoryWithNetworking(uint wcid, int amount = int.Ma private void DeepSave(WorldObject item) { - var biotas = new Collection<(Biota biota, ReaderWriterLockSlim rwLock)>(); + var biotas = new Collection<(Biota biota, Object rwLock)>(); if (item.ChangesDetected) { diff --git a/Source/ACE.Server/WorldObjects/Player_Trade.cs b/Source/ACE.Server/WorldObjects/Player_Trade.cs index f1f71acf66..5709455cb6 100644 --- a/Source/ACE.Server/WorldObjects/Player_Trade.cs +++ b/Source/ACE.Server/WorldObjects/Player_Trade.cs @@ -1,3 +1,4 @@ +using System; using System.Collections.Generic; using System.Collections.ObjectModel; using System.Linq; @@ -229,7 +230,7 @@ private void FinalizeTrade(Player target) Session.Network.EnqueueSend(new GameEventCommunicationTransientString(Session, "The items are being traded")); target.Session.Network.EnqueueSend(new GameEventCommunicationTransientString(target.Session, "The items are being traded")); - var tradedItems = new Collection<(Biota biota, ReaderWriterLockSlim rwLock)>(); + var tradedItems = new Collection<(Biota biota, Object rwLock)>(); var myEscrow = new List(); var targetEscrow = new List(); diff --git a/Source/ACE.Server/WorldObjects/WorldObject_Database.cs b/Source/ACE.Server/WorldObjects/WorldObject_Database.cs index e2a3f91e50..f944f9bacd 100644 --- a/Source/ACE.Server/WorldObjects/WorldObject_Database.cs +++ b/Source/ACE.Server/WorldObjects/WorldObject_Database.cs @@ -33,7 +33,7 @@ partial class WorldObject /// The critical thing is that the collections are not added to or removed from while Entity Framework is iterating over them. /// Mag-nus 2018-08-19 /// - public readonly ReaderWriterLockSlim BiotaDatabaseLock = new ReaderWriterLockSlim(); + public readonly Object BiotaDatabaseLock = new Object(); public bool BiotaOriginatedFromOrHasBeenSavedToDatabase() { diff --git a/Source/ACE.Server/WorldObjects/WorldObject_Properties.cs b/Source/ACE.Server/WorldObjects/WorldObject_Properties.cs index 1619f4b759..bc6ec6673a 100644 --- a/Source/ACE.Server/WorldObjects/WorldObject_Properties.cs +++ b/Source/ACE.Server/WorldObjects/WorldObject_Properties.cs @@ -309,8 +309,7 @@ public Dictionary GetAllPropertyBools() { var results = new Dictionary(); - BiotaDatabaseLock.EnterReadLock(); - try + lock (BiotaDatabaseLock) { if (Biota.PropertiesBool != null) { @@ -318,10 +317,6 @@ public Dictionary GetAllPropertyBools() results[kvp.Key] = kvp.Value; } } - finally - { - BiotaDatabaseLock.ExitReadLock(); - } if (ephemeralPropertyBools != null) { @@ -341,8 +336,7 @@ public Dictionary GetAllPropertyDataId() { var results = new Dictionary(); - BiotaDatabaseLock.EnterReadLock(); - try + lock (BiotaDatabaseLock) { if (Biota.PropertiesDID != null) { @@ -350,10 +344,6 @@ public Dictionary GetAllPropertyDataId() results[kvp.Key] = kvp.Value; } } - finally - { - BiotaDatabaseLock.ExitReadLock(); - } if (ephemeralPropertyDataIds != null) { @@ -373,8 +363,7 @@ public Dictionary GetAllPropertyFloat() { var results = new Dictionary(); - BiotaDatabaseLock.EnterReadLock(); - try + lock (BiotaDatabaseLock) { if (Biota.PropertiesFloat != null) { @@ -382,10 +371,6 @@ public Dictionary GetAllPropertyFloat() results[kvp.Key] = kvp.Value; } } - finally - { - BiotaDatabaseLock.ExitReadLock(); - } if (ephemeralPropertyFloats != null) { @@ -405,8 +390,7 @@ public Dictionary GetAllPropertyInstanceId() { var results = new Dictionary(); - BiotaDatabaseLock.EnterReadLock(); - try + lock (BiotaDatabaseLock) { if (Biota.PropertiesIID != null) { @@ -414,10 +398,6 @@ public Dictionary GetAllPropertyInstanceId() results[kvp.Key] = kvp.Value; } } - finally - { - BiotaDatabaseLock.ExitReadLock(); - } if (ephemeralPropertyInstanceIds != null) { @@ -437,8 +417,7 @@ public Dictionary GetAllPropertyInt() { var results = new Dictionary(); - BiotaDatabaseLock.EnterReadLock(); - try + lock (BiotaDatabaseLock) { if (Biota.PropertiesInt != null) { @@ -446,10 +425,6 @@ public Dictionary GetAllPropertyInt() results[kvp.Key] = kvp.Value; } } - finally - { - BiotaDatabaseLock.ExitReadLock(); - } if (ephemeralPropertyInts != null) { @@ -469,8 +444,7 @@ public Dictionary GetAllPropertyInt64() { var results = new Dictionary(); - BiotaDatabaseLock.EnterReadLock(); - try + lock (BiotaDatabaseLock) { if (Biota.PropertiesInt64 != null) { @@ -478,10 +452,6 @@ public Dictionary GetAllPropertyInt64() results[kvp.Key] = kvp.Value; } } - finally - { - BiotaDatabaseLock.ExitReadLock(); - } if (ephemeralPropertyInt64s != null) { @@ -501,8 +471,7 @@ public Dictionary GetAllPropertyString() { var results = new Dictionary(); - BiotaDatabaseLock.EnterReadLock(); - try + lock (BiotaDatabaseLock) { if (Biota.PropertiesString != null) { @@ -510,10 +479,6 @@ public Dictionary GetAllPropertyString() results[kvp.Key] = kvp.Value; } } - finally - { - BiotaDatabaseLock.ExitReadLock(); - } if (ephemeralPropertyStrings != null) { @@ -535,8 +500,7 @@ public Dictionary GetAllPropertyBoolsWhere(HashSet k { var results = new Dictionary(); - BiotaDatabaseLock.EnterReadLock(); - try + lock (BiotaDatabaseLock) { if (Biota.PropertiesBool != null) { @@ -544,10 +508,6 @@ public Dictionary GetAllPropertyBoolsWhere(HashSet k results[kvp.Key] = kvp.Value; } } - finally - { - BiotaDatabaseLock.ExitReadLock(); - } if (ephemeralPropertyBools != null) { @@ -567,8 +527,7 @@ public Dictionary GetAllPropertyDataIdWhere(HashSet(); - BiotaDatabaseLock.EnterReadLock(); - try + lock (BiotaDatabaseLock) { if (Biota.PropertiesDID != null) { @@ -576,10 +535,6 @@ public Dictionary GetAllPropertyDataIdWhere(HashSet GetAllPropertyFloatWhere(HashSet(); - BiotaDatabaseLock.EnterReadLock(); - try + lock (BiotaDatabaseLock) { if (Biota.PropertiesFloat != null) { @@ -608,10 +562,6 @@ public Dictionary GetAllPropertyFloatWhere(HashSet GetAllPropertyInstanceIdWhere(HashSe { var results = new Dictionary(); - BiotaDatabaseLock.EnterReadLock(); - try + lock (BiotaDatabaseLock) { if (Biota.PropertiesIID != null) { @@ -640,10 +589,6 @@ public Dictionary GetAllPropertyInstanceIdWhere(HashSe results[kvp.Key] = kvp.Value; } } - finally - { - BiotaDatabaseLock.ExitReadLock(); - } if (ephemeralPropertyInstanceIds != null) { @@ -663,8 +608,7 @@ public Dictionary GetAllPropertyIntWhere(HashSet keys) { var results = new Dictionary(); - BiotaDatabaseLock.EnterReadLock(); - try + lock (BiotaDatabaseLock) { if (Biota.PropertiesInt != null) { @@ -672,10 +616,6 @@ public Dictionary GetAllPropertyIntWhere(HashSet keys) results[kvp.Key] = kvp.Value; } } - finally - { - BiotaDatabaseLock.ExitReadLock(); - } if (ephemeralPropertyInts != null) { @@ -695,8 +635,7 @@ public Dictionary GetAllPropertyInt64Where(HashSet { var results = new Dictionary(); - BiotaDatabaseLock.EnterReadLock(); - try + lock (BiotaDatabaseLock) { if (Biota.PropertiesInt64 != null) { @@ -704,10 +643,6 @@ public Dictionary GetAllPropertyInt64Where(HashSet results[kvp.Key] = kvp.Value; } } - finally - { - BiotaDatabaseLock.ExitReadLock(); - } if (ephemeralPropertyInt64s != null) { @@ -727,8 +662,7 @@ public Dictionary GetAllPropertyStringWhere(HashSet(); - BiotaDatabaseLock.EnterReadLock(); - try + lock (BiotaDatabaseLock) { if (Biota.PropertiesString != null) { @@ -736,10 +670,6 @@ public Dictionary GetAllPropertyStringWhere(HashSet GetAllPositions() { var results = new Dictionary(); - BiotaDatabaseLock.EnterReadLock(); - try + lock (BiotaDatabaseLock) { foreach (var kvp in Biota.PropertiesPosition) results[kvp.Key] = new Position(kvp.Value.ObjCellId, kvp.Value.PositionX, kvp.Value.PositionY, kvp.Value.PositionZ, kvp.Value.RotationX, kvp.Value.RotationY, kvp.Value.RotationZ, kvp.Value.RotationW); } - finally - { - BiotaDatabaseLock.ExitReadLock(); - } foreach (var property in ephemeralPositions) {