more on ReaderWriterLockSlim and thread.abort

This commit is contained in:
UbitUmarov
2025-10-18 22:07:51 +01:00
parent d6c83aee6b
commit c69eca6911
3 changed files with 90 additions and 513 deletions

View File

@@ -67,10 +67,7 @@ namespace OpenSim.Framework
[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]
private void CheckTimer()
{
if (m_purgeTimer == null)
{
m_purgeTimer = new Timer(Purge, null, m_expire, Timeout.Infinite);
}
m_purgeTimer ??= new Timer(Purge, null, m_expire, Timeout.Infinite);
}
[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]
@@ -106,17 +103,9 @@ namespace OpenSim.Framework
private void Purge(object ignored)
{
bool gotLock = false;
m_rwLock.EnterUpgradeableReadLock();
try
{
try { }
finally
{
m_rwLock.EnterUpgradeableReadLock();
gotLock = true;
}
if (m_expireControl.Count == 0)
{
DisposeTimer();
@@ -134,16 +123,9 @@ namespace OpenSim.Framework
if(expired.Count > 0)
{
bool gotWriteLock = false;
m_rwLock.EnterWriteLock();
try
{
try { }
finally
{
m_rwLock.EnterWriteLock();
gotWriteLock = true;
}
valuesArrayCache = null;
foreach (TKey1 key in expired)
{
@@ -151,11 +133,8 @@ namespace OpenSim.Framework
m_values.Remove(key);
}
}
finally
{
if (gotWriteLock)
m_rwLock.ExitWriteLock();
}
finally { m_rwLock.ExitWriteLock(); }
if (m_expireControl.Count == 0)
DisposeTimer();
else
@@ -164,11 +143,7 @@ namespace OpenSim.Framework
else
m_purgeTimer.Change(m_expire, Timeout.Infinite);
}
finally
{
if (gotLock)
m_rwLock.ExitUpgradeableReadLock();
}
finally { m_rwLock.ExitUpgradeableReadLock(); }
}
[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]
@@ -179,28 +154,17 @@ namespace OpenSim.Framework
public void Add(TKey1 key, TValue1 val)
{
bool gotLock = false;
int now = (int)(Util.GetTimeStampMS() - m_startTS) + m_expire;
m_rwLock.EnterWriteLock();
try
{
try { }
finally
{
m_rwLock.EnterWriteLock();
gotLock = true;
}
m_expireControl[key] = now;
m_values[key] = val;
valuesArrayCache = null;
CheckTimer();
}
finally
{
if (gotLock)
m_rwLock.ExitWriteLock();
}
finally { m_rwLock.ExitWriteLock(); }
}
[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]
@@ -217,7 +181,6 @@ namespace OpenSim.Framework
public void Add(TKey1 key, TValue1 val, int expireMS)
{
bool gotLock = false;
int now;
if (expireMS > 0)
{
@@ -227,78 +190,44 @@ namespace OpenSim.Framework
else
now = int.MinValue;
m_rwLock.EnterWriteLock();
try
{
try { }
finally
{
m_rwLock.EnterWriteLock();
gotLock = true;
}
m_expireControl[key] = now;
m_values[key] = val;
valuesArrayCache = null;
CheckTimer();
}
finally
{
if (gotLock)
m_rwLock.ExitWriteLock();
}
finally { m_rwLock.ExitWriteLock(); }
}
public bool Remove(TKey1 key)
{
bool success;
bool gotLock = false;
m_rwLock.EnterWriteLock();
try
{
try {}
finally
{
m_rwLock.EnterWriteLock();
gotLock = true;
}
success = m_expireControl.Remove(key);
bool success = m_expireControl.Remove(key);
success |= m_values.Remove(key);
if(success)
valuesArrayCache = null;
if (m_expireControl.Count == 0)
DisposeTimer();
return success;
}
finally
{
if (gotLock)
m_rwLock.ExitWriteLock();
}
return success;
finally { m_rwLock.ExitWriteLock(); }
}
public void Clear()
{
bool gotLock = false;
m_rwLock.EnterWriteLock();
try
{
try {}
finally
{
m_rwLock.EnterWriteLock();
gotLock = true;
}
DisposeTimer();
m_expireControl.Clear();
m_values.Clear();
valuesArrayCache = null;
}
finally
{
if (gotLock)
m_rwLock.ExitWriteLock();
}
finally { m_rwLock.ExitWriteLock(); }
}
public int Count
@@ -314,22 +243,12 @@ namespace OpenSim.Framework
public bool ContainsKey(TKey1 key)
{
bool gotLock = false;
m_rwLock.EnterReadLock();
try
{
try { }
finally
{
m_rwLock.EnterReadLock();
gotLock = true;
}
return m_expireControl.ContainsKey(key);
}
finally
{
if (gotLock)
m_rwLock.ExitReadLock();
}
finally { m_rwLock.ExitReadLock(); }
}
[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]
@@ -340,26 +259,14 @@ namespace OpenSim.Framework
public bool ContainsKey(TKey1 key, int expireMS)
{
bool gotLock = false;
m_rwLock.EnterUpgradeableReadLock();
try
{
try { }
finally
{
m_rwLock.EnterUpgradeableReadLock();
gotLock = true;
}
if(m_expireControl.ContainsKey(key))
{
bool gotWriteLock = false;
m_rwLock.EnterWriteLock();
try
{
try { }
finally
{
m_rwLock.EnterWriteLock();
gotWriteLock = true;
}
int now;
if(expireMS > 0)
{
@@ -372,68 +279,33 @@ namespace OpenSim.Framework
m_expireControl[key] = now;
return true;
}
finally
{
if (gotWriteLock)
m_rwLock.ExitWriteLock();
}
finally { m_rwLock.ExitWriteLock(); }
}
return false;
}
finally
{
if (gotLock)
m_rwLock.ExitUpgradeableReadLock();
}
finally { m_rwLock.ExitUpgradeableReadLock(); }
}
public bool TryGetValue(TKey1 key, out TValue1 value)
{
bool gotLock = false;
m_rwLock.EnterReadLock();
try
{
try {}
finally
{
m_rwLock.EnterReadLock();
gotLock = true;
}
return m_values.TryGetValue(key, out value);
}
finally
{
if (gotLock)
m_rwLock.ExitReadLock();
}
finally { m_rwLock.ExitReadLock(); }
}
public bool TryGetValue(TKey1 key, int expireMS, out TValue1 value)
{
bool success;
bool gotLock = false;
m_rwLock.EnterUpgradeableReadLock();
try
{
try { }
finally
if(m_values.TryGetValue(key, out value))
{
m_rwLock.EnterUpgradeableReadLock();
gotLock = true;
}
success = m_values.TryGetValue(key, out value);
if(success)
{
bool gotWriteLock = false;
m_rwLock.EnterWriteLock();
try
{
try { }
finally
{
m_rwLock.EnterWriteLock();
gotWriteLock = true;
}
int now;
if(expireMS > 0)
{
@@ -444,56 +316,30 @@ namespace OpenSim.Framework
now = int.MinValue;
m_expireControl[key] = now;
return true;
}
finally
{
if (gotWriteLock)
m_rwLock.ExitWriteLock();
}
finally { m_rwLock.ExitWriteLock(); }
}
return false;
}
finally
{
if (gotLock)
m_rwLock.ExitUpgradeableReadLock();
}
return success;
finally { m_rwLock.ExitUpgradeableReadLock(); }
}
public ref TValue1 TryGetOrDefaultValue(TKey1 key, out bool existed)
{
bool gotLock = false;
m_rwLock.ExitUpgradeableReadLock();
try
{
try { }
finally
{
m_rwLock.ExitUpgradeableReadLock();
gotLock = true;
}
return ref CollectionsMarshal.GetValueRefOrAddDefault(m_values, key, out existed);
}
finally
{
if (gotLock)
m_rwLock.ExitUpgradeableReadLock();
}
finally { m_rwLock.ExitUpgradeableReadLock(); }
}
public ref TValue1 TryGetOrDefaultValue(TKey1 key, int expireMS, out bool existed)
{
bool gotLock = false;
m_rwLock.EnterWriteLock();
try
{
try { }
finally
{
m_rwLock.EnterWriteLock();
gotLock = true;
}
ref TValue1 ret = ref CollectionsMarshal.GetValueRefOrAddDefault(m_values, key, out existed);
int now;
if (expireMS > 0)
@@ -507,26 +353,16 @@ namespace OpenSim.Framework
m_expireControl[key] = now;
return ref ret;
}
finally
{
if (gotLock)
m_rwLock.EnterWriteLock();
}
finally { m_rwLock.EnterWriteLock(); }
}
public TValue1[] Values
{
get
{
bool gotLock = false;
m_rwLock.EnterUpgradeableReadLock();
try
{
try { }
finally
{
m_rwLock.EnterUpgradeableReadLock();
gotLock = true;
}
if(valuesArrayCache == null)
{
valuesArrayCache = new TValue1[m_values.Count];
@@ -534,11 +370,7 @@ namespace OpenSim.Framework
}
return valuesArrayCache;
}
finally
{
if (gotLock)
m_rwLock.ExitUpgradeableReadLock();
}
finally { m_rwLock.ExitUpgradeableReadLock(); }
}
}
@@ -547,22 +379,12 @@ namespace OpenSim.Framework
{
get
{
bool gotLock = false;
m_rwLock.EnterUpgradeableReadLock();
try
{
try { }
finally
{
m_rwLock.EnterUpgradeableReadLock();
gotLock = true;
}
return m_values.Keys;
}
finally
{
if (gotLock)
m_rwLock.ExitUpgradeableReadLock();
}
finally { m_rwLock.ExitUpgradeableReadLock(); }
}
}
*/

View File

@@ -60,10 +60,7 @@ namespace OpenSim.Framework
[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]
private void CheckTimer()
{
if (m_purgeTimer == null)
{
m_purgeTimer = new Timer(Purge, null, m_expire, Timeout.Infinite);
}
m_purgeTimer ??= new Timer(Purge, null, m_expire, Timeout.Infinite);
}
[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]
@@ -99,17 +96,9 @@ namespace OpenSim.Framework
private void Purge(object ignored)
{
bool gotLock = false;
m_rwLock.EnterUpgradeableReadLock();
try
{
try { }
finally
{
m_rwLock.EnterUpgradeableReadLock();
gotLock = true;
}
if (m_dictionary.Count == 0)
{
DisposeTimer();
@@ -127,24 +116,14 @@ namespace OpenSim.Framework
if (expired.Count > 0)
{
bool gotWriteLock = false;
m_rwLock.EnterWriteLock();
try
{
try { }
finally
{
m_rwLock.EnterWriteLock();
gotWriteLock = true;
}
foreach (Tkey1 key in expired)
m_dictionary.Remove(key);
}
finally
{
if (gotWriteLock)
m_rwLock.ExitWriteLock();
}
finally { m_rwLock.ExitWriteLock(); }
if (m_dictionary.Count == 0)
DisposeTimer();
else
@@ -153,40 +132,24 @@ namespace OpenSim.Framework
else
m_purgeTimer.Change(m_expire, Timeout.Infinite);
}
finally
{
if (gotLock)
m_rwLock.ExitUpgradeableReadLock();
}
finally { m_rwLock.ExitUpgradeableReadLock(); }
}
public void Add(Tkey1 key)
{
bool gotLock = false;
int now = (int)(Util.GetTimeStampMS() - m_startTS) + m_expire;
m_rwLock.EnterWriteLock();
try
{
try { }
finally
{
m_rwLock.EnterWriteLock();
gotLock = true;
}
m_dictionary[key] = now;
CheckTimer();
}
finally
{
if (gotLock)
m_rwLock.ExitWriteLock();
}
finally { m_rwLock.ExitWriteLock(); }
}
public void Add(Tkey1 key, int expireMS)
{
bool gotLock = false;
int now;
if (expireMS > 0)
{
@@ -196,71 +159,38 @@ namespace OpenSim.Framework
else
now = int.MinValue;
m_rwLock.EnterWriteLock();
try
{
try { }
finally
{
m_rwLock.EnterWriteLock();
gotLock = true;
}
m_dictionary[key] = now;
CheckTimer();
}
finally
{
if (gotLock)
m_rwLock.ExitWriteLock();
}
finally { m_rwLock.ExitWriteLock(); }
}
public bool Remove(Tkey1 key)
{
bool success;
bool gotLock = false;
m_rwLock.EnterWriteLock();
try
{
try {}
finally
{
m_rwLock.EnterWriteLock();
gotLock = true;
}
success = m_dictionary.Remove(key);
bool success = m_dictionary.Remove(key);
if(m_dictionary.Count == 0)
DisposeTimer();
return success;
}
finally
{
if (gotLock)
m_rwLock.ExitWriteLock();
}
finally { m_rwLock.ExitWriteLock(); }
return success;
}
public void Clear()
{
bool gotLock = false;
m_rwLock.EnterWriteLock();
try
{
try {}
finally
{
m_rwLock.EnterWriteLock();
gotLock = true;
}
m_dictionary.Clear();
DisposeTimer();
}
finally
{
if (gotLock)
m_rwLock.ExitWriteLock();
}
finally { m_rwLock.ExitWriteLock(); }
}
public int Count
@@ -270,46 +200,24 @@ namespace OpenSim.Framework
public bool ContainsKey(Tkey1 key)
{
bool gotLock = false;
m_rwLock.EnterReadLock();
try
{
try { }
finally
{
m_rwLock.EnterReadLock();
gotLock = true;
}
return m_dictionary.ContainsKey(key);
}
finally
{
if (gotLock)
m_rwLock.ExitReadLock();
}
finally { m_rwLock.ExitReadLock(); }
}
public bool ContainsKey(Tkey1 key, int expireMS)
{
bool gotLock = false;
m_rwLock.EnterUpgradeableReadLock();
try
{
try { }
finally
{
m_rwLock.EnterUpgradeableReadLock();
gotLock = true;
}
if (m_dictionary.ContainsKey(key))
{
bool gotWriteLock = false;
m_rwLock.EnterWriteLock();
try
{
try { }
finally
{
m_rwLock.EnterWriteLock();
gotWriteLock = true;
}
int now;
if (expireMS > 0)
{
@@ -322,44 +230,21 @@ namespace OpenSim.Framework
m_dictionary[key] = now;
return true;
}
finally
{
if (gotWriteLock)
m_rwLock.ExitWriteLock();
}
finally { m_rwLock.ExitWriteLock(); }
}
return false;
}
finally
{
if (gotLock)
m_rwLock.EnterUpgradeableReadLock();
}
finally { m_rwLock.EnterUpgradeableReadLock(); }
}
public bool TryGetValue(Tkey1 key, out int value)
{
bool success;
bool gotLock = false;
m_rwLock.EnterReadLock();
try
{
try {}
finally
{
m_rwLock.EnterReadLock();
gotLock = true;
}
success = m_dictionary.TryGetValue(key, out value);
return m_dictionary.TryGetValue(key, out value);
}
finally
{
if (gotLock)
m_rwLock.ExitReadLock();
}
return success;
finally { m_rwLock.ExitReadLock(); }
}
}
}

View File

@@ -116,16 +116,9 @@ namespace OpenSim.Region.CoreModules.ServiceConnectorsOut.Grid
if (rinfo == null)
return false;
bool gotLock = false;
m_rwLock.EnterWriteLock();
try
{
try { }
finally
{
m_rwLock.EnterWriteLock();
gotLock = true;
}
int newexpire = (int)(Util.GetTimeStamp() - starttimeS) + expire;
ulong handle = rinfo.RegionHandle & HANDLEMASK;
@@ -148,11 +141,7 @@ namespace OpenSim.Region.CoreModules.ServiceConnectorsOut.Grid
m_byUUID[rinfo.RegionID] = rinfo;
return true;
}
finally
{
if (gotLock)
m_rwLock.ExitWriteLock();
}
finally { m_rwLock.ExitWriteLock(); }
}
public void Cache(GridRegion rinfo)
@@ -200,27 +189,16 @@ namespace OpenSim.Region.CoreModules.ServiceConnectorsOut.Grid
//if (disposed)
// return;
bool gotLock = false;
m_rwLock.EnterWriteLock();
try
{
try { }
finally
{
m_rwLock.EnterWriteLock();
gotLock = true;
}
m_expireControl.Clear();
m_byHandler.Clear();
m_byName.Clear();
m_byUUID.Clear();
m_innerHandles.Clear();
}
finally
{
if (gotLock)
m_rwLock.ExitWriteLock();
}
finally { m_rwLock.ExitWriteLock(); }
}
public bool Contains(ulong handle)
@@ -228,23 +206,12 @@ namespace OpenSim.Region.CoreModules.ServiceConnectorsOut.Grid
//if (disposed)
// return false;
bool gotLock = false;
m_rwLock.EnterReadLock();
try
{
try { }
finally
{
m_rwLock.EnterReadLock();
gotLock = true;
}
return m_byHandler.ContainsKey(handle & HANDLEMASK);
}
finally
{
if (gotLock)
m_rwLock.ExitReadLock();
}
finally { m_rwLock.ExitReadLock(); }
}
public bool Contains(GridRegion rinfo)
@@ -252,16 +219,9 @@ namespace OpenSim.Region.CoreModules.ServiceConnectorsOut.Grid
//if (disposed)
// return false;
bool gotLock = false;
m_rwLock.EnterReadLock();
try
{
try { }
finally
{
m_rwLock.EnterReadLock();
gotLock = true;
}
if(!m_byHandler.TryGetValue(rinfo.RegionHandle & HANDLEMASK, out GridRegion rcur))
return false;
@@ -269,11 +229,7 @@ namespace OpenSim.Region.CoreModules.ServiceConnectorsOut.Grid
rcur.RegionSizeX == rinfo.RegionSizeX &&
rcur.RegionSizeY == rinfo.RegionSizeY;
}
finally
{
if (gotLock)
m_rwLock.ExitReadLock();
}
finally { m_rwLock.ExitReadLock(); }
}
public bool Contains(UUID scope, ulong handle)
@@ -291,23 +247,12 @@ namespace OpenSim.Region.CoreModules.ServiceConnectorsOut.Grid
//if (disposed)
// return 0;
bool gotLock = false;
m_rwLock.EnterReadLock();
try
{
try { }
finally
{
m_rwLock.EnterReadLock();
gotLock = true;
}
return m_byName.Count;
}
finally
{
if (gotLock)
m_rwLock.ExitReadLock();
}
finally { m_rwLock.ExitReadLock(); }
}
public void Remove(GridRegion rinfo)
@@ -315,16 +260,9 @@ namespace OpenSim.Region.CoreModules.ServiceConnectorsOut.Grid
if (rinfo == null)
return;
bool gotLock = false;
m_rwLock.EnterWriteLock();
try
{
try { }
finally
{
m_rwLock.EnterWriteLock();
gotLock = true;
}
m_byName.Remove(rinfo.RegionName.ToLowerInvariant());
m_byUUID.Remove(rinfo.RegionID);
@@ -343,11 +281,7 @@ namespace OpenSim.Region.CoreModules.ServiceConnectorsOut.Grid
}
}
}
finally
{
if (gotLock)
m_rwLock.ExitWriteLock();
}
finally { m_rwLock.ExitWriteLock(); }
}
public void Remove(ulong regionHandle)
@@ -355,16 +289,9 @@ namespace OpenSim.Region.CoreModules.ServiceConnectorsOut.Grid
//if(disposed)
// return;
bool gotLock = false;
m_rwLock.EnterWriteLock();
try
{
try { }
finally
{
m_rwLock.EnterWriteLock();
gotLock = true;
}
regionHandle &= HANDLEMASK;
if (m_byHandler.TryGetValue(regionHandle, out GridRegion r))
@@ -387,11 +314,7 @@ namespace OpenSim.Region.CoreModules.ServiceConnectorsOut.Grid
}
}
}
finally
{
if (gotLock)
m_rwLock.ExitWriteLock();
}
finally { m_rwLock.ExitWriteLock(); }
}
public void Remove(UUID scopeID, GridRegion rinfo)
@@ -438,23 +361,12 @@ namespace OpenSim.Region.CoreModules.ServiceConnectorsOut.Grid
}
*/
bool gotLock = false;
m_rwLock.EnterReadLock();
try
{
try { }
finally
{
m_rwLock.EnterReadLock();
gotLock = true;
}
return m_byUUID.TryGetValue(regionID, out rinfo);
}
finally
{
if (gotLock)
m_rwLock.ExitReadLock();
}
finally { m_rwLock.ExitReadLock(); }
}
public bool TryGet(ulong handle, out GridRegion rinfo)
@@ -467,27 +379,16 @@ namespace OpenSim.Region.CoreModules.ServiceConnectorsOut.Grid
}
*/
bool gotLock = false;
m_rwLock.EnterReadLock();
try
{
try { }
finally
{
m_rwLock.EnterReadLock();
gotLock = true;
}
handle &= HANDLEMASK;
if (m_byHandler.TryGetValue(handle, out rinfo))
return true;
return m_innerHandles.TryGetValue(handle, out rinfo);
}
finally
{
if (gotLock)
m_rwLock.ExitReadLock();
}
finally { m_rwLock.ExitReadLock(); }
}
public bool TryGet(string name, out GridRegion rinfo)
@@ -500,23 +401,12 @@ namespace OpenSim.Region.CoreModules.ServiceConnectorsOut.Grid
}
*/
bool gotLock = false;
m_rwLock.EnterReadLock();
try
{
try { }
finally
{
m_rwLock.EnterReadLock();
gotLock = true;
}
return m_byName.TryGetValue(name.ToLowerInvariant(), out rinfo);
}
finally
{
if (gotLock)
m_rwLock.ExitReadLock();
return m_byName.TryGetValue(name.ToLowerInvariant(), out rinfo);
}
finally { m_rwLock.ExitReadLock(); }
}
public bool TryGet(uint x, uint y, out GridRegion rinfo)
@@ -529,16 +419,9 @@ namespace OpenSim.Region.CoreModules.ServiceConnectorsOut.Grid
}
*/
bool gotLock = false;
m_rwLock.EnterReadLock();
try
{
try { }
finally
{
m_rwLock.EnterReadLock();
gotLock = true;
}
ulong handle = x & HANDLECOORDMASK;
handle <<= 32;
handle |= y & HANDLECOORDMASK;
@@ -575,11 +458,7 @@ namespace OpenSim.Region.CoreModules.ServiceConnectorsOut.Grid
rinfo = null;
return false;
}
finally
{
if (gotLock)
m_rwLock.ExitReadLock();
}
finally { m_rwLock.ExitReadLock(); }
}
private void PurgeCache(object ignored)
@@ -588,16 +467,9 @@ namespace OpenSim.Region.CoreModules.ServiceConnectorsOut.Grid
if (m_expireControl.Count == 0)
return;
bool gotLock = false;
m_rwLock.EnterWriteLock();
try
{
try { }
finally
{
m_rwLock.EnterWriteLock();
gotLock = true;
}
int now = (int)(Util.GetTimeStamp() - starttimeS);
List<ulong> toexpire = new List<ulong>(m_expireControl.Count);
foreach (KeyValuePair<ulong, int> kvp in m_expireControl)
@@ -625,10 +497,8 @@ namespace OpenSim.Region.CoreModules.ServiceConnectorsOut.Grid
}
finally
{
if (gotLock)
m_rwLock.ExitWriteLock();
if (m_timer != null)
m_timer.Change(CACHE_PURGE_TIME, Timeout.Infinite);
m_rwLock.ExitWriteLock();
m_timer?.Change(CACHE_PURGE_TIME, Timeout.Infinite);
}
}