Changed API for fetching StackFrames in bulk on multiple threads ThreadMirror.FetchFr...
[mono.git] / mcs / class / System / System.Net / SimpleAsyncResult.cs
1 //
2 // System.Net.WebAsyncResult
3 //
4 // Authors:
5 //      Gonzalo Paniagua Javier (gonzalo@ximian.com)
6 //      Martin Baulig (martin.baulig@xamarin.com)
7 //
8 // (C) 2003 Ximian, Inc (http://www.ximian.com)
9 // Copyright (c) 2014 Xamarin Inc. (http://www.xamarin.com)
10 //
11 //
12 // Permission is hereby granted, free of charge, to any person obtaining
13 // a copy of this software and associated documentation files (the
14 // "Software"), to deal in the Software without restriction, including
15 // without limitation the rights to use, copy, modify, merge, publish,
16 // distribute, sublicense, and/or sell copies of the Software, and to
17 // permit persons to whom the Software is furnished to do so, subject to
18 // the following conditions:
19 // 
20 // The above copyright notice and this permission notice shall be
21 // included in all copies or substantial portions of the Software.
22 // 
23 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
27 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
28 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
29 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
30 //
31
32 using System.IO;
33 using System.Threading;
34
35 namespace System.Net
36 {
37         delegate void SimpleAsyncCallback (SimpleAsyncResult result);
38
39         delegate bool SimpleAsyncFunc (SimpleAsyncResult result);
40
41         class SimpleAsyncResult : IAsyncResult
42         {
43                 ManualResetEvent handle;
44                 bool synch;
45                 bool isCompleted;
46                 SimpleAsyncCallback cb;
47                 object state;
48                 bool callbackDone;
49                 Exception exc;
50                 object locker = new object ();
51
52                 SimpleAsyncResult (SimpleAsyncCallback cb)
53                 {
54                         this.cb = cb;
55                 }
56
57                 protected SimpleAsyncResult (AsyncCallback cb, object state)
58                 {
59                         this.state = state;
60                         this.cb = result => {
61                                 if (cb != null)
62                                         cb (this);
63                         };
64                 }
65
66                 public static SimpleAsyncResult Run (SimpleAsyncFunc func, SimpleAsyncCallback callback)
67                 {
68                         var result = new SimpleAsyncResult (callback);
69                         try {
70                                 if (!func (result))
71                                         result.SetCompleted (true);
72                         } catch (Exception ex) {
73                                 result.SetCompleted (true, ex);
74                         }
75                         return result;
76                 }
77
78                 public static SimpleAsyncResult RunWithLock (object locker, SimpleAsyncFunc func, SimpleAsyncCallback callback)
79                 {
80                         return Run (inner => {
81                                 bool running = func (inner);
82                                 if (running)
83                                         Monitor.Exit (locker);
84                                 return running;
85                         }, inner => {
86                                 if (inner.GotException) {
87                                         if (inner.CompletedSynchronously)
88                                                 Monitor.Exit (locker);
89                                         callback (inner);
90                                         return;
91                                 }
92
93                                 try {
94                                         if (!inner.CompletedSynchronously)
95                                                 Monitor.Enter (locker);
96
97                                         callback (inner);
98                                 } finally {
99                                         Monitor.Exit (locker);
100                                 }
101                         });
102                 }
103
104                 protected void Reset_internal ()
105                 {
106                         callbackDone = false;
107                         exc = null;
108                         exc = null;
109                         lock (locker) {
110                                 isCompleted = false;
111                                 if (handle != null)
112                                         handle.Reset ();
113                         }
114                 }
115
116                 internal void SetCompleted (bool synch, Exception e)
117                 {
118                         SetCompleted_internal (synch, e);
119                         DoCallback_private ();
120                 }
121
122                 internal void SetCompleted (bool synch)
123                 {
124                         SetCompleted_internal (synch);
125                         DoCallback_private ();
126                 }
127
128                 protected void SetCompleted_internal (bool synch, Exception e)
129                 {
130                         this.synch = synch;
131                         exc = e;
132                         lock (locker) {
133                                 isCompleted = true;
134                                 if (handle != null)
135                                         handle.Set ();
136                         }
137                 }
138
139                 protected void SetCompleted_internal (bool synch)
140                 {
141                         this.synch = synch;
142                         exc = null;
143                         lock (locker) {
144                                 isCompleted = true;
145                                 if (handle != null)
146                                         handle.Set ();
147                         }
148                 }
149
150                 void DoCallback_private ()
151                 {
152                         if (callbackDone)
153                                 throw new InvalidOperationException ();
154                         callbackDone = true;
155                         if (cb == null)
156                                 return;
157                         cb (this);
158                 }
159
160                 protected void DoCallback_internal ()
161                 {
162                         if (!callbackDone && cb != null) {
163                                 callbackDone = true;
164                                 if (true || synch)
165                                         cb (this);
166                                 else
167                                         ThreadPool.QueueUserWorkItem (CB, null);
168                         }
169                 }
170
171                 void CB (object unused)
172                 {
173                         cb (this);
174                 }
175
176                 internal void WaitUntilComplete ()
177                 {
178                         if (IsCompleted)
179                                 return;
180
181                         AsyncWaitHandle.WaitOne ();
182                 }
183
184                 internal bool WaitUntilComplete (int timeout, bool exitContext)
185                 {
186                         if (IsCompleted)
187                                 return true;
188
189                         return AsyncWaitHandle.WaitOne (timeout, exitContext);
190                 }
191
192                 public object AsyncState {
193                         get { return state; }
194                 }
195
196                 public WaitHandle AsyncWaitHandle {
197                         get {
198                                 lock (locker) {
199                                         if (handle == null)
200                                                 handle = new ManualResetEvent (isCompleted);
201                                 }
202
203                                 return handle;
204                         }
205                 }
206
207                 public bool CompletedSynchronously {
208                         get { return synch; }
209                 }
210
211                 public bool IsCompleted {
212                         get {
213                                 lock (locker) {
214                                         return isCompleted;
215                                 }
216                         }
217                 }
218
219                 internal bool GotException {
220                         get { return (exc != null); }
221                 }
222
223                 internal Exception Exception {
224                         get { return exc; }
225                 }
226         }
227 }
228