2010-03-12 Jb Evain <jbevain@novell.com>
[mono.git] / mcs / class / corlib / System.Threading.Tasks / TaskFactory.cs
1 #if NET_4_0
2 // 
3 // TaskFactory.cs
4 //  
5 // Author:
6 //       Jérémie "Garuma" Laval <jeremie.laval@gmail.com>
7 // 
8 // Copyright (c) 2009 Jérémie "Garuma" Laval
9 // 
10 // Permission is hereby granted, free of charge, to any person obtaining a copy
11 // of this software and associated documentation files (the "Software"), to deal
12 // in the Software without restriction, including without limitation the rights
13 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
14 // copies of the Software, and to permit persons to whom the Software is
15 // furnished to do so, subject to the following conditions:
16 // 
17 // The above copyright notice and this permission notice shall be included in
18 // all copies or substantial portions of the Software.
19 // 
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
23 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
26 // THE SOFTWARE.
27
28 using System;
29 using System.Threading;
30
31 namespace System.Threading.Tasks
32 {
33         
34         public class TaskFactory
35         {
36                 TaskScheduler scheduler;
37                 TaskCreationOptions options;
38                 TaskContinuationOptions contOptions;
39                 CancellationToken token;
40                 
41                 #region ctors
42                 public TaskFactory ()
43                         : this (CancellationToken.None, TaskCreationOptions.None, TaskContinuationOptions.None, TaskScheduler.Current)
44                 {
45                 }
46
47                 public TaskFactory (CancellationToken token)
48                         : this (token, TaskCreationOptions.None, TaskContinuationOptions.None, TaskScheduler.Current)
49                 {       
50                 }
51
52                 public TaskFactory (TaskScheduler scheduler)
53                         : this (CancellationToken.None, TaskCreationOptions.None, TaskContinuationOptions.None, scheduler)
54                 {       
55                 }
56                 
57                 public TaskFactory (TaskCreationOptions options, TaskContinuationOptions contOptions)
58                         : this (CancellationToken.None, options, contOptions, TaskScheduler.Current)
59                 {       
60                 }
61                 
62                 public TaskFactory (CancellationToken token, TaskCreationOptions options, TaskContinuationOptions contOptions,
63                                     TaskScheduler scheduler)
64                 {
65                         this.token = token;
66                         this.scheduler = scheduler;
67                         this.options = options;
68                         this.contOptions = contOptions;
69                 }
70                 #endregion
71                 
72                 #region StartNew for Task
73                 public Task StartNew (Action action)
74                 {
75                         return StartNew (action, token, options, scheduler);
76                 }
77                 
78                 public Task StartNew (Action action, CancellationToken token)
79                 {
80                         return StartNew (action, token, options, scheduler);
81                 }
82                 
83                 public Task StartNew (Action action, TaskCreationOptions options)
84                 {
85                         return StartNew (action, token, options, scheduler);
86                 }
87                 
88                 public Task StartNew (Action<object> action, object state)
89                 {
90                         return StartNew (action, state, token, options, scheduler);
91                 }
92                 
93                 public Task StartNew (Action<object> action, object state, CancellationToken token)
94                 {
95                         return StartNew (action, state, token, options, scheduler);
96                 }
97                 
98                 public Task StartNew (Action<object> action, object state, TaskCreationOptions options)
99                 {
100                         return StartNew (action, state, token, options, scheduler);
101                 }
102                 
103                 public Task StartNew (Action action, CancellationToken token, TaskCreationOptions options, TaskScheduler scheduler)
104                 {
105                         return StartNew ((o) => action (), null, token, options, scheduler);
106                 }
107                 
108                 public Task StartNew (Action<object> action, object state, CancellationToken token, TaskCreationOptions options,
109                                       TaskScheduler scheduler)
110                 {
111                         Task t = new Task (action, state, options);
112                         t.Start (scheduler);
113                         
114                         return t;
115                 }
116                 #endregion
117                 
118                 #region StartNew for Task<TResult>      
119                 public Task<TResult> StartNew<TResult> (Func<TResult> function)
120                 {
121                         return StartNew<TResult> (function, token, options, scheduler);
122                 }
123                 
124                 public Task<TResult> StartNew<TResult> (Func<TResult> function, TaskCreationOptions options)
125                 {
126                         return StartNew<TResult> (function, token, options, scheduler);
127
128                 }
129                 
130                 public Task<TResult> StartNew<TResult> (Func<TResult> function, CancellationToken token)
131                 {
132                         return StartNew<TResult> (function, token, options, scheduler);
133                 }
134                 
135                 public Task<TResult> StartNew<TResult> (Func<TResult> function,
136                                                         CancellationToken token,
137                                                         TaskCreationOptions options,
138                                                         TaskScheduler scheduler)
139                 {
140                         return StartNew<TResult> ((o) => function (), null, token, options, scheduler);
141                 }
142                 
143                 public Task<TResult> StartNew<TResult> (Func<object, TResult> function, object state)
144                 {
145                         return StartNew<TResult> (function, state, token, options, scheduler);
146                 }
147                 
148                 public Task<TResult> StartNew<TResult> (Func<object, TResult> function, object state, CancellationToken token)
149                 {
150                         return StartNew<TResult> (function, state, token, options, scheduler);
151                 }
152                 
153                 public Task<TResult> StartNew<TResult> (Func<object, TResult> function, object state, TaskCreationOptions options)
154                 {
155                         return StartNew<TResult> (function, state, token, options, scheduler);
156                 }
157                 
158                 public Task<TResult> StartNew<TResult> (Func<object, TResult> function, object state,
159                                                         CancellationToken token,
160                                                         TaskCreationOptions options,
161                                                         TaskScheduler scheduler)
162                 {
163                         Task<TResult> t = new Task<TResult> (function, state, token, options);
164                         t.Start (scheduler);
165                         
166                         return t;
167                 }
168                 #endregion
169                 
170                 #region Continue
171                 
172                 public Task ContinueWhenAny (Task[] tasks, Action<Task> continuationAction)
173                 {
174                         return ContinueWhenAny (tasks, continuationAction, token, contOptions, scheduler);
175                 }
176                 
177                 public Task ContinueWhenAny (Task[] tasks, Action<Task> continuationAction, CancellationToken token)
178                 {
179                         return ContinueWhenAny (tasks, continuationAction, token, contOptions, scheduler);
180                 }
181                 
182                 public Task ContinueWhenAny (Task[] tasks, Action<Task> continuationAction, TaskContinuationOptions continuationOptions)
183                 {
184                         return ContinueWhenAny (tasks, continuationAction, token, continuationOptions, scheduler);
185                 }
186
187                 public Task ContinueWhenAny (Task[] tasks, Action<Task> continuationAction, CancellationToken token, 
188                                              TaskContinuationOptions continuationOptions, TaskScheduler scheduler)
189                 {
190                         AtomicBoolean trigger = new AtomicBoolean ();
191                         Task commonContinuation = new Task (null);
192                         
193                         foreach (Task t in tasks) {
194                                 Task cont = new Task ((o) => { continuationAction ((Task)o); }, t, token, options);
195                                 t.ContinueWithCore (cont, continuationOptions, scheduler, trigger.TrySet);
196                                 cont.ContinueWithCore (commonContinuation, TaskContinuationOptions.None, scheduler);
197                         }
198                         
199                         return commonContinuation;
200                 }
201                 
202                 public Task ContinueWhenAny<TAntecedentResult> (Task<TAntecedentResult>[] tasks, Action<Task<TAntecedentResult>> continuationAction)
203                 {
204                         return ContinueWhenAny (tasks, continuationAction, token, contOptions, scheduler);
205                 }
206                 
207                 public Task ContinueWhenAny<TAntecedentResult> (Task<TAntecedentResult>[] tasks, Action<Task<TAntecedentResult>> continuationAction,
208                                                                 CancellationToken token)
209                 {
210                         return ContinueWhenAny (tasks, continuationAction, token, contOptions, scheduler);
211                 }
212                 
213                 public Task ContinueWhenAny<TAntecedentResult> (Task<TAntecedentResult>[] tasks, Action<Task<TAntecedentResult>> continuationAction,
214                                                                 TaskContinuationOptions continuationOptions)
215                 {
216                         return ContinueWhenAny (tasks, continuationAction, token, continuationOptions, scheduler);
217                 }
218
219                 public Task ContinueWhenAny<TAntecedentResult> (Task<TAntecedentResult>[] tasks, Action<Task<TAntecedentResult>> continuationAction,
220                                                                 CancellationToken token, TaskContinuationOptions continuationOptions,
221                                                                 TaskScheduler scheduler)
222                 {
223                         return ContinueWhenAny ((Task[]) tasks, (o) => continuationAction ((Task<TAntecedentResult>)o),
224                                                 token, continuationOptions, scheduler);
225                 }
226                 
227                 [MonoTODO]
228                 public Task<TResult> ContinueWhenAny<TResult> (Task[] tasks, Func<Task, TResult> continuationAction)
229                 {
230                         return ContinueWhenAny (tasks, continuationAction, token, contOptions, scheduler);
231                 }
232                 
233                 [MonoTODO]
234                 public Task<TResult> ContinueWhenAny<TResult> (Task[] tasks, Func<Task, TResult> continuationAction,
235                                                                CancellationToken token)
236                 {
237                         return ContinueWhenAny (tasks, continuationAction, token, contOptions, scheduler);
238                 }
239                 
240                 [MonoTODO]
241                 public Task<TResult> ContinueWhenAny<TResult> (Task[] tasks, Func<Task, TResult> continuationAction,
242                                                                TaskContinuationOptions continuationOptions)
243                 {
244                         return ContinueWhenAny (tasks, continuationAction, token, continuationOptions, scheduler);
245                 }
246
247                 [MonoTODO]
248                 public Task<TResult> ContinueWhenAny<TResult> (Task[] tasks, Func<Task, TResult> continuationAction,
249                                                                CancellationToken token,
250                                                                TaskContinuationOptions continuationOptions,
251                                                                TaskScheduler scheduler)
252                 {
253                         throw new NotImplementedException ();
254                 }
255                 
256                 [MonoTODO]
257                 public Task<TResult> ContinueWhenAny<TAntecedentResult, TResult> (Task<TAntecedentResult>[] tasks,
258                                                                                   Func<Task<TAntecedentResult>, TResult> continuationAction)
259                 {
260                         return ContinueWhenAny (tasks, continuationAction, token, contOptions, scheduler);
261                 }
262
263                 [MonoTODO]
264                 public Task<TResult> ContinueWhenAny<TAntecedentResult, TResult> (Task<TAntecedentResult>[] tasks,
265                                                                                   Func<Task<TAntecedentResult>, TResult> continuationAction,
266                                                                                   CancellationToken token)
267                 {
268                         return ContinueWhenAny (tasks, continuationAction, token, contOptions, scheduler);
269                 }
270                 
271                 [MonoTODO]
272                 public Task<TResult> ContinueWhenAny<TAntecedentResult, TResult> (Task<TAntecedentResult>[] tasks,
273                                                                                   Func<Task<TAntecedentResult>, TResult> continuationAction,
274                                                                                   TaskContinuationOptions continuationOptions)
275                 {
276                         return ContinueWhenAny (tasks, continuationAction, token, continuationOptions, scheduler);
277                 }
278
279                 [MonoTODO]
280                 public Task<TResult> ContinueWhenAny<TAntecedentResult, TResult> (Task<TAntecedentResult>[] tasks,
281                                                                                   Func<Task<TAntecedentResult>, TResult> continuationAction,
282                                                                                   CancellationToken token,
283                                                                                   TaskContinuationOptions continuationOptions,
284                                                                                   TaskScheduler scheduler)
285                 {
286                         return ContinueWhenAny<TResult> ((Task[])tasks, (t) => continuationAction((Task<TAntecedentResult>)t), token, continuationOptions, scheduler);
287                 }
288                 
289                 public Task ContinueWhenAll (Task[] tasks, Action<Task[]> continuationFunction)
290                 {
291                         return ContinueWhenAll (tasks, continuationFunction, token, contOptions, scheduler);
292                 }
293                 
294                 public Task ContinueWhenAll (Task[] tasks, Action<Task[]> continuationFunction, CancellationToken token)
295                 {
296                         return ContinueWhenAll (tasks, continuationFunction, token, contOptions, scheduler);
297                 }
298                 
299                 public Task ContinueWhenAll (Task[] tasks, Action<Task[]> continuationFunction,
300                                              TaskContinuationOptions continuationOptions)
301                 {
302                         return ContinueWhenAll (tasks, continuationFunction, token, continuationOptions, scheduler);
303                 }
304                 
305                 public Task ContinueWhenAll (Task[] tasks, Action<Task[]> continuationFunction, CancellationToken token,
306                                              TaskContinuationOptions continuationOptions, TaskScheduler scheduler)
307                 {
308                         CountdownEvent evt = new CountdownEvent (tasks.Length);
309                         Task cont = new Task ((o) => continuationFunction ((Task[])o), tasks, token, options);
310                         
311                         foreach (Task t in tasks)
312                                 t.ContinueWithCore (cont, continuationOptions, scheduler, evt.Signal);
313                         
314                         return cont;
315                 }
316                 
317                 public Task ContinueWhenAll<TAntecedentResult> (Task<TAntecedentResult>[] tasks,
318                                                                 Action<Task<TAntecedentResult>[]> continuationFunction)
319                 {
320                         return ContinueWhenAll (tasks, continuationFunction, token, contOptions, scheduler);
321                 }
322                 
323                 public Task ContinueWhenAll<TAntecedentResult> (Task<TAntecedentResult>[] tasks,
324                                                                 Action<Task<TAntecedentResult>[]> continuationFunction, CancellationToken token)
325                 {
326                         return ContinueWhenAll (tasks, continuationFunction, token, contOptions, scheduler);
327                 }
328                 
329                 public Task ContinueWhenAll<TAntecedentResult> (Task<TAntecedentResult>[] tasks, Action<Task<TAntecedentResult>[]> continuationFunction,
330                                                                 TaskContinuationOptions continuationOptions)
331                 {
332                         return ContinueWhenAll (tasks, continuationFunction, token, continuationOptions, scheduler);
333                 }
334                 
335                 public Task ContinueWhenAll<TAntecedentResult> (Task<TAntecedentResult>[] tasks, 
336                                                                 Action<Task<TAntecedentResult>[]> continuationFunction,
337                                                                 CancellationToken token, TaskContinuationOptions continuationOptions,
338                                                                 TaskScheduler scheduler)
339                 {
340                         return ContinueWhenAll ((Task[]) tasks, (o) => continuationFunction (tasks), token,
341                                                 continuationOptions, scheduler);
342                 }
343                 
344                 public Task<TResult> ContinueWhenAll<TResult> (Task[] tasks, Func<Task[], TResult> continuationFunction)
345                 {
346                         return ContinueWhenAll<TResult> (tasks, continuationFunction, token, contOptions, scheduler);
347                 }
348                 
349                 public Task<TResult> ContinueWhenAll<TResult> (Task[] tasks, Func<Task[], TResult> continuationFunction,
350                                                                TaskContinuationOptions continuationOptions)
351                 {
352                         return ContinueWhenAll<TResult> (tasks, continuationFunction, token, continuationOptions, scheduler);
353                 }
354                 
355                 public Task<TResult> ContinueWhenAll<TResult> (Task[] tasks, Func<Task[], TResult> continuationFunction,
356                                                                CancellationToken token)
357                 {
358                         return ContinueWhenAll<TResult> (tasks, continuationFunction, token, contOptions, scheduler);
359                 }
360                 
361                 public Task<TResult> ContinueWhenAll<TResult> (Task[] tasks, Func<Task[], TResult> continuationFunction,
362                                                                CancellationToken token,
363                                                                TaskContinuationOptions continuationOptions, TaskScheduler scheduler)
364                 {
365                         CountdownEvent evt = new CountdownEvent (tasks.Length);
366                         Task<TResult> cont = new Task<TResult> ((o) => continuationFunction ((Task[])o), tasks, token, options);
367                         
368                         foreach (Task t in tasks)
369                                 t.ContinueWithCore (cont, continuationOptions, scheduler, evt.Signal);
370                         
371                         return cont;
372                 }
373                 
374                 public Task<TResult> ContinueWhenAll<TAntecedentResult, TResult> (Task<TAntecedentResult>[] tasks,
375                                                                                   Func<Task<TAntecedentResult>[], TResult> continuationFunction)
376                 {
377                         return ContinueWhenAll<TAntecedentResult, TResult> (tasks, continuationFunction, token, contOptions, scheduler);
378                 }
379                 
380                 public Task<TResult> ContinueWhenAll<TAntecedentResult, TResult> (Task<TAntecedentResult>[] tasks, 
381                                                                                   Func<Task<TAntecedentResult>[], TResult> continuationFunction,
382                                                                                   TaskContinuationOptions continuationOptions)
383                 {
384                         return ContinueWhenAll<TAntecedentResult, TResult> (tasks, continuationFunction, token, continuationOptions, scheduler);
385                 }
386                 
387                 public Task<TResult> ContinueWhenAll<TAntecedentResult, TResult> (Task<TAntecedentResult>[] tasks,
388                                                                                   Func<Task<TAntecedentResult>[], TResult> continuationFunction,
389                                                                                   CancellationToken token)
390                 {
391                         return ContinueWhenAll<TAntecedentResult, TResult> (tasks, continuationFunction, token, contOptions, scheduler);
392                 }
393                 
394                 public Task<TResult> ContinueWhenAll<TAntecedentResult, TResult> (Task<TAntecedentResult>[] tasks, 
395                                                                                   Func<Task<TAntecedentResult>[], TResult> continuationFunction,
396                                                                                   CancellationToken token,
397                                                                                   TaskContinuationOptions continuationOptions,
398                                                                                   TaskScheduler scheduler)
399                 {
400                         return ContinueWhenAll<TResult> ((Task[]) tasks,
401                                                          (o) => continuationFunction (tasks),
402                                                          token,
403                                                          continuationOptions, scheduler);
404                 }
405
406                 #endregion
407                 
408                 #region FromAsync
409                 // For these methods to work we first have to convert the ThreadPool to use Tasks as it
410                 // is doing in 4.0, then all that is remaining is to identify the Task on which is 
411                 // run the async operation (probably with some additional state in a IAsyncResult subclass)
412                 // and call its ContinueWith method accordingly
413                 
414                 const string errorMsg = "Mono's thread pool doesn't support this operation yet";
415                 
416                 [MonoLimitation(errorMsg)]
417                 public Task FromAsync (IAsyncResult asyncResult, Action<IAsyncResult> endMethod)
418                 {
419                         return FromAsync (asyncResult, endMethod, options);
420                 }
421                 
422                 [MonoLimitation(errorMsg)]
423                 public Task FromAsync (IAsyncResult asyncResult, Action<IAsyncResult> endMethod,
424                                        TaskCreationOptions creationOptions)
425                 {
426                         return FromAsync (asyncResult, endMethod, creationOptions);
427                 }
428                 
429                 [MonoLimitation(errorMsg)]
430                 public Task FromAsync (IAsyncResult asyncResult, Action<IAsyncResult> endMethod,
431                                        TaskCreationOptions creationOptions, TaskScheduler scheduler)
432                 {
433                         throw new NotSupportedException (errorMsg);
434                 }
435                 
436                 [MonoLimitation(errorMsg)]
437                 public Task<TResult> FromAsync<TResult> (IAsyncResult asyncResult, Func<IAsyncResult, TResult> endMethod)
438                 {
439                         return FromAsync<TResult> (asyncResult, endMethod, options);
440                 }
441                 
442                 [MonoLimitation(errorMsg)]
443                 public Task<TResult> FromAsync<TResult> (IAsyncResult asyncResult, Func<IAsyncResult, TResult> endMethod,
444                                                          TaskCreationOptions creationOptions)
445                 {
446                         return FromAsync<TResult> (asyncResult, endMethod, creationOptions);
447                 }
448                 
449                 [MonoLimitation(errorMsg)]
450                 public Task<TResult> FromAsync<TResult> (IAsyncResult asyncResult, Func<IAsyncResult, TResult> endMethod,
451                                                          TaskCreationOptions creationOptions, TaskScheduler scheduler)
452                 {
453                         throw new NotSupportedException (errorMsg);
454                 }
455                 
456                 
457                 [MonoLimitation(errorMsg)]
458                 public Task FromAsync (Func<AsyncCallback, Object, IAsyncResult> beginMethod, Action<IAsyncResult> endMethod,
459                                        object state)
460                 {
461                         return FromAsync<object> ((a, c, o) => beginMethod (c, o), endMethod, state, options);
462                 }
463                 
464                 [MonoLimitation(errorMsg)]
465                 public Task FromAsync (Func<AsyncCallback, Object, IAsyncResult> beginMethod, Action<IAsyncResult> endMethod,
466                                        object state, TaskCreationOptions creationOptions)
467                 {
468                         return FromAsync<object> ((a, c, o) => beginMethod (c, o), endMethod, state, creationOptions);
469                 }
470                 
471                 [MonoLimitation(errorMsg)]
472                 public Task FromAsync<TArg1> (Func<TArg1, AsyncCallback, Object, IAsyncResult> beginMethod, Action<IAsyncResult> endMethod,
473                                               TArg1 arg1, object state)
474                 {
475                         throw new NotSupportedException (errorMsg);
476                 }
477                 
478                 [MonoLimitation(errorMsg)]
479                 public Task FromAsync<TArg1> (Func<TArg1, AsyncCallback, Object, IAsyncResult> beginMethod, Action<IAsyncResult> endMethod,
480                                               TArg1 arg1, object state, TaskCreationOptions creationOptions)
481                 {
482                         throw new NotSupportedException (errorMsg);
483                 }
484                 
485                 [MonoLimitation(errorMsg)]
486                 public Task FromAsync<TArg1, TArg2> (Func<TArg1, TArg2, AsyncCallback, Object, IAsyncResult> beginMethod, Action<IAsyncResult> endMethod,
487                                                      TArg1 arg1, TArg2 arg2, object state)
488                 {
489                         throw new NotSupportedException (errorMsg);
490                 }
491                 
492                 [MonoLimitation(errorMsg)]
493                 public Task FromAsync<TArg1, TArg2> (Func<TArg1, TArg2, AsyncCallback, Object, IAsyncResult> beginMethod, Action<IAsyncResult> endMethod,
494                                                      TArg1 arg1, TArg2 arg2, object state, TaskCreationOptions creationOptions)
495                 {
496                         throw new NotSupportedException (errorMsg);
497                 }
498                 
499                 [MonoLimitation(errorMsg)]
500                 public Task FromAsync<TArg1, TArg2, TArg3> (Func<TArg1, TArg2, TArg3, AsyncCallback, Object, IAsyncResult> beginMethod, Action<IAsyncResult> endMethod,
501                                                             TArg1 arg1, TArg2 arg2, TArg3 arg3, object state)
502                 {
503                         throw new NotSupportedException (errorMsg);
504                 }
505                 
506                 [MonoLimitation(errorMsg)]
507                 public Task FromAsync<TArg1, TArg2, TArg3> (Func<TArg1, TArg2, TArg3, AsyncCallback, Object, IAsyncResult> beginMethod, Action<IAsyncResult> endMethod,
508                                                             TArg1 arg1, TArg2 arg2, TArg3 arg3, object state, TaskCreationOptions creationOptions)
509                 {
510                         throw new NotSupportedException (errorMsg);
511                 }               
512                 
513                 [MonoLimitation(errorMsg)]
514                 public Task<TResult> FromAsync<TResult> (Func<AsyncCallback, Object, IAsyncResult> beginMethod,
515                                                          Func<IAsyncResult, TResult> endMethod,
516                                                          object state)
517                 {
518                         throw new NotSupportedException (errorMsg);
519                 }
520                 
521                 [MonoLimitation(errorMsg)]
522                 public Task<TResult> FromAsync<TResult> (Func<AsyncCallback, Object, IAsyncResult> beginMethod,
523                                                          Func<IAsyncResult, TResult> endMethod,
524                                        object state, TaskCreationOptions creationOptions)
525                 {
526                         throw new NotSupportedException (errorMsg);
527                 }
528                 
529                 [MonoLimitation(errorMsg)]
530                 public Task<TResult> FromAsync<TArg1, TResult> (Func<TArg1, AsyncCallback, Object, IAsyncResult> beginMethod,
531                                                                 Func<IAsyncResult, TResult> endMethod,
532                                                                 TArg1 arg1, object state)
533                 {
534                         throw new NotSupportedException (errorMsg);
535                 }
536                 
537                 [MonoLimitation(errorMsg)]
538                 public Task<TResult> FromAsync<TArg1, TResult> (Func<TArg1, AsyncCallback, Object, IAsyncResult> beginMethod,
539                                                              Func<IAsyncResult, TResult> endMethod,
540                                                              TArg1 arg1, object state, TaskCreationOptions creationOptions)
541                 {
542                         throw new NotSupportedException (errorMsg);
543                 }
544                 
545                 [MonoLimitation(errorMsg)]
546                 public Task<TResult> FromAsync<TArg1, TArg2, TResult> (Func<TArg1, TArg2, AsyncCallback, Object, IAsyncResult> beginMethod,
547                                                                        Func<IAsyncResult, TResult> endMethod,
548                                                                        TArg1 arg1, TArg2 arg2, object state)
549                 {
550                         throw new NotSupportedException (errorMsg);
551                 }
552                 
553                 [MonoLimitation(errorMsg)]
554                 public Task<TResult> FromAsync<TArg1, TArg2, TResult> (Func<TArg1, TArg2, AsyncCallback, Object, IAsyncResult> beginMethod,
555                                                                        Func<IAsyncResult, TResult> endMethod,
556                                                                        TArg1 arg1, TArg2 arg2, object state, TaskCreationOptions creationOptions)
557                 {
558                         throw new NotSupportedException (errorMsg);
559                 }
560                 
561                 [MonoLimitation(errorMsg)]
562                 public Task<TResult> FromAsync<TArg1, TArg2, TArg3, TResult> (Func<TArg1, TArg2, TArg3, AsyncCallback, Object, IAsyncResult> beginMethod,
563                                                                               Func<IAsyncResult, TResult> endMethod,
564                                                                               TArg1 arg1, TArg2 arg2, TArg3 arg3, object state)
565                 {
566                         throw new NotSupportedException (errorMsg);
567                 }
568                 
569                 [MonoLimitation(errorMsg)]
570                 public Task<TResult> FromAsync<TArg1, TArg2, TArg3, TResult> (Func<TArg1, TArg2, TArg3, AsyncCallback, Object, IAsyncResult> beginMethod,
571                                                                               Func<IAsyncResult, TResult> endMethod,
572                                                                               TArg1 arg1, TArg2 arg2, TArg3 arg3, object state,
573                                                                               TaskCreationOptions creationOptions)
574                 {
575                         throw new NotSupportedException (errorMsg);
576                 }
577                 #endregion
578                 
579                 public TaskScheduler Scheduler {
580                         get {
581                                 return scheduler;
582                         }
583                 }
584                 
585                 public TaskContinuationOptions ContinuationOptions {
586                         get {
587                                 return contOptions;
588                         }
589                 }
590                 
591                 public TaskCreationOptions CreationOptions {
592                         get {
593                                 return options;
594                         }
595                 }
596                 
597                 public CancellationToken CancellationToken {
598                         get {
599                                 return token;
600                         }
601                 }
602         }
603         
604         public class TaskFactory<TResult>
605         {
606                 TaskScheduler scheduler;
607                 TaskCreationOptions options;
608                 TaskContinuationOptions contOptions;
609                 CancellationToken token;
610                 
611                 TaskFactory parent;
612                 
613                 #region ctors
614                 public TaskFactory ()
615                         : this (CancellationToken.None, TaskCreationOptions.None, TaskContinuationOptions.None, TaskScheduler.Current)
616                 {       
617                 }
618                 
619                 public TaskFactory (TaskScheduler scheduler)
620                         : this (CancellationToken.None, TaskCreationOptions.None, TaskContinuationOptions.None, scheduler)
621                 {       
622                 }
623                 
624                 public TaskFactory (CancellationToken token)
625                         : this (token, TaskCreationOptions.None, TaskContinuationOptions.None, TaskScheduler.Current)
626                 {       
627                 }
628                 
629                 public TaskFactory (TaskCreationOptions options, TaskContinuationOptions contOptions)
630                         : this (CancellationToken.None, options, contOptions, TaskScheduler.Current)
631                 {       
632                 }
633                 
634                 public TaskFactory (CancellationToken token, TaskCreationOptions options, TaskContinuationOptions contOptions,
635                                     TaskScheduler scheduler)
636                 {
637                         this.token = token;
638                         this.scheduler = scheduler;
639                         this.options = options;
640                         this.contOptions = contOptions;
641                         
642                         this.parent = new TaskFactory (token, options, contOptions, scheduler);
643                 }
644                 
645                 #endregion
646                 
647                 #region StartNew for Task<TResult>      
648                 public Task<TResult> StartNew (Func<TResult> function)
649                 {
650                         return StartNew (function, token, options, scheduler);
651                 }
652                 
653                 public Task<TResult> StartNew (Func<TResult> function, TaskCreationOptions options)
654                 {
655                         return StartNew (function, token, options, scheduler);
656                 }
657                 
658                 public Task<TResult> StartNew (Func<TResult> function, CancellationToken token)
659                 {
660                         return StartNew (function, token, options, scheduler);
661                 }
662                 
663                 public Task<TResult> StartNew (Func<TResult> function, 
664                                                CancellationToken token,
665                                                TaskCreationOptions options,
666                                                TaskScheduler scheduler)
667                 {
668                         return StartNew ((o) => function (), null, token, options, scheduler);
669                 }
670                 
671                 public Task<TResult> StartNew (Func<object, TResult> function, object state)
672                 {
673                         return StartNew (function, state, token, options, scheduler);
674                 }
675                 
676                 public Task<TResult> StartNew (Func<object, TResult> function, object state, TaskCreationOptions options)
677                 {
678                         return StartNew (function, state, token, options, scheduler);
679                 }
680                 
681                 public Task<TResult> StartNew (Func<object, TResult> function, object state, CancellationToken token)
682                 {
683                         return StartNew (function, state, token, options, scheduler);
684                 }
685                 
686                 public Task<TResult> StartNew (Func<object, TResult> function, object state, 
687                                                CancellationToken token,
688                                                TaskCreationOptions options,
689                                                TaskScheduler scheduler)
690                 {
691                         return parent.StartNew<TResult> (function, state, token, options, scheduler);
692                 }
693                 #endregion
694                 
695                 #region Continue
696                 
697                 [MonoTODO]
698                 public Task<TResult> ContinueWhenAny (Task[] tasks,
699                                                       Func<Task, TResult> continuationAction)
700                 {
701                         return ContinueWhenAny (tasks, continuationAction, token, contOptions, scheduler);
702                 }
703                 
704                 [MonoTODO]
705                 public Task<TResult> ContinueWhenAny (Task[] tasks,
706                                                       Func<Task, TResult> continuationAction,
707                                                       CancellationToken token)
708                 {
709                         return ContinueWhenAny (tasks, continuationAction, token, contOptions, scheduler);
710                 }
711                 
712                 [MonoTODO]
713                 public Task<TResult> ContinueWhenAny (Task[] tasks,
714                                                       Func<Task, TResult> continuationAction,
715                                                       TaskContinuationOptions continuationOptions)
716                 {
717                         return ContinueWhenAny (tasks, continuationAction, token, continuationOptions, scheduler);
718                 }
719
720                 [MonoTODO]
721                 public Task<TResult> ContinueWhenAny (Task[] tasks,
722                                                       Func<Task, TResult> continuationAction,
723                                                       CancellationToken token,
724                                                       TaskContinuationOptions continuationOptions,
725                                                       TaskScheduler scheduler)
726                 {
727                         throw new NotImplementedException ();
728                 }
729                 
730                 [MonoTODO]
731                 public Task<TResult> ContinueWhenAny<TAntecedentResult> (Task<TAntecedentResult>[] tasks,
732                                                                          Func<Task<TAntecedentResult>, TResult> continuationAction)
733                 {
734                         return ContinueWhenAny (tasks, continuationAction, token, contOptions, scheduler);
735                 }
736                 
737                 [MonoTODO]
738                 public Task<TResult> ContinueWhenAny<TAntecedentResult> (Task<TAntecedentResult>[] tasks,
739                                                                          Func<Task<TAntecedentResult>, TResult> continuationAction,
740                                                                          CancellationToken token)
741                 {
742                         return ContinueWhenAny (tasks, continuationAction, token, contOptions, scheduler);
743                 }
744                 
745                 [MonoTODO]
746                 public Task<TResult> ContinueWhenAny<TAntecedentResult> (Task<TAntecedentResult>[] tasks,
747                                                                          Func<Task<TAntecedentResult>, TResult> continuationAction,
748                                                                          TaskContinuationOptions continuationOptions)
749                 {
750                         return ContinueWhenAny (tasks, continuationAction, token, continuationOptions, scheduler);
751                 }
752
753                 [MonoTODO]
754                 public Task<TResult> ContinueWhenAny<TAntecedentResult> (Task<TAntecedentResult>[] tasks,
755                                                                          Func<Task<TAntecedentResult>, TResult> continuationAction,
756                                                                          CancellationToken token,
757                                                                          TaskContinuationOptions continuationOptions,
758                                                                          TaskScheduler scheduler)
759                 {
760                         throw new NotImplementedException ();
761                 }
762                 
763                 public Task<TResult> ContinueWhenAll (Task[] tasks,
764                                                       Func<Task[], TResult> continuationFunction)
765                 {
766                         return ContinueWhenAll (tasks, continuationFunction, token, contOptions, scheduler);
767                 }
768                 
769                 public Task<TResult> ContinueWhenAll (Task[] tasks,
770                                                       Func<Task[], TResult> continuationFunction,
771                                                       TaskContinuationOptions continuationOptions)
772                 {
773                         return ContinueWhenAll (tasks, continuationFunction, token, continuationOptions, scheduler);
774                 }
775                 
776                 public Task<TResult> ContinueWhenAll (Task[] tasks,
777                                                       Func<Task[], TResult> continuationFunction,
778                                                       CancellationToken token)
779                 {
780                         return ContinueWhenAll (tasks, continuationFunction, token, contOptions, scheduler);
781                 }
782                 
783                 public Task<TResult> ContinueWhenAll (Task[] tasks,
784                                                       Func<Task[], TResult> continuationFunction,
785                                                       CancellationToken token,
786                                                       TaskContinuationOptions continuationOptions, TaskScheduler scheduler)
787                 {
788                         CountdownEvent evt = new CountdownEvent (tasks.Length);
789                         Task<TResult> cont = new Task<TResult> ((o) => continuationFunction (tasks), tasks, token, options);
790                         
791                         foreach (Task t in tasks)
792                                 t.ContinueWithCore (cont, continuationOptions, scheduler, evt.Signal);
793                         
794                         return cont;
795                 }
796                 
797                 public Task<TResult> ContinueWhenAll<TAntecedentResult> (Task<TAntecedentResult>[] tasks,
798                                                                          Func<Task<TAntecedentResult>[], TResult> continuationFunction)
799                 {
800                         return ContinueWhenAll (tasks, continuationFunction, token, contOptions, scheduler);
801                 }
802                 
803                 public Task<TResult> ContinueWhenAll<TAntecedentResult> (Task<TAntecedentResult>[] tasks,
804                                                                          Func<Task<TAntecedentResult>[], TResult> continuationFunction,
805                                                                          TaskContinuationOptions continuationOptions)
806                 {
807                         return ContinueWhenAll (tasks, continuationFunction, token, continuationOptions, scheduler);
808                 }
809                 
810                 public Task<TResult> ContinueWhenAll<TAntecedentResult> (Task<TAntecedentResult>[] tasks,
811                                                                          Func<Task<TAntecedentResult>[], TResult> continuationFunction,
812                                                                          CancellationToken token)
813                 {
814                         return ContinueWhenAll (tasks, continuationFunction, token, contOptions, scheduler);
815                 }
816                 
817                 public Task<TResult> ContinueWhenAll<TAntecedentResult> (Task<TAntecedentResult>[] tasks,
818                                                                          Func<Task<TAntecedentResult>[], TResult> continuationFunction,
819                                                                          CancellationToken token,
820                                                                          TaskContinuationOptions continuationOptions, TaskScheduler scheduler)
821                 {
822                         CountdownEvent evt = new CountdownEvent (tasks.Length);
823                         Task<TResult> cont = new Task<TResult> ((o) => continuationFunction (tasks), tasks, token, options);
824                         
825                         foreach (Task t in tasks)
826                                 t.ContinueWithCore (cont, continuationOptions, scheduler, evt.Signal);
827                         
828                         return cont;
829                 }
830
831                 #endregion
832                 
833                 #region FromAsync
834                 const string errorMsg = "Mono's thread pool doesn't support this operation yet";
835                 
836                 [MonoLimitation(errorMsg)]
837                 public Task<TResult> FromAsync (IAsyncResult asyncResult, Func<IAsyncResult, TResult> endMethod)
838                 {
839                         return FromAsync (asyncResult, endMethod, options);
840                 }
841                 
842                 [MonoLimitation(errorMsg)]
843                 public Task<TResult> FromAsync (IAsyncResult asyncResult, Func<IAsyncResult, TResult> endMethod,
844                                                 TaskCreationOptions creationOptions)
845                 {
846                         return FromAsync (asyncResult, endMethod, creationOptions);
847                 }
848                 
849                 [MonoLimitation(errorMsg)]
850                 public Task<TResult> FromAsync (IAsyncResult asyncResult, Func<IAsyncResult, TResult> endMethod,
851                                                 TaskCreationOptions creationOptions, TaskScheduler scheduler)
852                 {
853                         throw new NotSupportedException (errorMsg);
854                 }
855                 
856                 [MonoLimitation(errorMsg)]
857                 public Task<TResult> FromAsync (Func<AsyncCallback, Object, IAsyncResult> beginMethod,
858                                                 Func<IAsyncResult, TResult> endMethod,
859                                                 object state)
860                 {
861                         throw new NotSupportedException (errorMsg);
862                 }
863                 
864                 [MonoLimitation(errorMsg)]
865                 public Task<TResult> FromAsync (Func<AsyncCallback, Object, IAsyncResult> beginMethod,
866                                                 Func<IAsyncResult, TResult> endMethod,
867                                                 object state, TaskCreationOptions creationOptions)
868                 {
869                         throw new NotSupportedException (errorMsg);
870                 }
871                 
872                 [MonoLimitation(errorMsg)]
873                 public Task<TResult> FromAsync<TArg1> (Func<TArg1, AsyncCallback, Object, IAsyncResult> beginMethod,
874                                                        Func<IAsyncResult, TResult> endMethod,
875                                                        TArg1 arg1, object state)
876                 {
877                         throw new NotSupportedException (errorMsg);
878                 }
879                 
880                 [MonoLimitation(errorMsg)]
881                 public Task<TResult> FromAsync<TArg1> (Func<TArg1, AsyncCallback, Object, IAsyncResult> beginMethod,
882                                                        Func<IAsyncResult, TResult> endMethod,
883                                                        TArg1 arg1, object state, TaskCreationOptions creationOptions)
884                 {
885                         throw new NotSupportedException (errorMsg);
886                 }
887                 
888                 [MonoLimitation(errorMsg)]
889                 public Task<TResult> FromAsync<TArg1, TArg2> (Func<TArg1, TArg2, AsyncCallback, Object, IAsyncResult> beginMethod,
890                                                               Func<IAsyncResult, TResult> endMethod,
891                                                               TArg1 arg1, TArg2 arg2, object state)
892                 {
893                         throw new NotSupportedException (errorMsg);
894                 }
895                 
896                 [MonoLimitation(errorMsg)]
897                 public Task<TResult> FromAsync<TArg1, TArg2> (Func<TArg1, TArg2, AsyncCallback, Object, IAsyncResult> beginMethod,
898                                                               Func<IAsyncResult, TResult> endMethod,
899                                                               TArg1 arg1, TArg2 arg2, object state, TaskCreationOptions creationOptions)
900                 {
901                         throw new NotSupportedException (errorMsg);
902                 }
903                 
904                 [MonoLimitation(errorMsg)]
905                 public Task<TResult> FromAsync<TArg1, TArg2, TArg3> (Func<TArg1, TArg2, TArg3, AsyncCallback, Object, IAsyncResult> beginMethod,
906                                                                      Func<IAsyncResult, TResult> endMethod,
907                                                                      TArg1 arg1, TArg2 arg2, TArg3 arg3, object state)
908                 {
909                         throw new NotSupportedException (errorMsg);
910                 }
911                 
912                 [MonoLimitation(errorMsg)]
913                 public Task<TResult> FromAsync<TArg1, TArg2, TArg3> (Func<TArg1, TArg2, TArg3, AsyncCallback, Object, IAsyncResult> beginMethod,
914                                                                      Func<IAsyncResult, TResult> endMethod,
915                                                                      TArg1 arg1, TArg2 arg2, TArg3 arg3, object state,
916                                                                      TaskCreationOptions creationOptions)
917                 {
918                         throw new NotSupportedException (errorMsg);
919                 }
920                 #endregion
921                 
922                 public TaskScheduler Scheduler {
923                         get {
924                                 return scheduler;
925                         }
926                 }
927                 
928                 public TaskContinuationOptions ContinuationOptions {
929                         get {
930                                 return contOptions;
931                         }
932                 }
933                 
934                 public TaskCreationOptions CreationOptions {
935                         get {
936                                 return options;
937                         }
938                 }
939                 
940                 public CancellationToken CancellationToken {
941                         get {
942                                 return token;
943                         }
944                 }
945         }
946 }
947 #endif