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