Change EntityManager to use RWlocks

avinationmerge
Melanie 2009-12-06 03:56:15 +00:00
parent f9ae9afaac
commit 1e222d52e6
1 changed files with 67 additions and 12 deletions

View File

@ -40,7 +40,7 @@ namespace OpenSim.Region.Framework.Scenes
private readonly Dictionary<UUID,EntityBase> m_eb_uuid = new Dictionary<UUID, EntityBase>(); private readonly Dictionary<UUID,EntityBase> m_eb_uuid = new Dictionary<UUID, EntityBase>();
private readonly Dictionary<uint, EntityBase> m_eb_localID = new Dictionary<uint, EntityBase>(); private readonly Dictionary<uint, EntityBase> m_eb_localID = new Dictionary<uint, EntityBase>();
//private readonly Dictionary<UUID, ScenePresence> m_pres_uuid = new Dictionary<UUID, ScenePresence>(); //private readonly Dictionary<UUID, ScenePresence> m_pres_uuid = new Dictionary<UUID, ScenePresence>();
private readonly Object m_lock = new Object(); private System.Threading.ReaderWriterLockSlim m_lock = new System.Threading.ReaderWriterLockSlim();
[Obsolete("Use Add() instead.")] [Obsolete("Use Add() instead.")]
public void Add(UUID id, EntityBase eb) public void Add(UUID id, EntityBase eb)
@ -50,7 +50,8 @@ namespace OpenSim.Region.Framework.Scenes
public void Add(EntityBase entity) public void Add(EntityBase entity)
{ {
lock (m_lock) m_lock.EnterWriteLock();
try
{ {
try try
{ {
@ -62,11 +63,16 @@ namespace OpenSim.Region.Framework.Scenes
m_log.ErrorFormat("Add Entity failed: {0}", e.Message); m_log.ErrorFormat("Add Entity failed: {0}", e.Message);
} }
} }
finally
{
m_lock.ExitWriteLock();
}
} }
public void InsertOrReplace(EntityBase entity) public void InsertOrReplace(EntityBase entity)
{ {
lock (m_lock) m_lock.EnterWriteLock();
try
{ {
try try
{ {
@ -78,15 +84,24 @@ namespace OpenSim.Region.Framework.Scenes
m_log.ErrorFormat("Insert or Replace Entity failed: {0}", e.Message); m_log.ErrorFormat("Insert or Replace Entity failed: {0}", e.Message);
} }
} }
finally
{
m_lock.ExitWriteLock();
}
} }
public void Clear() public void Clear()
{ {
lock (m_lock) m_lock.EnterWriteLock();
try
{ {
m_eb_uuid.Clear(); m_eb_uuid.Clear();
m_eb_localID.Clear(); m_eb_localID.Clear();
} }
finally
{
m_lock.ExitWriteLock();
}
} }
public int Count public int Count
@ -123,7 +138,8 @@ namespace OpenSim.Region.Framework.Scenes
public bool Remove(uint localID) public bool Remove(uint localID)
{ {
lock (m_lock) m_lock.EnterWriteLock();
try
{ {
try try
{ {
@ -141,11 +157,16 @@ namespace OpenSim.Region.Framework.Scenes
return false; return false;
} }
} }
finally
{
m_lock.ExitWriteLock();
}
} }
public bool Remove(UUID id) public bool Remove(UUID id)
{ {
lock (m_lock) m_lock.EnterWriteLock();
try
{ {
try try
{ {
@ -163,13 +184,18 @@ namespace OpenSim.Region.Framework.Scenes
return false; return false;
} }
} }
finally
{
m_lock.ExitWriteLock();
}
} }
public List<EntityBase> GetAllByType<T>() public List<EntityBase> GetAllByType<T>()
{ {
List<EntityBase> tmp = new List<EntityBase>(); List<EntityBase> tmp = new List<EntityBase>();
lock (m_lock) m_lock.EnterReadLock();
try
{ {
try try
{ {
@ -187,23 +213,33 @@ namespace OpenSim.Region.Framework.Scenes
tmp = null; tmp = null;
} }
} }
finally
{
m_lock.ExitReadLock();
}
return tmp; return tmp;
} }
public List<EntityBase> GetEntities() public List<EntityBase> GetEntities()
{ {
lock (m_lock) m_lock.EnterReadLock();
try
{ {
return new List<EntityBase>(m_eb_uuid.Values); return new List<EntityBase>(m_eb_uuid.Values);
} }
finally
{
m_lock.ExitReadLock();
}
} }
public EntityBase this[UUID id] public EntityBase this[UUID id]
{ {
get get
{ {
lock (m_lock) m_lock.EnterReadLock();
try
{ {
EntityBase entity; EntityBase entity;
if (m_eb_uuid.TryGetValue(id, out entity)) if (m_eb_uuid.TryGetValue(id, out entity))
@ -211,6 +247,10 @@ namespace OpenSim.Region.Framework.Scenes
else else
return null; return null;
} }
finally
{
m_lock.ExitReadLock();
}
} }
set set
{ {
@ -222,7 +262,8 @@ namespace OpenSim.Region.Framework.Scenes
{ {
get get
{ {
lock (m_lock) m_lock.EnterReadLock();
try
{ {
EntityBase entity; EntityBase entity;
if (m_eb_localID.TryGetValue(localID, out entity)) if (m_eb_localID.TryGetValue(localID, out entity))
@ -230,6 +271,10 @@ namespace OpenSim.Region.Framework.Scenes
else else
return null; return null;
} }
finally
{
m_lock.ExitReadLock();
}
} }
set set
{ {
@ -239,18 +284,28 @@ namespace OpenSim.Region.Framework.Scenes
public bool TryGetValue(UUID key, out EntityBase obj) public bool TryGetValue(UUID key, out EntityBase obj)
{ {
lock (m_lock) m_lock.EnterReadLock();
try
{ {
return m_eb_uuid.TryGetValue(key, out obj); return m_eb_uuid.TryGetValue(key, out obj);
} }
finally
{
m_lock.ExitReadLock();
}
} }
public bool TryGetValue(uint key, out EntityBase obj) public bool TryGetValue(uint key, out EntityBase obj)
{ {
lock (m_lock) m_lock.EnterReadLock();
try
{ {
return m_eb_localID.TryGetValue(key, out obj); return m_eb_localID.TryGetValue(key, out obj);
} }
finally
{
m_lock.ExitReadLock();
}
} }
/// <summary> /// <summary>