From 72d04e7b821bd34d0f45ff2a113f132ed8bdb4c1 Mon Sep 17 00:00:00 2001 From: Konstantin Triger Date: Sun, 26 Feb 2006 18:27:43 +0000 Subject: [PATCH] make the MessageVector a synchronized collection svn path=/trunk/mcs/; revision=57316 --- .../Novell.Directory.Ldap/ChangeLog | 6 + .../Novell.Directory.Ldap/Message.cs | 13 +- .../Novell.Directory.Ldap/MessageAgent.cs | 21 ++- .../Novell.Directory.Ldap/MessageVector.cs | 124 ++++++++++++++++-- .../Novell.Directory.Ldap/SupportClass.cs | 2 +- 5 files changed, 133 insertions(+), 33 deletions(-) diff --git a/mcs/class/Novell.Directory.Ldap/Novell.Directory.Ldap/ChangeLog b/mcs/class/Novell.Directory.Ldap/Novell.Directory.Ldap/ChangeLog index bfe255f1972..3f5f42cf4cc 100644 --- a/mcs/class/Novell.Directory.Ldap/Novell.Directory.Ldap/ChangeLog +++ b/mcs/class/Novell.Directory.Ldap/Novell.Directory.Ldap/ChangeLog @@ -1,3 +1,9 @@ +2006-02-26 Konstantin Triger + + * SupportClass.cs, Message.cs, MessageAgent.cs, MessageVector.cs: increase compliancy with jldap making + the MessageVector a synchronized collection. Based on Mike Glenn and + JD Conley patches. + 2006-01-26 Konstantin Triger * Connection.cs: TARGET_JVM: catch ObjectDisposedException in addition diff --git a/mcs/class/Novell.Directory.Ldap/Novell.Directory.Ldap/Message.cs b/mcs/class/Novell.Directory.Ldap/Novell.Directory.Ldap/Message.cs index 4706750c862..998e76c72fd 100644 --- a/mcs/class/Novell.Directory.Ldap/Novell.Directory.Ldap/Message.cs +++ b/mcs/class/Novell.Directory.Ldap/Novell.Directory.Ldap/Message.cs @@ -151,7 +151,7 @@ namespace Novell.Directory.Ldap return null; } // sync on message so don't confuse with timer thread - lock (replies) + lock (replies.SyncRoot) { System.Object msg = null; while (waitForReply_Renamed_Field) @@ -160,7 +160,7 @@ namespace Novell.Directory.Ldap { try { - System.Threading.Monitor.Wait(replies); + System.Threading.Monitor.Wait(replies.SyncRoot); } catch (System.Threading.ThreadInterruptedException ir) { @@ -213,7 +213,7 @@ namespace Novell.Directory.Ldap { return null; } - lock (replies) + lock (replies.SyncRoot) { // Test and remove must be atomic if ((replies.Count == 0)) @@ -466,10 +466,7 @@ namespace Novell.Directory.Ldap { return ; } - lock(replies) - { replies.Add(message); - } message.RequestingMessage = msg; // Save request message info switch (message.Type) { @@ -544,9 +541,9 @@ namespace Novell.Directory.Ldap private void sleepersAwake() { // Notify any thread waiting for this message id - lock (replies) + lock (replies.SyncRoot) { - System.Threading.Monitor.Pulse(replies); + System.Threading.Monitor.Pulse(replies.SyncRoot); } // Notify a thread waiting for any message id agent.sleepersAwake(false); diff --git a/mcs/class/Novell.Directory.Ldap/Novell.Directory.Ldap/MessageAgent.cs b/mcs/class/Novell.Directory.Ldap/Novell.Directory.Ldap/MessageAgent.cs index 55ec2994ad5..27af28aa781 100644 --- a/mcs/class/Novell.Directory.Ldap/Novell.Directory.Ldap/MessageAgent.cs +++ b/mcs/class/Novell.Directory.Ldap/Novell.Directory.Ldap/MessageAgent.cs @@ -103,10 +103,9 @@ namespace Novell.Directory.Ldap get { int count = 0; - System.Object[] msgs = messages.ToArray(); - for (int i = 0; i < msgs.Length; i++) + for (int i = 0; i < messages.Count; i++) { - Message m = (Message) msgs[i]; + Message m = (Message) messages[i]; count += m.Count; } return count; @@ -140,15 +139,15 @@ namespace Novell.Directory.Ldap messages.Add(msgs[i]); ((Message) (msgs[i])).Agent = this; } - lock (messages) + lock (messages.SyncRoot) { if (msgs.Length > 1) { - System.Threading.Monitor.PulseAll(messages); // wake all threads waiting for messages + System.Threading.Monitor.PulseAll(messages.SyncRoot); // wake all threads waiting for messages } else if (msgs.Length == 1) { - System.Threading.Monitor.Pulse(messages); // only wake one thread + System.Threading.Monitor.Pulse(messages.SyncRoot); // only wake one thread } } return ; @@ -161,12 +160,12 @@ namespace Novell.Directory.Ldap /* package */ internal void sleepersAwake(bool all) { - lock (messages) + lock (messages.SyncRoot) { if (all) - System.Threading.Monitor.PulseAll(messages); + System.Threading.Monitor.PulseAll(messages.SyncRoot); else - System.Threading.Monitor.Pulse(messages); + System.Threading.Monitor.Pulse(messages.SyncRoot); } return ; } @@ -368,7 +367,7 @@ namespace Novell.Directory.Ldap else { // A msgId was NOT specified, any message will do - lock (messages) + lock (messages.SyncRoot) { while (true) { @@ -413,7 +412,7 @@ namespace Novell.Directory.Ldap // No data, wait for something to come in. try { - System.Threading.Monitor.Wait(messages); + System.Threading.Monitor.Wait(messages.SyncRoot); } catch (System.Threading.ThreadInterruptedException ex) { diff --git a/mcs/class/Novell.Directory.Ldap/Novell.Directory.Ldap/MessageVector.cs b/mcs/class/Novell.Directory.Ldap/Novell.Directory.Ldap/MessageVector.cs index cdbdb8ac427..fee7ab1db6a 100644 --- a/mcs/class/Novell.Directory.Ldap/Novell.Directory.Ldap/MessageVector.cs +++ b/mcs/class/Novell.Directory.Ldap/Novell.Directory.Ldap/MessageVector.cs @@ -38,8 +38,9 @@ namespace Novell.Directory.Ldap /// to Vector needed for handling messages. /// /* package */ - class MessageVector:System.Collections.ArrayList + class MessageVector:System.Collections.IList { + private readonly System.Collections.ArrayList _innerList; /// Returns an array containing all of the elements in this MessageVector. /// The elements returned are in the same order in the array as in the /// Vector. The contents of the vector are cleared. @@ -47,29 +48,25 @@ namespace Novell.Directory.Ldap /// /// the array containing all of the elements. /// - virtual internal System.Object[] ObjectArray + internal System.Object[] ObjectArray { /* package */ get { - lock (this) + lock (this.SyncRoot) { - System.Object[] results = new System.Object[Count]; - Array.Copy((System.Array) ToArray(), 0, (System.Array) results, 0, Count); - for (int i = 0; i < Count; i++) - { - ToArray()[i] = null; - } -// Count = 0; + System.Object[] results = ToArray(); + Clear(); return results; } } } /* package */ - internal MessageVector(int cap, int incr):base(cap) + internal MessageVector(int cap, int incr) { + _innerList = System.Collections.ArrayList.Synchronized(new System.Collections.ArrayList(cap)); return ; } @@ -88,12 +85,12 @@ namespace Novell.Directory.Ldap /* package */ internal Message findMessageById(int msgId) { - lock (this) + lock (this.SyncRoot) { Message msg = null; for (int i = 0; i < Count; i++) { - if ((msg = (Message) ToArray()[i]) == null) + if ((msg = (Message) this[i]) == null) { throw new System.FieldAccessException(); } @@ -105,5 +102,106 @@ namespace Novell.Directory.Ldap throw new System.FieldAccessException(); } } + + #region ArrayList members + public object[] ToArray() + { + return _innerList.ToArray(); + } + #endregion + + #region IList Members + + public int Add(object value) + { + return _innerList.Add(value); + } + + public void Clear() + { + _innerList.Clear(); + } + + public bool Contains(object value) + { + return _innerList.Contains(value); + } + + public int IndexOf(object value) + { + return _innerList.IndexOf(value); + } + + public void Insert(int index, object value) + { + _innerList.Insert(index, value); + } + + public bool IsFixedSize + { + get { return _innerList.IsFixedSize; } + } + + public bool IsReadOnly + { + get { return _innerList.IsReadOnly; } + } + + public void Remove(object value) + { + _innerList.Remove(value); + } + + public void RemoveAt(int index) + { + _innerList.RemoveAt(index); + } + + public object this[int index] + { + get + { + return _innerList[index]; + } + set + { + _innerList[index] = value; + } + } + + #endregion + + #region ICollection Members + + public void CopyTo(Array array, int index) + { + _innerList.CopyTo(array, index); + } + + public int Count + { + get { return _innerList.Count; } + } + + public bool IsSynchronized + { + get { return _innerList.IsSynchronized; } + } + + public object SyncRoot + { + get { return _innerList.SyncRoot; } + } + + #endregion + + #region IEnumerable Members + + public System.Collections.IEnumerator GetEnumerator() + { + return _innerList.GetEnumerator(); + } + + #endregion } } diff --git a/mcs/class/Novell.Directory.Ldap/Novell.Directory.Ldap/SupportClass.cs b/mcs/class/Novell.Directory.Ldap/Novell.Directory.Ldap/SupportClass.cs index 7c3b964b421..0f9033a3dbe 100644 --- a/mcs/class/Novell.Directory.Ldap/Novell.Directory.Ldap/SupportClass.cs +++ b/mcs/class/Novell.Directory.Ldap/Novell.Directory.Ldap/SupportClass.cs @@ -560,7 +560,7 @@ using System; /// The ArrayList instance /// The element to remove /// True if item is found in the ArrayList; otherwise, false - public static System.Boolean VectorRemoveElement(System.Collections.ArrayList arrayList, System.Object element) + public static System.Boolean VectorRemoveElement(System.Collections.IList arrayList, System.Object element) { System.Boolean containsItem = arrayList.Contains(element); arrayList.Remove(element); -- 2.25.1