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/Scene.cs | 81 +++------ OpenSim/Region/Framework/Scenes/SceneGraph.cs | 186 +++++++++------------ OpenSim/Region/Framework/Scenes/SceneManager.cs | 6 +- OpenSim/Region/Framework/Scenes/SceneObjectPart.cs | 19 ++- OpenSim/Region/Framework/Scenes/ScenePresence.cs | 58 +++---- 5 files changed, 147 insertions(+), 203 deletions(-) (limited to 'OpenSim/Region/Framework') diff --git a/OpenSim/Region/Framework/Scenes/Scene.cs b/OpenSim/Region/Framework/Scenes/Scene.cs index a86a33c..4b97e39 100644 --- a/OpenSim/Region/Framework/Scenes/Scene.cs +++ b/OpenSim/Region/Framework/Scenes/Scene.cs @@ -3269,7 +3269,7 @@ namespace OpenSim.Region.Framework.Scenes } } - ScenePresence sp = m_sceneGraph.GetScenePresence(agent.AgentID); + ScenePresence sp = GetScenePresence(agent.AgentID); if (sp != null) { m_log.DebugFormat( @@ -3561,8 +3561,7 @@ namespace OpenSim.Region.Framework.Scenes /// message to display to the user. Reason for being logged off public void HandleLogOffUserFromGrid(UUID AvatarID, UUID RegionSecret, string message) { - ScenePresence loggingOffUser = null; - loggingOffUser = GetScenePresence(AvatarID); + ScenePresence loggingOffUser = GetScenePresence(AvatarID); if (loggingOffUser != null) { UUID localRegionSecret = UUID.Zero; @@ -3598,8 +3597,8 @@ namespace OpenSim.Region.Framework.Scenes /// public virtual void AgentCrossing(UUID agentID, Vector3 position, bool isFlying) { - ScenePresence presence; - if(m_sceneGraph.TryGetAvatar(agentID, out presence)) + ScenePresence presence = GetScenePresence(agentID); + if(presence != null) { try { @@ -3773,8 +3772,8 @@ namespace OpenSim.Region.Framework.Scenes public void RequestTeleportLocation(IClientAPI remoteClient, ulong regionHandle, Vector3 position, Vector3 lookAt, uint teleportFlags) { - ScenePresence sp; - if(m_sceneGraph.TryGetAvatar(remoteClient.AgentId, out sp)) + ScenePresence sp = GetScenePresence(remoteClient.AgentId); + if (sp != null) { uint regionX = m_regInfo.RegionLocX; uint regionY = m_regInfo.RegionLocY; @@ -3952,17 +3951,17 @@ namespace OpenSim.Region.Framework.Scenes m_log.ErrorFormat("{0,-16}{1,-16}{2,-25}{3,-25}{4,-16}{5,-16}{6,-16}", "Firstname", "Lastname", "Agent ID", "Session ID", "Circuit", "IP", "World"); - foreach (ScenePresence scenePresence in GetAvatars()) + ForEachScenePresence(delegate(ScenePresence sp) { m_log.ErrorFormat("{0,-16}{1,-16}{2,-25}{3,-25}{4,-16},{5,-16}{6,-16}", - scenePresence.Firstname, - scenePresence.Lastname, - scenePresence.UUID, - scenePresence.ControllingClient.AgentId, + sp.Firstname, + sp.Lastname, + sp.UUID, + sp.ControllingClient.AgentId, "Unknown", "Unknown", RegionInfo.RegionName); - } + }); break; } @@ -4128,72 +4127,42 @@ namespace OpenSim.Region.Framework.Scenes m_sceneGraph.RemovePhysicalPrim(num); } - //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 first/last name. - // ForEachScenePresence requests the Scene to run a delegate function against all presences. - - /// - /// Return a list of all avatars in this region. - /// This list is a new object, so it can be iterated over without locking. - /// - /// - public List GetAvatars() - { - return m_sceneGraph.GetAvatars(); - } - - /// - /// Return a list of all ScenePresences in this region. This returns child agents as well as root agents. - /// This list is a new object, so it can be iterated over without locking. - /// - /// - public List GetScenePresences() + public int GetRootAgentCount() { - return m_sceneGraph.GetScenePresences(); + return m_sceneGraph.GetRootAgentCount(); } - /// - /// Request a filtered list of ScenePresences in this region. - /// This list is a new object, so it can be iterated over without locking. - /// - /// - /// - public List GetScenePresences(FilterAvatarList filter) + public int GetChildAgentCount() { - return m_sceneGraph.GetScenePresences(filter); + return m_sceneGraph.GetChildAgentCount(); } /// - /// Request a scene presence by UUID + /// Request a scene presence by UUID. Fast, indexed lookup. /// - /// - /// - public ScenePresence GetScenePresence(UUID avatarID) + /// + /// null if the presence was not found + public ScenePresence GetScenePresence(UUID agentID) { - return m_sceneGraph.GetScenePresence(avatarID); + return m_sceneGraph.GetScenePresence(agentID); } /// - /// Request the ScenePresence in this region by first/last name. - /// Should normally only be a single match, but first is always returned + /// Request the scene presence by name. /// /// /// - /// + /// null if the presence was not found public ScenePresence GetScenePresence(string firstName, string lastName) { return m_sceneGraph.GetScenePresence(firstName, lastName); } /// - /// Request the ScenePresence in this region by localID. + /// Request the scene presence by localID. /// /// - /// + /// null if the presence was not found public ScenePresence GetScenePresence(uint localID) { return m_sceneGraph.GetScenePresence(localID); 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); } } diff --git a/OpenSim/Region/Framework/Scenes/SceneManager.cs b/OpenSim/Region/Framework/Scenes/SceneManager.cs index a955532..1168341 100644 --- a/OpenSim/Region/Framework/Scenes/SceneManager.cs +++ b/OpenSim/Region/Framework/Scenes/SceneManager.cs @@ -454,8 +454,10 @@ namespace OpenSim.Region.Framework.Scenes ForEachCurrentScene(delegate(Scene scene) { - List scenePresences = scene.GetScenePresences(); - presences.AddRange(scenePresences); + scene.ForEachScenePresence(delegate(ScenePresence sp) + { + presences.Add(sp); + }); }); return presences; diff --git a/OpenSim/Region/Framework/Scenes/SceneObjectPart.cs b/OpenSim/Region/Framework/Scenes/SceneObjectPart.cs index 0e21487..88bdf31 100644 --- a/OpenSim/Region/Framework/Scenes/SceneObjectPart.cs +++ b/OpenSim/Region/Framework/Scenes/SceneObjectPart.cs @@ -1321,11 +1321,11 @@ namespace OpenSim.Region.Framework.Scenes if (volume < 0) volume = 0; - List avatarts = m_parentGroup.Scene.GetAvatars(); - foreach (ScenePresence p in avatarts) + m_parentGroup.Scene.ForEachScenePresence(delegate(ScenePresence sp) { - p.ControllingClient.SendAttachedSoundGainChange(UUID, (float)volume); - } + if(!sp.IsChildAgent) + sp.ControllingClient.SendAttachedSoundGainChange(UUID, (float)volume); + }); } /// @@ -2609,12 +2609,13 @@ namespace OpenSim.Region.Framework.Scenes } } - List avatarts = m_parentGroup.Scene.GetAvatars(); - foreach (ScenePresence p in avatarts) + m_parentGroup.Scene.ForEachScenePresence(delegate(ScenePresence sp) { - if (!(Util.GetDistanceTo(p.AbsolutePosition, AbsolutePosition) >= 100)) - p.ControllingClient.SendPreLoadSound(objectID, objectID, soundID); - } + if (sp.IsChildAgent) + return; + if (!(Util.GetDistanceTo(sp.AbsolutePosition, AbsolutePosition) >= 100)) + sp.ControllingClient.SendPreLoadSound(objectID, objectID, soundID); + }); } public void RemFlag(PrimFlags flag) diff --git a/OpenSim/Region/Framework/Scenes/ScenePresence.cs b/OpenSim/Region/Framework/Scenes/ScenePresence.cs index 766f6d3..b5f6217 100644 --- a/OpenSim/Region/Framework/Scenes/ScenePresence.cs +++ b/OpenSim/Region/Framework/Scenes/ScenePresence.cs @@ -2396,35 +2396,33 @@ namespace OpenSim.Region.Framework.Scenes List CoarseLocations = new List(); List AvatarUUIDs = new List(); - List avatars = m_scene.GetAvatars(); - for (int i = 0; i < avatars.Count; i++) + m_scene.ForEachScenePresence(delegate(ScenePresence sp) { - // Requested by LibOMV. Send Course Location on self. - //if (avatars[i] != this) - //{ - if (avatars[i].ParentID != 0) + if (sp.IsChildAgent) + return; + + if (sp.ParentID != 0) + { + // sitting avatar + SceneObjectPart sop = m_scene.GetSceneObjectPart(sp.ParentID); + if (sop != null) { - // sitting avatar - SceneObjectPart sop = m_scene.GetSceneObjectPart(avatars[i].ParentID); - if (sop != null) - { - CoarseLocations.Add(sop.AbsolutePosition + avatars[i].m_pos); - AvatarUUIDs.Add(avatars[i].UUID); - } - else - { - // we can't find the parent.. ! arg! - CoarseLocations.Add(avatars[i].m_pos); - AvatarUUIDs.Add(avatars[i].UUID); - } + CoarseLocations.Add(sop.AbsolutePosition + sp.m_pos); + AvatarUUIDs.Add(sp.UUID); } else { - CoarseLocations.Add(avatars[i].m_pos); - AvatarUUIDs.Add(avatars[i].UUID); + // we can't find the parent.. ! arg! + CoarseLocations.Add(sp.m_pos); + AvatarUUIDs.Add(sp.UUID); } - //} - } + } + else + { + CoarseLocations.Add(sp.m_pos); + AvatarUUIDs.Add(sp.UUID); + } + }); m_controllingClient.SendCoarseLocationUpdate(AvatarUUIDs, CoarseLocations); @@ -2498,13 +2496,15 @@ namespace OpenSim.Region.Framework.Scenes m_perfMonMS = Util.EnvironmentTickCount(); // only send update from root agents to other clients; children are only "listening posts" - List avatars = m_scene.GetAvatars(); - foreach (ScenePresence avatar in avatars) + int count = 0; + m_scene.ForEachScenePresence(delegate(ScenePresence sp) { - SendFullUpdateToOtherClient(avatar); - - } - m_scene.StatsReporter.AddAgentUpdates(avatars.Count); + if (sp.IsChildAgent) + return; + SendFullUpdateToOtherClient(sp); + ++count; + }); + m_scene.StatsReporter.AddAgentUpdates(count); m_scene.StatsReporter.AddAgentTime(Util.EnvironmentTickCountSubtract(m_perfMonMS)); Animator.SendAnimPack(); -- cgit v1.1