diff options
Diffstat (limited to 'OpenSim/Framework')
-rw-r--r-- | OpenSim/Framework/MapAndArray.cs | 46 |
1 files changed, 25 insertions, 21 deletions
diff --git a/OpenSim/Framework/MapAndArray.cs b/OpenSim/Framework/MapAndArray.cs index 32d6978..a3ce55e 100644 --- a/OpenSim/Framework/MapAndArray.cs +++ b/OpenSim/Framework/MapAndArray.cs | |||
@@ -41,14 +41,16 @@ namespace OpenSim.Framework | |||
41 | { | 41 | { |
42 | private Dictionary<TKey, TValue> m_dict; | 42 | private Dictionary<TKey, TValue> m_dict; |
43 | private TValue[] m_array; | 43 | private TValue[] m_array; |
44 | private object m_syncRoot = new object(); | 44 | private int m_lastArrayVersion; |
45 | private int m_arrayVersion; | ||
45 | 46 | ||
46 | /// <summary>Number of values currently stored in the collection</summary> | 47 | /// <summary>Number of values currently stored in the collection</summary> |
47 | public int Count { get { return m_array.Length; } } | 48 | public int Count { get { return m_dict.Count; } } |
48 | /// <summary>NOTE: This collection is thread safe. You do not need to | 49 | /// <summary>NOTE: This collection is thread safe. You do not need to |
49 | /// acquire a lock to add, remove, or enumerate entries. This | 50 | /// acquire a lock to add, remove, or enumerate entries. This |
50 | /// synchronization object should only be locked for larger | 51 | /// synchronization object should only be locked for larger |
51 | /// transactions</summary> | 52 | /// transactions</summary> |
53 | private object m_syncRoot = new object(); | ||
52 | public object SyncRoot { get { return m_syncRoot; } } | 54 | public object SyncRoot { get { return m_syncRoot; } } |
53 | 55 | ||
54 | /// <summary> | 56 | /// <summary> |
@@ -58,6 +60,8 @@ namespace OpenSim.Framework | |||
58 | { | 60 | { |
59 | m_dict = new Dictionary<TKey, TValue>(); | 61 | m_dict = new Dictionary<TKey, TValue>(); |
60 | m_array = new TValue[0]; | 62 | m_array = new TValue[0]; |
63 | m_lastArrayVersion = 0; | ||
64 | m_arrayVersion = 0; | ||
61 | } | 65 | } |
62 | 66 | ||
63 | /// <summary> | 67 | /// <summary> |
@@ -68,6 +72,8 @@ namespace OpenSim.Framework | |||
68 | { | 72 | { |
69 | m_dict = new Dictionary<TKey, TValue>(capacity); | 73 | m_dict = new Dictionary<TKey, TValue>(capacity); |
70 | m_array = new TValue[0]; | 74 | m_array = new TValue[0]; |
75 | m_lastArrayVersion = 0; | ||
76 | m_arrayVersion = 0; | ||
71 | } | 77 | } |
72 | 78 | ||
73 | /// <summary> | 79 | /// <summary> |
@@ -85,7 +91,7 @@ namespace OpenSim.Framework | |||
85 | bool containedKey = m_dict.ContainsKey(key); | 91 | bool containedKey = m_dict.ContainsKey(key); |
86 | 92 | ||
87 | m_dict[key] = value; | 93 | m_dict[key] = value; |
88 | CreateArray(); | 94 | ++m_arrayVersion; |
89 | 95 | ||
90 | return !containedKey; | 96 | return !containedKey; |
91 | } | 97 | } |
@@ -103,8 +109,8 @@ namespace OpenSim.Framework | |||
103 | lock (m_syncRoot) | 109 | lock (m_syncRoot) |
104 | { | 110 | { |
105 | m_dict.Add(key, value); | 111 | m_dict.Add(key, value); |
106 | CreateArray(); | 112 | ++m_arrayVersion; |
107 | return m_array.Length; | 113 | return m_dict.Count; |
108 | } | 114 | } |
109 | } | 115 | } |
110 | 116 | ||
@@ -118,8 +124,7 @@ namespace OpenSim.Framework | |||
118 | lock (m_syncRoot) | 124 | lock (m_syncRoot) |
119 | { | 125 | { |
120 | bool removed = m_dict.Remove(key); | 126 | bool removed = m_dict.Remove(key); |
121 | CreateArray(); | 127 | ++m_arrayVersion; |
122 | |||
123 | return removed; | 128 | return removed; |
124 | } | 129 | } |
125 | } | 130 | } |
@@ -159,6 +164,8 @@ namespace OpenSim.Framework | |||
159 | { | 164 | { |
160 | m_dict = new Dictionary<TKey, TValue>(); | 165 | m_dict = new Dictionary<TKey, TValue>(); |
161 | m_array = new TValue[0]; | 166 | m_array = new TValue[0]; |
167 | m_lastArrayVersion = 0; | ||
168 | m_arrayVersion = 0; | ||
162 | } | 169 | } |
163 | } | 170 | } |
164 | 171 | ||
@@ -170,20 +177,17 @@ namespace OpenSim.Framework | |||
170 | /// values</returns> | 177 | /// values</returns> |
171 | public TValue[] GetArray() | 178 | public TValue[] GetArray() |
172 | { | 179 | { |
173 | return m_array; | 180 | lock (m_syncRoot) |
174 | } | 181 | { |
175 | 182 | if(m_lastArrayVersion != m_arrayVersion) | |
176 | private void CreateArray() | 183 | { |
177 | { | 184 | TValue[] array = new TValue[m_dict.Count]; |
178 | // Rebuild the array from the dictionary. This method must be | 185 | m_dict.Values.CopyTo(array, 0); |
179 | // called from inside a lock | 186 | m_array = array; |
180 | TValue[] array = new TValue[m_dict.Count]; | 187 | m_lastArrayVersion = m_arrayVersion; |
181 | int i = 0; | 188 | } |
182 | 189 | return m_array; | |
183 | foreach (TValue value in m_dict.Values) | 190 | } |
184 | array[i++] = value; | ||
185 | |||
186 | m_array = array; | ||
187 | } | 191 | } |
188 | } | 192 | } |
189 | } | 193 | } |