GenericAsyncResult.cs 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. /*
  2. * Copyright (c) Contributors, http://opensimulator.org/
  3. * See CONTRIBUTORS.TXT for a full list of copyright holders.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. * * Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * * Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. * * Neither the name of the OpenSimulator Project nor the
  13. * names of its contributors may be used to endorse or promote products
  14. * derived from this software without specific prior written permission.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY
  17. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  18. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  19. * DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY
  20. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  21. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  22. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  23. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  24. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  25. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. using System;
  28. using System.Threading;
  29. namespace OpenSim.Framework.Communications
  30. {
  31. internal class SimpleAsyncResult : IAsyncResult
  32. {
  33. private readonly AsyncCallback m_callback;
  34. /// <summary>
  35. /// Is process completed?
  36. /// </summary>
  37. /// <remarks>Should really be boolean, but VolatileRead has no boolean method</remarks>
  38. private byte m_completed;
  39. /// <summary>
  40. /// Did process complete synchronously?
  41. /// </summary>
  42. /// <remarks>I have a hard time imagining a scenario where this is the case, again, same issue about
  43. /// booleans and VolatileRead as m_completed
  44. /// </remarks>
  45. private byte m_completedSynchronously;
  46. private readonly object m_asyncState;
  47. private ManualResetEvent m_waitHandle;
  48. private Exception m_exception;
  49. internal SimpleAsyncResult(AsyncCallback cb, object state)
  50. {
  51. m_callback = cb;
  52. m_asyncState = state;
  53. m_completed = 0;
  54. m_completedSynchronously = 1;
  55. }
  56. #region IAsyncResult Members
  57. public object AsyncState
  58. {
  59. get { return m_asyncState; }
  60. }
  61. public WaitHandle AsyncWaitHandle
  62. {
  63. get
  64. {
  65. if (m_waitHandle == null)
  66. {
  67. bool done = IsCompleted;
  68. ManualResetEvent mre = new ManualResetEvent(done);
  69. if (Interlocked.CompareExchange(ref m_waitHandle, mre, null) != null)
  70. {
  71. mre.Close();
  72. }
  73. else
  74. {
  75. if (!done && IsCompleted)
  76. {
  77. m_waitHandle.Set();
  78. }
  79. }
  80. }
  81. return m_waitHandle;
  82. }
  83. }
  84. public bool CompletedSynchronously
  85. {
  86. get { return Thread.VolatileRead(ref m_completedSynchronously) == 1; }
  87. }
  88. public bool IsCompleted
  89. {
  90. get { return Thread.VolatileRead(ref m_completed) == 1; }
  91. }
  92. #endregion
  93. #region class Methods
  94. internal void SetAsCompleted(bool completedSynchronously)
  95. {
  96. m_completed = 1;
  97. if (completedSynchronously)
  98. m_completedSynchronously = 1;
  99. else
  100. m_completedSynchronously = 0;
  101. SignalCompletion();
  102. }
  103. internal void HandleException(Exception e, bool completedSynchronously)
  104. {
  105. m_completed = 1;
  106. if (completedSynchronously)
  107. m_completedSynchronously = 1;
  108. else
  109. m_completedSynchronously = 0;
  110. m_exception = e;
  111. SignalCompletion();
  112. }
  113. private void SignalCompletion()
  114. {
  115. if (m_waitHandle != null) m_waitHandle.Set();
  116. if (m_callback != null) m_callback(this);
  117. }
  118. public void EndInvoke()
  119. {
  120. // This method assumes that only 1 thread calls EndInvoke
  121. if (!IsCompleted)
  122. {
  123. // If the operation isn't done, wait for it
  124. AsyncWaitHandle.WaitOne();
  125. AsyncWaitHandle.Close();
  126. m_waitHandle.Close();
  127. m_waitHandle = null; // Allow early GC
  128. }
  129. // Operation is done: if an exception occured, throw it
  130. if (m_exception != null) throw m_exception;
  131. }
  132. #endregion
  133. }
  134. internal class AsyncResult<T> : SimpleAsyncResult
  135. {
  136. private T m_result = default(T);
  137. public AsyncResult(AsyncCallback asyncCallback, Object state) :
  138. base(asyncCallback, state)
  139. {
  140. }
  141. public void SetAsCompleted(T result, bool completedSynchronously)
  142. {
  143. // Save the asynchronous operation's result
  144. m_result = result;
  145. // Tell the base class that the operation completed
  146. // sucessfully (no exception)
  147. base.SetAsCompleted(completedSynchronously);
  148. }
  149. public new T EndInvoke()
  150. {
  151. base.EndInvoke();
  152. return m_result;
  153. }
  154. }
  155. }