GenericAsyncResult.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. using System;
  2. using System.Threading;
  3. namespace OpenSim.Framework.Communications
  4. {
  5. internal class SimpleAsyncResult : IAsyncResult
  6. {
  7. private readonly AsyncCallback m_callback;
  8. /// <summary>
  9. /// Is process completed?
  10. /// </summary>
  11. /// <remarks>Should really be boolean, but VolatileRead has no boolean method</remarks>
  12. private byte m_completed;
  13. /// <summary>
  14. /// Did process complete synchroneously?
  15. /// </summary>
  16. /// <remarks>I have a hard time imagining a scenario where this is the case, again, same issue about
  17. /// booleans and VolatileRead as m_completed
  18. /// </remarks>
  19. private byte m_completedSynchronously;
  20. private readonly object m_asyncState;
  21. private ManualResetEvent m_waitHandle;
  22. private Exception m_exception;
  23. internal SimpleAsyncResult(AsyncCallback cb, object state)
  24. {
  25. m_callback = cb;
  26. m_asyncState = state;
  27. m_completed = 0;
  28. m_completedSynchronously = 1;
  29. }
  30. #region IAsyncResult Members
  31. public object AsyncState
  32. {
  33. get { return m_asyncState; }
  34. }
  35. public WaitHandle AsyncWaitHandle
  36. {
  37. get
  38. {
  39. if (m_waitHandle == null)
  40. {
  41. bool done = IsCompleted;
  42. ManualResetEvent mre = new ManualResetEvent(done);
  43. if (Interlocked.CompareExchange(ref m_waitHandle, mre, null) != null)
  44. {
  45. mre.Close();
  46. }
  47. else
  48. {
  49. if (!done && IsCompleted)
  50. {
  51. m_waitHandle.Set();
  52. }
  53. }
  54. }
  55. return m_waitHandle;
  56. }
  57. }
  58. public bool CompletedSynchronously
  59. {
  60. get { return Thread.VolatileRead(ref m_completedSynchronously) == 1; }
  61. }
  62. public bool IsCompleted
  63. {
  64. get { return Thread.VolatileRead(ref m_completed) == 1; }
  65. }
  66. #endregion
  67. #region class Methods
  68. internal void SetAsCompleted(bool completedSynchronously)
  69. {
  70. m_completed = 1;
  71. if (completedSynchronously)
  72. m_completedSynchronously = 1;
  73. else
  74. m_completedSynchronously = 0;
  75. SignalCompletion();
  76. }
  77. internal void HandleException(Exception e, bool completedSynchronously)
  78. {
  79. m_completed = 1;
  80. if (completedSynchronously)
  81. m_completedSynchronously = 1;
  82. else
  83. m_completedSynchronously = 0;
  84. m_exception = e;
  85. SignalCompletion();
  86. }
  87. private void SignalCompletion()
  88. {
  89. if (m_waitHandle != null) m_waitHandle.Set();
  90. if (m_callback != null) m_callback(this);
  91. }
  92. public void EndInvoke()
  93. {
  94. // This method assumes that only 1 thread calls EndInvoke
  95. if (!IsCompleted)
  96. {
  97. // If the operation isn't done, wait for it
  98. AsyncWaitHandle.WaitOne();
  99. AsyncWaitHandle.Close();
  100. m_waitHandle = null; // Allow early GC
  101. }
  102. // Operation is done: if an exception occured, throw it
  103. if (m_exception != null) throw m_exception;
  104. }
  105. #endregion
  106. }
  107. internal class AsyncResult<T> : SimpleAsyncResult
  108. {
  109. private T m_result = default(T);
  110. public AsyncResult(AsyncCallback asyncCallback, Object state) :
  111. base(asyncCallback, state)
  112. {
  113. }
  114. public void SetAsCompleted(T result, bool completedSynchronously)
  115. {
  116. // Save the asynchronous operation's result
  117. m_result = result;
  118. // Tell the base class that the operation completed
  119. // sucessfully (no exception)
  120. base.SetAsCompleted(completedSynchronously);
  121. }
  122. public new T EndInvoke()
  123. {
  124. base.EndInvoke();
  125. return m_result;
  126. }
  127. }
  128. }