aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/OpenSim/Framework/Communications/RestClient/GenericAsyncResult.cs
diff options
context:
space:
mode:
Diffstat (limited to 'OpenSim/Framework/Communications/RestClient/GenericAsyncResult.cs')
-rw-r--r--OpenSim/Framework/Communications/RestClient/GenericAsyncResult.cs326
1 files changed, 163 insertions, 163 deletions
diff --git a/OpenSim/Framework/Communications/RestClient/GenericAsyncResult.cs b/OpenSim/Framework/Communications/RestClient/GenericAsyncResult.cs
index 4be459d..55456ae 100644
--- a/OpenSim/Framework/Communications/RestClient/GenericAsyncResult.cs
+++ b/OpenSim/Framework/Communications/RestClient/GenericAsyncResult.cs
@@ -1,163 +1,163 @@
1using System; 1using System;
2using System.Collections.Generic; 2using System.Collections.Generic;
3using System.Text; 3using System.Text;
4using System.Threading; 4using System.Threading;
5 5
6namespace OpenSim.Framework.RestClient 6namespace OpenSim.Framework.RestClient
7{ 7{
8 internal class SimpleAsyncResult : IAsyncResult 8 internal class SimpleAsyncResult : IAsyncResult
9 { 9 {
10 10
11 private readonly AsyncCallback m_callback; 11 private readonly AsyncCallback m_callback;
12 12
13 /// <summary> 13 /// <summary>
14 /// Is process completed? 14 /// Is process completed?
15 /// </summary> 15 /// </summary>
16 /// <remarks>Should really be boolean, but VolatileRead has no boolean method</remarks> 16 /// <remarks>Should really be boolean, but VolatileRead has no boolean method</remarks>
17 private byte m_completed; 17 private byte m_completed;
18 18
19 /// <summary> 19 /// <summary>
20 /// Did process complete synchroneously? 20 /// Did process complete synchroneously?
21 /// </summary> 21 /// </summary>
22 /// <remarks>I have a hard time imagining a scenario where this is the case, again, same issue about 22 /// <remarks>I have a hard time imagining a scenario where this is the case, again, same issue about
23 /// booleans and VolatileRead as m_completed 23 /// booleans and VolatileRead as m_completed
24 /// </remarks> 24 /// </remarks>
25 private byte m_completedSynchronously; 25 private byte m_completedSynchronously;
26 26
27 private readonly object m_asyncState; 27 private readonly object m_asyncState;
28 private ManualResetEvent m_waitHandle; 28 private ManualResetEvent m_waitHandle;
29 private Exception m_exception; 29 private Exception m_exception;
30 30
31 internal SimpleAsyncResult(AsyncCallback cb, object state) 31 internal SimpleAsyncResult(AsyncCallback cb, object state)
32 { 32 {
33 m_callback = cb; 33 m_callback = cb;
34 m_asyncState = state; 34 m_asyncState = state;
35 m_completed = 0; 35 m_completed = 0;
36 m_completedSynchronously = 1; 36 m_completedSynchronously = 1;
37 } 37 }
38 38
39 39
40 #region IAsyncResult Members 40 #region IAsyncResult Members
41 41
42 public object AsyncState 42 public object AsyncState
43 { 43 {
44 get { return m_asyncState; } 44 get { return m_asyncState; }
45 } 45 }
46 46
47 47
48 48
49 public WaitHandle AsyncWaitHandle 49 public WaitHandle AsyncWaitHandle
50 { 50 {
51 get 51 get
52 { 52 {
53 if (m_waitHandle == null) 53 if (m_waitHandle == null)
54 { 54 {
55 bool done = IsCompleted; 55 bool done = IsCompleted;
56 ManualResetEvent mre = new ManualResetEvent(done); 56 ManualResetEvent mre = new ManualResetEvent(done);
57 if (Interlocked.CompareExchange(ref m_waitHandle, mre, null) != null) 57 if (Interlocked.CompareExchange(ref m_waitHandle, mre, null) != null)
58 { 58 {
59 mre.Close(); 59 mre.Close();
60 } 60 }
61 else 61 else
62 { 62 {
63 if (!done && IsCompleted) 63 if (!done && IsCompleted)
64 { 64 {
65 m_waitHandle.Set(); 65 m_waitHandle.Set();
66 } 66 }
67 } 67 }
68 } 68 }
69 return m_waitHandle; 69 return m_waitHandle;
70 } 70 }
71 } 71 }
72 72
73 73
74 public bool CompletedSynchronously 74 public bool CompletedSynchronously
75 { 75 {
76 get { return Thread.VolatileRead(ref m_completedSynchronously) == 1; } 76 get { return Thread.VolatileRead(ref m_completedSynchronously) == 1; }
77 } 77 }
78 78
79 79
80 public bool IsCompleted 80 public bool IsCompleted
81 { 81 {
82 get { return Thread.VolatileRead(ref m_completed) == 1; } 82 get { return Thread.VolatileRead(ref m_completed) == 1; }
83 } 83 }
84 84
85 85
86 #endregion 86 #endregion
87 87
88 88
89 #region class Methods 89 #region class Methods
90 internal void SetAsCompleted(bool completedSynchronously) 90 internal void SetAsCompleted(bool completedSynchronously)
91 { 91 {
92 m_completed = 1; 92 m_completed = 1;
93 if(completedSynchronously) 93 if(completedSynchronously)
94 m_completedSynchronously = 1; 94 m_completedSynchronously = 1;
95 else 95 else
96 m_completedSynchronously = 0; 96 m_completedSynchronously = 0;
97 97
98 SignalCompletion(); 98 SignalCompletion();
99 } 99 }
100 100
101 internal void HandleException(Exception e, bool completedSynchronously) 101 internal void HandleException(Exception e, bool completedSynchronously)
102 { 102 {
103 m_completed = 1; 103 m_completed = 1;
104 if (completedSynchronously) 104 if (completedSynchronously)
105 m_completedSynchronously = 1; 105 m_completedSynchronously = 1;
106 else 106 else
107 m_completedSynchronously = 0; 107 m_completedSynchronously = 0;
108 m_exception = e; 108 m_exception = e;
109 109
110 SignalCompletion(); 110 SignalCompletion();
111 } 111 }
112 112
113 private void SignalCompletion() 113 private void SignalCompletion()
114 { 114 {
115 if(m_waitHandle != null) m_waitHandle.Set(); 115 if(m_waitHandle != null) m_waitHandle.Set();
116 116
117 if(m_callback != null) m_callback(this); 117 if(m_callback != null) m_callback(this);
118 } 118 }
119 119
120 public void EndInvoke() 120 public void EndInvoke()
121 { 121 {
122 // This method assumes that only 1 thread calls EndInvoke 122 // This method assumes that only 1 thread calls EndInvoke
123 if (!IsCompleted) 123 if (!IsCompleted)
124 { 124 {
125 // If the operation isn't done, wait for it 125 // If the operation isn't done, wait for it
126 AsyncWaitHandle.WaitOne(); 126 AsyncWaitHandle.WaitOne();
127 AsyncWaitHandle.Close(); 127 AsyncWaitHandle.Close();
128 m_waitHandle = null; // Allow early GC 128 m_waitHandle = null; // Allow early GC
129 } 129 }
130 130
131 // Operation is done: if an exception occured, throw it 131 // Operation is done: if an exception occured, throw it
132 if (m_exception != null) throw m_exception; 132 if (m_exception != null) throw m_exception;
133 } 133 }
134 134
135 #endregion 135 #endregion
136 } 136 }
137 137
138 internal class AsyncResult<T> : SimpleAsyncResult 138 internal class AsyncResult<T> : SimpleAsyncResult
139 { 139 {
140 private T m_result = default(T); 140 private T m_result = default(T);
141 141
142 public AsyncResult(AsyncCallback asyncCallback, Object state) : 142 public AsyncResult(AsyncCallback asyncCallback, Object state) :
143 base(asyncCallback, state) { } 143 base(asyncCallback, state) { }
144 144
145 145
146 public void SetAsCompleted(T result, bool completedSynchronously) 146 public void SetAsCompleted(T result, bool completedSynchronously)
147 { 147 {
148 // Save the asynchronous operation's result 148 // Save the asynchronous operation's result
149 m_result = result; 149 m_result = result;
150 150
151 // Tell the base class that the operation completed 151 // Tell the base class that the operation completed
152 // sucessfully (no exception) 152 // sucessfully (no exception)
153 base.SetAsCompleted(completedSynchronously); 153 base.SetAsCompleted(completedSynchronously);
154 } 154 }
155 155
156 new public T EndInvoke() 156 new public T EndInvoke()
157 { 157 {
158 base.EndInvoke(); 158 base.EndInvoke();
159 return m_result; 159 return m_result;
160 } 160 }
161 161
162 } 162 }
163} 163}