From dab6387bba7a7388e3cdb4ad1eeea646bc03a287 Mon Sep 17 00:00:00 2001 From: Justin Clark-Casey (justincc) Date: Mon, 12 Sep 2011 21:05:26 +0100 Subject: lock AvatarAppearance.m_attachments when we use it This is partly to address http://opensimulator.org/mantis/view.php?id=5644, though something more thorough is needed. --- OpenSim/Framework/AvatarAppearance.cs | 91 ++++++++++++++++++++++------------- 1 file changed, 58 insertions(+), 33 deletions(-) (limited to 'OpenSim/Framework/AvatarAppearance.cs') diff --git a/OpenSim/Framework/AvatarAppearance.cs b/OpenSim/Framework/AvatarAppearance.cs index cce44b0..c69dde3 100644 --- a/OpenSim/Framework/AvatarAppearance.cs +++ b/OpenSim/Framework/AvatarAppearance.cs @@ -391,10 +391,14 @@ namespace OpenSim.Framework public List GetAttachments() { List alist = new List(); - foreach (KeyValuePair> kvp in m_attachments) + + lock (m_attachments) { - foreach (AvatarAttachment attach in kvp.Value) - alist.Add(new AvatarAttachment(attach)); + foreach (KeyValuePair> kvp in m_attachments) + { + foreach (AvatarAttachment attach in kvp.Value) + alist.Add(new AvatarAttachment(attach)); + } } return alist; @@ -406,10 +410,13 @@ namespace OpenSim.Framework // "[AVATAR APPEARNCE]: Appending itemID={0}, assetID={1} at {2}", // attach.ItemID, attach.AssetID, attach.AttachPoint); - if (!m_attachments.ContainsKey(attach.AttachPoint)) - m_attachments[attach.AttachPoint] = new List(); - - m_attachments[attach.AttachPoint].Add(attach); + lock (m_attachments) + { + if (!m_attachments.ContainsKey(attach.AttachPoint)) + m_attachments[attach.AttachPoint] = new List(); + + m_attachments[attach.AttachPoint].Add(attach); + } } internal void ReplaceAttachment(AvatarAttachment attach) @@ -418,8 +425,11 @@ namespace OpenSim.Framework // "[AVATAR APPEARANCE]: Replacing itemID={0}, assetID={1} at {2}", // attach.ItemID, attach.AssetID, attach.AttachPoint); - m_attachments[attach.AttachPoint] = new List(); - m_attachments[attach.AttachPoint].Add(attach); + lock (m_attachments) + { + m_attachments[attach.AttachPoint] = new List(); + m_attachments[attach.AttachPoint].Add(attach); + } } /// @@ -448,10 +458,13 @@ namespace OpenSim.Framework if (item == UUID.Zero) { - if (m_attachments.ContainsKey(attachpoint)) + lock (m_attachments) { - m_attachments.Remove(attachpoint); - return true; + if (m_attachments.ContainsKey(attachpoint)) + { + m_attachments.Remove(attachpoint); + return true; + } } return false; @@ -494,11 +507,14 @@ namespace OpenSim.Framework /// Returns null if this item is not attached. public AvatarAttachment GetAttachmentForItem(UUID itemID) { - foreach (KeyValuePair> kvp in m_attachments) + lock (m_attachments) { - int index = kvp.Value.FindIndex(delegate(AvatarAttachment a) { return a.ItemID == itemID; }); - if (index >= 0) - return kvp.Value[index]; + foreach (KeyValuePair> kvp in m_attachments) + { + int index = kvp.Value.FindIndex(delegate(AvatarAttachment a) { return a.ItemID == itemID; }); + if (index >= 0) + return kvp.Value[index]; + } } return null; @@ -506,11 +522,14 @@ namespace OpenSim.Framework public int GetAttachpoint(UUID itemID) { - foreach (KeyValuePair> kvp in m_attachments) + lock (m_attachments) { - int index = kvp.Value.FindIndex(delegate(AvatarAttachment a) { return a.ItemID == itemID; }); - if (index >= 0) - return kvp.Key; + foreach (KeyValuePair> kvp in m_attachments) + { + int index = kvp.Value.FindIndex(delegate(AvatarAttachment a) { return a.ItemID == itemID; }); + if (index >= 0) + return kvp.Key; + } } return 0; @@ -518,27 +537,32 @@ namespace OpenSim.Framework public bool DetachAttachment(UUID itemID) { - foreach (KeyValuePair> kvp in m_attachments) + lock (m_attachments) { - int index = kvp.Value.FindIndex(delegate(AvatarAttachment a) { return a.ItemID == itemID; }); - if (index >= 0) + foreach (KeyValuePair> kvp in m_attachments) { - // Remove it from the list of attachments at that attach point - m_attachments[kvp.Key].RemoveAt(index); - - // And remove the list if there are no more attachments here - if (m_attachments[kvp.Key].Count == 0) - m_attachments.Remove(kvp.Key); - - return true; + int index = kvp.Value.FindIndex(delegate(AvatarAttachment a) { return a.ItemID == itemID; }); + if (index >= 0) + { + // Remove it from the list of attachments at that attach point + m_attachments[kvp.Key].RemoveAt(index); + + // And remove the list if there are no more attachments here + if (m_attachments[kvp.Key].Count == 0) + m_attachments.Remove(kvp.Key); + + return true; + } } } + return false; } public void ClearAttachments() { - m_attachments.Clear(); + lock (m_attachments) + m_attachments.Clear(); } #region Packing Functions @@ -576,7 +600,8 @@ namespace OpenSim.Framework data["visualparams"] = visualparams; // Attachments - OSDArray attachs = new OSDArray(m_attachments.Count); + List attachments = GetAttachments(); + OSDArray attachs = new OSDArray(attachments.Count); foreach (AvatarAttachment attach in GetAttachments()) attachs.Add(attach.Pack()); data["attachments"] = attachs; -- cgit v1.1