Author: gonzalo
Date: 2006-03-06 13:46:00 -0500 (Mon, 06 Mar 2006)
New Revision: 57625

Modified:
   trunk/mcs/class/Mono.Security/Mono.Security.Protocol.Tls/ChangeLog
   trunk/mcs/class/Mono.Security/Mono.Security.Protocol.Tls/RecordProtocol.cs
Log:
2006-03-06 Gonzalo Paniagua Javier <[EMAIL PROTECTED]>

        * RecordProtocol.cs: avoid creating the ManualResetEvent whenever
        possible. Don't lock on an instance of an object that the user can get.



Modified: trunk/mcs/class/Mono.Security/Mono.Security.Protocol.Tls/ChangeLog
===================================================================
--- trunk/mcs/class/Mono.Security/Mono.Security.Protocol.Tls/ChangeLog  
2006-03-06 18:20:53 UTC (rev 57624)
+++ trunk/mcs/class/Mono.Security/Mono.Security.Protocol.Tls/ChangeLog  
2006-03-06 18:46:00 UTC (rev 57625)
@@ -1,3 +1,8 @@
+2006-03-06 Gonzalo Paniagua Javier <[EMAIL PROTECTED]>
+
+       * RecordProtocol.cs: avoid creating the ManualResetEvent whenever
+       possible. Don't lock on an instance of an object that the user can get.
+
 2006-03-02  Sebastien Pouliot  <[EMAIL PROTECTED]>
 
        * RecordProtocol.cs: Fix possible endless loop (#77663). Remove hack

Modified: 
trunk/mcs/class/Mono.Security/Mono.Security.Protocol.Tls/RecordProtocol.cs
===================================================================
--- trunk/mcs/class/Mono.Security/Mono.Security.Protocol.Tls/RecordProtocol.cs  
2006-03-06 18:20:53 UTC (rev 57624)
+++ trunk/mcs/class/Mono.Security/Mono.Security.Protocol.Tls/RecordProtocol.cs  
2006-03-06 18:46:00 UTC (rev 57625)
@@ -87,12 +87,14 @@
                #region Receive Record Async Result
                private class ReceiveRecordAsyncResult : IAsyncResult
                {
+                       private object locker = new object ();
                        private AsyncCallback _userCallback;
                        private object _userState;
                        private Exception _asyncException;
-                       private ManualResetEvent _complete;
+                       private ManualResetEvent handle;
                        private byte[] _resultingBuffer;
                        private Stream _record;
+                       private bool completed;
 
                        private byte[] _initialBuffer;
 
@@ -100,7 +102,6 @@
                        {
                                _userCallback = userCallback;
                                _userState = userState;
-                               _complete = new ManualResetEvent(false);
                                _initialBuffer = initialBuffer;
                                _record = record;
                        }
@@ -132,12 +133,24 @@
 
                        public bool CompletedWithError
                        {
-                               get { return null != _asyncException; }
+                               get {
+                                       if (!IsCompleted)
+                                               return false; // Perhaps throw 
InvalidOperationExcetion?
+
+                                       return null != _asyncException;
+                               }
                        }
 
                        public WaitHandle AsyncWaitHandle
                        {
-                               get { return _complete; }
+                               get {
+                                       lock (locker) {
+                                               if (handle == null)
+                                                       handle = new 
ManualResetEvent (completed);
+                                       }
+                                       return handle;
+                               }
+                               
                        }
 
                        public bool CompletedSynchronously
@@ -147,27 +160,29 @@
 
                        public bool IsCompleted
                        {
-                               get { return _complete.WaitOne(0, false); }
+                               get {
+                                       lock (locker) {
+                                               return completed;
+                                       }
+                               }
                        }
 
                        private void SetComplete(Exception ex, byte[] 
resultingBuffer)
                        {
-                               if (this.IsCompleted)
-                                       return;
-
-                               lock (this)
-                               {
-                                       if (this.IsCompleted)
+                               lock (locker) {
+                                       if (completed)
                                                return;
 
+                                       completed = true;
+                                       if (handle != null)
+                                               handle.Set ();
+
+                                       if (_userCallback != null)
+                                               _userCallback.BeginInvoke 
(this, null, null);
+
                                        _asyncException = ex;
                                        _resultingBuffer = resultingBuffer;
-                                       _complete.Set();
                                }
-
-                               if (null != _userCallback)
-                                       _userCallback (this);
-
                        }
 
                        public void SetComplete(Exception ex)
@@ -190,17 +205,18 @@
                #region Receive Record Async Result
                private class SendRecordAsyncResult : IAsyncResult
                {
+                       private object locker = new object ();
                        private AsyncCallback _userCallback;
                        private object _userState;
                        private Exception _asyncException;
-                       private ManualResetEvent _complete;
+                       private ManualResetEvent handle;
                        private HandshakeMessage _message;
+                       private bool completed;
 
                        public SendRecordAsyncResult(AsyncCallback 
userCallback, object userState, HandshakeMessage message)
                        {
                                _userCallback = userCallback;
                                _userState = userState;
-                               _complete = new ManualResetEvent(false);
                                _message = message;
                        }
 
@@ -221,12 +237,24 @@
 
                        public bool CompletedWithError
                        {
-                               get { return null != _asyncException; }
+                               get {
+                                       if (!IsCompleted)
+                                               return false; // Perhaps throw 
InvalidOperationExcetion?
+
+                                       return null != _asyncException;
+                               }
                        }
 
                        public WaitHandle AsyncWaitHandle
                        {
-                               get { return _complete; }
+                               get {
+                                       lock (locker) {
+                                               if (handle == null)
+                                                       handle = new 
ManualResetEvent (completed);
+                                       }
+                                       return handle;
+                               }
+                               
                        }
 
                        public bool CompletedSynchronously
@@ -236,26 +264,28 @@
 
                        public bool IsCompleted
                        {
-                               get { return _complete.WaitOne(0, false); }
+                               get {
+                                       lock (locker) {
+                                               return completed;
+                                       }
+                               }
                        }
 
                        public void SetComplete(Exception ex)
                        {
-                               if (this.IsCompleted)
-                                       return;
-
-                               lock (this)
-                               {
-                                       if (this.IsCompleted)
+                               lock (locker) {
+                                       if (completed)
                                                return;
 
+                                       completed = true;
+                                       if (handle != null)
+                                               handle.Set ();
+
+                                       if (_userCallback != null)
+                                               _userCallback.BeginInvoke 
(this, null, null);
+
                                        _asyncException = ex;
-                                       _complete.Set();
                                }
-
-                               if (null != _userCallback)
-                                       _userCallback (this);
-
                        }
 
                        public void SetComplete()
@@ -385,7 +415,8 @@
                        if (null == internalResult)
                                throw new ArgumentException("Either the 
provided async result is null or was not created by this RecordProtocol.");
 
-                       internalResult.AsyncWaitHandle.WaitOne();
+                       if (!internalResult.IsCompleted)
+                               internalResult.AsyncWaitHandle.WaitOne();
 
                        if (internalResult.CompletedWithError)
                                throw internalResult.AsyncException;
@@ -662,7 +693,8 @@
                        if (asyncResult is SendRecordAsyncResult)
                        {
                                SendRecordAsyncResult internalResult = 
asyncResult as SendRecordAsyncResult;
-                               internalResult.AsyncWaitHandle.WaitOne();
+                               if (!internalResult.IsCompleted)
+                                       
internalResult.AsyncWaitHandle.WaitOne();
                                if (internalResult.CompletedWithError)
                                        throw internalResult.AsyncException;
                        }

_______________________________________________
Mono-patches maillist  -  [email protected]
http://lists.ximian.com/mailman/listinfo/mono-patches

Reply via email to