From dd277a0d02f1aa79f4fcb5d108cbc696e90500c2 Mon Sep 17 00:00:00 2001 From: John Hurliman Date: Fri, 10 Sep 2010 12:04:12 -0700 Subject: First pass at cleaning up thread safety in EntityManager and SceneGraph --- OpenSim/Region/Framework/Scenes/EntityManager.cs | 210 ++++++----------------- 1 file changed, 50 insertions(+), 160 deletions(-) (limited to 'OpenSim/Region/Framework/Scenes/EntityManager.cs') diff --git a/OpenSim/Region/Framework/Scenes/EntityManager.cs b/OpenSim/Region/Framework/Scenes/EntityManager.cs index 099fcce..85d0a4f 100644 --- a/OpenSim/Region/Framework/Scenes/EntityManager.cs +++ b/OpenSim/Region/Framework/Scenes/EntityManager.cs @@ -34,187 +34,89 @@ using OpenMetaverse; namespace OpenSim.Region.Framework.Scenes { - public class EntityManager : IEnumerable + public class EntityManager //: IEnumerable { private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType); - private readonly Dictionary m_eb_uuid = new Dictionary(); - private readonly Dictionary m_eb_localID = new Dictionary(); - //private readonly Dictionary m_pres_uuid = new Dictionary(); - private readonly Object m_lock = new Object(); + private readonly DoubleDictionary m_entities = new DoubleDictionary(); - [Obsolete("Use Add() instead.")] - public void Add(UUID id, EntityBase eb) + public int Count { - Add(eb); + get { return m_entities.Count; } } public void Add(EntityBase entity) { - lock (m_lock) - { - try - { - m_eb_uuid.Add(entity.UUID, entity); - m_eb_localID.Add(entity.LocalId, entity); - } - catch(Exception e) - { - m_log.ErrorFormat("Add Entity failed: {0}", e.Message); - } - } - } - - public void InsertOrReplace(EntityBase entity) - { - lock (m_lock) - { - try - { - m_eb_uuid[entity.UUID] = entity; - m_eb_localID[entity.LocalId] = entity; - } - catch(Exception e) - { - m_log.ErrorFormat("Insert or Replace Entity failed: {0}", e.Message); - } - } + m_entities.Add(entity.UUID, entity.LocalId, entity); } public void Clear() { - lock (m_lock) - { - m_eb_uuid.Clear(); - m_eb_localID.Clear(); - } - } - - public int Count - { - get - { - return m_eb_uuid.Count; - } + m_entities.Clear(); } public bool ContainsKey(UUID id) { - try - { - return m_eb_uuid.ContainsKey(id); - } - catch - { - return false; - } + return m_entities.ContainsKey(id); } public bool ContainsKey(uint localID) { - try - { - return m_eb_localID.ContainsKey(localID); - } - catch - { - return false; - } + return m_entities.ContainsKey(localID); } public bool Remove(uint localID) { - lock (m_lock) - { - try - { - bool a = false; - EntityBase entity; - if (m_eb_localID.TryGetValue(localID, out entity)) - a = m_eb_uuid.Remove(entity.UUID); - - bool b = m_eb_localID.Remove(localID); - return a && b; - } - catch (Exception e) - { - m_log.ErrorFormat("Remove Entity failed for {0}", localID, e); - return false; - } - } + return m_entities.Remove(localID); } public bool Remove(UUID id) { - lock (m_lock) - { - try - { - bool a = false; - EntityBase entity; - if (m_eb_uuid.TryGetValue(id, out entity)) - a = m_eb_localID.Remove(entity.LocalId); - - bool b = m_eb_uuid.Remove(id); - return a && b; - } - catch (Exception e) - { - m_log.ErrorFormat("Remove Entity failed for {0}", id, e); - return false; - } - } + return m_entities.Remove(id); } - public List GetAllByType() + public EntityBase[] GetAllByType() { List tmp = new List(); - lock (m_lock) - { - try + m_entities.ForEach( + delegate(EntityBase entity) { - foreach (KeyValuePair pair in m_eb_uuid) - { - if (pair.Value is T) - { - tmp.Add(pair.Value); - } - } + if (entity is T) + tmp.Add(entity); } - catch (Exception e) - { - m_log.ErrorFormat("GetAllByType failed for {0}", e); - tmp = null; - } - } + ); - return tmp; + return tmp.ToArray(); } - public List GetEntities() + public EntityBase[] GetEntities() { - lock (m_lock) - { - return new List(m_eb_uuid.Values); - } + List tmp = new List(m_entities.Count); + m_entities.ForEach(delegate(EntityBase entity) { tmp.Add(entity); }); + return tmp.ToArray(); + } + + public void ForEach(Action action) + { + m_entities.ForEach(action); + } + + public EntityBase Find(Predicate predicate) + { + return m_entities.FindValue(predicate); } public EntityBase this[UUID id] { get { - lock (m_lock) - { - EntityBase entity; - if (m_eb_uuid.TryGetValue(id, out entity)) - return entity; - else - return null; - } + EntityBase entity; + m_entities.TryGetValue(id, out entity); + return entity; } set { - InsertOrReplace(value); + Add(value); } } @@ -222,50 +124,38 @@ namespace OpenSim.Region.Framework.Scenes { get { - lock (m_lock) - { - EntityBase entity; - if (m_eb_localID.TryGetValue(localID, out entity)) - return entity; - else - return null; - } + EntityBase entity; + m_entities.TryGetValue(localID, out entity); + return entity; } set { - InsertOrReplace(value); + Add(value); } } public bool TryGetValue(UUID key, out EntityBase obj) { - lock (m_lock) - { - return m_eb_uuid.TryGetValue(key, out obj); - } + return m_entities.TryGetValue(key, out obj); } public bool TryGetValue(uint key, out EntityBase obj) { - lock (m_lock) - { - return m_eb_localID.TryGetValue(key, out obj); - } + return m_entities.TryGetValue(key, out obj); } /// /// This could be optimised to work on the list 'live' rather than making a safe copy and iterating that. /// /// - public IEnumerator GetEnumerator() - { - return GetEntities().GetEnumerator(); - } - - IEnumerator IEnumerable.GetEnumerator() - { - return GetEnumerator(); - } - + //public IEnumerator GetEnumerator() + //{ + // return GetEntities().GetEnumerator(); + //} + + //IEnumerator IEnumerable.GetEnumerator() + //{ + // return GetEnumerator(); + //} } } -- cgit v1.1