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