From 859bc717a4fe4cd5810ad9889cfb9b1e7f5c2046 Mon Sep 17 00:00:00 2001 From: Dan Lake Date: Fri, 19 Mar 2010 05:51:16 -0700 Subject: Cleaned up access to scenepresences in scenegraph. GetScenePresences and GetAvatars have been removed to consolidate locking and iteration within SceneGraph. All callers which used these to then iterate over presences have been refactored to instead pass their delegates to Scene.ForEachScenePresence(Action). --- OpenSim/Region/Framework/Scenes/SceneGraph.cs | 186 +++++++++++--------------- 1 file changed, 79 insertions(+), 107 deletions(-) (limited to 'OpenSim/Region/Framework/Scenes/SceneGraph.cs') diff --git a/OpenSim/Region/Framework/Scenes/SceneGraph.cs b/OpenSim/Region/Framework/Scenes/SceneGraph.cs index d259c42..b6e5995 100644 --- a/OpenSim/Region/Framework/Scenes/SceneGraph.cs +++ b/OpenSim/Region/Framework/Scenes/SceneGraph.cs @@ -699,116 +699,84 @@ namespace OpenSim.Region.Framework.Scenes return null; } - // The idea is to have a group of method that return a list of avatars meeting some requirement - // ie it could be all m_scenePresences within a certain range of the calling prim/avatar. - // - // GetAvatars returns a new list of all root agent presences in the scene - // GetScenePresences returns a new list of all presences in the scene or a filter may be passed. - // GetScenePresence returns the presence with matching UUID or the first presence matching the passed filter. - // ForEachScenePresence requests the Scene to run a delegate function against all presences. - /// - /// Request a list of all avatars in this region (no child agents) - /// This list is a new object, so it can be iterated over without locking. + /// Request a copy of m_scenePresences in this World + /// There is no guarantee that presences will remain in the scene after the list is returned. + /// This list should remain private to SceneGraph. Callers wishing to iterate should instead + /// pass a delegate to ForEachScenePresence. /// /// - public List GetAvatars() + private List GetScenePresences() { - return GetScenePresences(delegate(ScenePresence scenePresence) - { - return !scenePresence.IsChildAgent; - }); + lock (m_scenePresences) + return new List(m_scenePresenceArray); } /// - /// Request a list of m_scenePresences in this World - /// Returns a copy so it can be iterated without a lock. - /// There is no guarantee that presences will remain in the scene after the list is returned. + /// Request a scene presence by UUID. Fast, indexed lookup. /// - /// - protected internal List GetScenePresences() + /// + /// null if the presence was not found + protected internal ScenePresence GetScenePresence(UUID agentID) { - List result; + ScenePresence sp; lock (m_scenePresences) { - result = new List(m_scenePresenceArray.Length); - result.AddRange(m_scenePresenceArray); + m_scenePresences.TryGetValue(agentID, out sp); } - return result; + return sp; } /// - /// Request a filtered list of m_scenePresences in this World - /// Returns a copy so it can be iterated without a lock. - /// There is no guarantee that presences will remain in the scene after the list is returned. + /// Request the scene presence by name. /// - /// - protected internal List GetScenePresences(FilterAvatarList filter) + /// + /// + /// null if the presence was not found + protected internal ScenePresence GetScenePresence(string firstName, string lastName) { - List result = new List(); - // Check each ScenePresence against the filter - ForEachScenePresence(delegate(ScenePresence presence) + foreach (ScenePresence presence in GetScenePresences()) { - if (filter(presence)) - result.Add(presence); - }); - return result; + if (presence.Firstname == firstName && presence.Lastname == lastName) + return presence; + } + return null; } /// - /// Request the ScenePresence in this region matching filter. - /// Only the first match is returned. - /// + /// Request the scene presence by localID. /// - /// - /// - protected internal ScenePresence GetScenePresence(FilterAvatarList filter) + /// + /// null if the presence was not found + protected internal ScenePresence GetScenePresence(uint localID) { - ScenePresence result = null; - // Get all of the ScenePresences - List presences = GetScenePresences(); - foreach (ScenePresence presence in presences) - { - if (filter(presence)) - { - result = presence; - break; - } - } - return result; + foreach (ScenePresence presence in GetScenePresences()) + if (presence.LocalId == localID) + return presence; + return null; } - protected internal ScenePresence GetScenePresence(string firstName, string lastName) + protected internal bool TryGetAvatar(UUID agentID, out ScenePresence avatar) { - return GetScenePresence(delegate(ScenePresence presence) + lock (m_scenePresences) { - return(presence.Firstname == firstName && presence.Lastname == lastName); - }); - } - - /// - /// Request a scene presence by UUID - /// - /// - /// null if the agent was not found - protected internal ScenePresence GetScenePresence(UUID agentID) - { - ScenePresence sp; - TryGetAvatar(agentID, out sp); - return sp; + m_scenePresences.TryGetValue(agentID, out avatar); + } + return (avatar != null); } - /// - /// Request the ScenePresence in this region by localID. - /// - /// - /// - protected internal ScenePresence GetScenePresence(uint localID) + protected internal bool TryGetAvatarByName(string name, out ScenePresence avatar) { - return GetScenePresence(delegate(ScenePresence presence) + avatar = null; + foreach (ScenePresence presence in GetScenePresences()) { - return (presence.LocalId == localID); - }); + if (String.Compare(name, presence.ControllingClient.Name, true) == 0) + { + avatar = presence; + break; + } + } + return (avatar != null); } /// @@ -962,24 +930,6 @@ namespace OpenSim.Region.Framework.Scenes return group.GetChildPart(fullID); } - protected internal bool TryGetAvatar(UUID avatarId, out ScenePresence avatar) - { - lock (m_scenePresences) - { - m_scenePresences.TryGetValue(avatarId, out avatar); - } - return (avatar != null); - } - - protected internal bool TryGetAvatarByName(string avatarName, out ScenePresence avatar) - { - avatar = GetScenePresence(delegate(ScenePresence presence) - { - return (String.Compare(avatarName, presence.ControllingClient.Name, true) == 0); - }); - return (avatar != null); - } - /// /// Returns a list of the entities in the scene. This is a new list so no locking is required to iterate over /// it @@ -1042,6 +992,10 @@ namespace OpenSim.Region.Framework.Scenes return UUID.Zero; } + /// + /// Performs action on all scene object groups. + /// + /// protected internal void ForEachSOG(Action action) { List objlist = new List(SceneObjectGroupsByFullID.Values); @@ -1061,23 +1015,41 @@ namespace OpenSim.Region.Framework.Scenes /// - /// Performs action on all scene presences. + /// Performs action on all scene presences. This can ultimately run the actions in parallel but + /// any delegates passed in will need to implement their own locking on data they reference and + /// modify outside of the scope of the delegate. /// /// public void ForEachScenePresence(Action action) { - List presences = GetScenePresences(); - try + // Once all callers have their delegates configured for parallelism, we can unleash this + /* + Action protectedAction = new Action(delegate(ScenePresence sp) + { + try + { + action(sp); + } + catch (Exception e) + { + m_log.Info("[BUG] in " + m_parentScene.RegionInfo.RegionName + ": " + e.ToString()); + m_log.Info("[BUG] Stack Trace: " + e.StackTrace); + } + }); + Parallel.ForEach(GetScenePresences(), protectedAction); + */ + // For now, perform actiona serially + foreach (ScenePresence sp in GetScenePresences()) { - foreach(ScenePresence presence in presences) + try { - action(presence); + action(sp); + } + catch (Exception e) + { + m_log.Info("[BUG] in " + m_parentScene.RegionInfo.RegionName + ": " + e.ToString()); + m_log.Info("[BUG] Stack Trace: " + e.StackTrace); } - } - catch (Exception e) - { - m_log.Info("[BUG] in " + m_parentScene.RegionInfo.RegionName + ": " + e.ToString()); - m_log.Info("[BUG] Stack Trace: " + e.StackTrace); } } -- cgit v1.1