From 1e1d0d8204181e4dedf438332625941cd56b02c2 Mon Sep 17 00:00:00 2001 From: UbitUmarov Date: Wed, 9 Nov 2016 20:09:49 +0000 Subject: [PATCH] move UserAccountCache access locking to its methods and not callers. --- .../LocalUserAccountServiceConnector.cs | 18 ++---- .../RemoteUserAccountServiceConnector.cs | 27 +++----- .../UserAccounts/UserAccountCache.cs | 64 +++++++++++-------- 3 files changed, 54 insertions(+), 55 deletions(-) diff --git a/OpenSim/Region/CoreModules/ServiceConnectorsOut/UserAccounts/LocalUserAccountServiceConnector.cs b/OpenSim/Region/CoreModules/ServiceConnectorsOut/UserAccounts/LocalUserAccountServiceConnector.cs index 3127199586..a413a8bff1 100644 --- a/OpenSim/Region/CoreModules/ServiceConnectorsOut/UserAccounts/LocalUserAccountServiceConnector.cs +++ b/OpenSim/Region/CoreModules/ServiceConnectorsOut/UserAccounts/LocalUserAccountServiceConnector.cs @@ -154,14 +154,12 @@ namespace OpenSim.Region.CoreModules.ServiceConnectorsOut.UserAccounts { bool inCache = false; UserAccount account; - lock(m_Cache) - account = m_Cache.Get(userID, out inCache); + account = m_Cache.Get(userID, out inCache); if (inCache) return account; account = UserAccountService.GetUserAccount(scopeID, userID); - lock(m_Cache) - m_Cache.Cache(userID, account); + m_Cache.Cache(userID, account); return account; } @@ -170,15 +168,13 @@ namespace OpenSim.Region.CoreModules.ServiceConnectorsOut.UserAccounts { bool inCache = false; UserAccount account; - lock(m_Cache) - account = m_Cache.Get(firstName + " " + lastName, out inCache); + account = m_Cache.Get(firstName + " " + lastName, out inCache); if (inCache) return account; account = UserAccountService.GetUserAccount(scopeID, firstName, lastName); if (account != null) - lock(m_Cache) - m_Cache.Cache(account.PrincipalID, account); + m_Cache.Cache(account.PrincipalID, account); return account; } @@ -201,8 +197,7 @@ namespace OpenSim.Region.CoreModules.ServiceConnectorsOut.UserAccounts { if(UUID.TryParse(id, out uuid)) { - lock(m_Cache) - account = m_Cache.Get(uuid, out inCache); + account = m_Cache.Get(uuid, out inCache); if (inCache) ret.Add(account); else @@ -220,8 +215,7 @@ namespace OpenSim.Region.CoreModules.ServiceConnectorsOut.UserAccounts foreach(UserAccount acc in ext) { if(acc != null) - lock(m_Cache) - m_Cache.Cache(acc.PrincipalID, acc); + m_Cache.Cache(acc.PrincipalID, acc); } } return ret; diff --git a/OpenSim/Region/CoreModules/ServiceConnectorsOut/UserAccounts/RemoteUserAccountServiceConnector.cs b/OpenSim/Region/CoreModules/ServiceConnectorsOut/UserAccounts/RemoteUserAccountServiceConnector.cs index 9140d786f5..60dd97ad90 100644 --- a/OpenSim/Region/CoreModules/ServiceConnectorsOut/UserAccounts/RemoteUserAccountServiceConnector.cs +++ b/OpenSim/Region/CoreModules/ServiceConnectorsOut/UserAccounts/RemoteUserAccountServiceConnector.cs @@ -128,8 +128,7 @@ namespace OpenSim.Region.CoreModules.ServiceConnectorsOut.UserAccounts // flags, title, etc. And country, don't forget country! private void OnNewClient(IClientAPI client) { - lock(m_Cache) - m_Cache.Remove(client.Name); + m_Cache.Remove(client.Name); } #region Overwritten methods from IUserAccountService @@ -138,17 +137,14 @@ namespace OpenSim.Region.CoreModules.ServiceConnectorsOut.UserAccounts { bool inCache = false; UserAccount account; - lock(m_Cache) - account = m_Cache.Get(userID, out inCache); + account = m_Cache.Get(userID, out inCache); if (inCache) return account; account = base.GetUserAccount(scopeID, userID); if(account != null) - { - lock(m_Cache) - m_Cache.Cache(userID, account); - } + m_Cache.Cache(userID, account); + return account; } @@ -156,17 +152,14 @@ namespace OpenSim.Region.CoreModules.ServiceConnectorsOut.UserAccounts { bool inCache = false; UserAccount account; - lock(m_Cache) - account = m_Cache.Get(firstName + " " + lastName, out inCache); + account = m_Cache.Get(firstName + " " + lastName, out inCache); if (inCache) return account; account = base.GetUserAccount(scopeID, firstName, lastName); if (account != null) - { - lock(m_Cache) - m_Cache.Cache(account.PrincipalID, account); - } + m_Cache.Cache(account.PrincipalID, account); + return account; } @@ -183,8 +176,7 @@ namespace OpenSim.Region.CoreModules.ServiceConnectorsOut.UserAccounts { if(UUID.TryParse(id, out uuid)) { - lock(m_Cache) - account = m_Cache.Get(uuid, out inCache); + account = m_Cache.Get(uuid, out inCache); if (inCache) accs.Add(account); else @@ -202,8 +194,7 @@ namespace OpenSim.Region.CoreModules.ServiceConnectorsOut.UserAccounts if(acc != null) { accs.Add(acc); - lock(m_Cache) - m_Cache.Cache(acc.PrincipalID, acc); + m_Cache.Cache(acc.PrincipalID, acc); } } } diff --git a/OpenSim/Region/CoreModules/ServiceConnectorsOut/UserAccounts/UserAccountCache.cs b/OpenSim/Region/CoreModules/ServiceConnectorsOut/UserAccounts/UserAccountCache.cs index 53610d95af..97baf8703a 100644 --- a/OpenSim/Region/CoreModules/ServiceConnectorsOut/UserAccounts/UserAccountCache.cs +++ b/OpenSim/Region/CoreModules/ServiceConnectorsOut/UserAccounts/UserAccountCache.cs @@ -44,6 +44,7 @@ namespace OpenSim.Region.CoreModules.ServiceConnectorsOut.UserAccounts private ExpiringCache m_UUIDCache; private ExpiringCache m_NameCache; + private object accessLock = new object(); public UserAccountCache() { @@ -54,60 +55,73 @@ namespace OpenSim.Region.CoreModules.ServiceConnectorsOut.UserAccounts public void Cache(UUID userID, UserAccount account) { // Cache even null accounts - m_UUIDCache.AddOrUpdate(userID, account, CACHE_EXPIRATION_SECONDS); - if (account != null) - m_NameCache.AddOrUpdate(account.Name, account.PrincipalID, CACHE_EXPIRATION_SECONDS); + lock(accessLock) + { + m_UUIDCache.AddOrUpdate(userID, account, CACHE_EXPIRATION_SECONDS); + if (account != null) + m_NameCache.AddOrUpdate(account.Name, account.PrincipalID, CACHE_EXPIRATION_SECONDS); //m_log.DebugFormat("[USER CACHE]: cached user {0}", userID); + } } public void Invalidate(UUID userID) { - m_UUIDCache.Remove(userID); + lock(accessLock) + m_UUIDCache.Remove(userID); } public UserAccount Get(UUID userID, out bool inCache) { UserAccount account = null; inCache = false; - if (m_UUIDCache.TryGetValue(userID, out account)) + lock(accessLock) { - //m_log.DebugFormat("[USER CACHE]: Account {0} {1} found in cache", account.FirstName, account.LastName); - inCache = true; - return account; + if (m_UUIDCache.TryGetValue(userID, out account)) + { + //m_log.DebugFormat("[USER CACHE]: Account {0} {1} found in cache", account.FirstName, account.LastName); + inCache = true; + return account; + } } - return null; } public UserAccount Get(string name, out bool inCache) { inCache = false; - if (!m_NameCache.Contains(name)) - return null; + lock(accessLock) + { + if (!m_NameCache.Contains(name)) + return null; - UserAccount account = null; - UUID uuid = UUID.Zero; - if (m_NameCache.TryGetValue(name, out uuid)) - if (m_UUIDCache.TryGetValue(uuid, out account)) + UserAccount account = null; + UUID uuid = UUID.Zero; + if (m_NameCache.TryGetValue(name, out uuid)) { - inCache = true; - return account; + if (m_UUIDCache.TryGetValue(uuid, out account)) + { + inCache = true; + return account; + } } - + } return null; } public void Remove(string name) { - if (!m_NameCache.Contains(name)) - return; - - UUID uuid = UUID.Zero; - if (m_NameCache.TryGetValue(name, out uuid)) + lock(accessLock) { - m_NameCache.Remove(name); - m_UUIDCache.Remove(uuid); + if (!m_NameCache.Contains(name)) + return; + + UUID uuid = UUID.Zero; + if (m_NameCache.TryGetValue(name, out uuid)) + { + m_NameCache.Remove(name); + m_UUIDCache.Remove(uuid); + } } } }