YieldableTask.cs
Go to the documentation of this file.
1 /*
2 
3 Author: Aaron Oneal, http://aarononeal.info
4 
5 Copyright (c) 2012 Spicy Pixel, http://spicypixel.com
6 
7 Permission is hereby granted, free of charge, to any person obtaining
8 a copy of this software and associated documentation files (the
9 "Software"), to deal in the Software without restriction, including
10 without limitation the rights to use, copy, modify, merge, publish,
11 distribute, sublicense, and/or sell copies of the Software, and to
12 permit persons to whom the Software is furnished to do so, subject to
13 the following conditions:
14 
15 The above copyright notice and this permission notice shall be
16 included in all copies or substantial portions of the Software.
17 
18 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
21 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
22 LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
23 OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
24 WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 
26 */
27 using System;
28 using System.Threading;
29 using System.Threading.Tasks;
30 using System.Collections;
31 
32 namespace SpicyPixel.Threading.Tasks
33 {
41  public class YieldableTask : Task
42  {
43  Fiber fiber;
44  Exception fiberException;
45 
46  internal Fiber Fiber {
47  get { return fiber; }
48  }
49 
50  internal Exception FiberException {
51  get { return fiberException; }
52  set { fiberException = value; }
53  }
54 
64  public YieldableTask (IEnumerator coroutine) :
65  base(() => {})
66  {
67  fiber = new Fiber(coroutine);
68  SetAction(() => InternalAction ());
69  }
70 
83  public YieldableTask (IEnumerator coroutine, TaskCreationOptions creationOptions) :
84  base(() => {}, creationOptions)
85  {
86  fiber = new Fiber(coroutine);
87  SetAction(() => InternalAction ());
88  }
89 
102  public YieldableTask (IEnumerator coroutine, CancellationToken cancellationToken) :
103  base(() => {}, cancellationToken)
104  {
105  fiber = new Fiber(coroutine);
106  SetAction(() => InternalAction ());
107  }
108 
124  public YieldableTask (IEnumerator coroutine, CancellationToken cancellationToken, TaskCreationOptions creationOptions) :
125  base(() => {}, cancellationToken, creationOptions)
126  {
127  fiber = new Fiber(coroutine);
128  SetAction(() => InternalAction ());
129  }
130 
140  public YieldableTask (FiberInstruction instruction) :
141  base(() => {})
142  {
143  fiber = new Fiber(() => instruction);
144  SetAction(() => InternalAction ());
145  }
146 
159  public YieldableTask (FiberInstruction instruction, TaskCreationOptions creationOptions) :
160  base(() => {}, creationOptions)
161  {
162  fiber = new Fiber(() => instruction);
163  SetAction(() => InternalAction ());
164  }
165 
178  public YieldableTask (FiberInstruction instruction, CancellationToken cancellationToken) :
179  base(() => {}, cancellationToken)
180  {
181  fiber = new Fiber(() => instruction);
182  SetAction(() => InternalAction ());
183  }
184 
200  public YieldableTask (FiberInstruction instruction, CancellationToken cancellationToken, TaskCreationOptions creationOptions) :
201  base(() => {}, cancellationToken, creationOptions)
202  {
203  fiber = new Fiber(() => instruction);
204  SetAction(() => InternalAction ());
205  }
206 
216  public YieldableTask (Func<FiberInstruction> coroutine) :
217  base(() => {})
218  {
219  fiber = new Fiber(coroutine);
220  SetAction(() => InternalAction ());
221  }
222 
235  public YieldableTask (Func<FiberInstruction> coroutine, TaskCreationOptions creationOptions) :
236  base(() => {}, creationOptions)
237  {
238  fiber = new Fiber(coroutine);
239  SetAction(() => InternalAction ());
240  }
241 
254  public YieldableTask (Func<FiberInstruction> coroutine, CancellationToken cancellationToken) :
255  base(() => {}, cancellationToken)
256  {
257  fiber = new Fiber(coroutine);
258  SetAction(() => InternalAction ());
259  }
260 
276  public YieldableTask (Func<FiberInstruction> coroutine, CancellationToken cancellationToken, TaskCreationOptions creationOptions) :
277  base(() => {}, cancellationToken, creationOptions)
278  {
279  fiber = new Fiber(coroutine);
280  SetAction(() => InternalAction ());
281  }
282 
295  public YieldableTask (Func<object, FiberInstruction> coroutine, object state) :
296  base(() => {})
297  {
298  fiber = new Fiber(coroutine, state);
299  SetAction(() => InternalAction ());
300  }
301 
317  public YieldableTask (Func<object, FiberInstruction> coroutine, object state, TaskCreationOptions creationOptions) :
318  base(() => {}, creationOptions)
319  {
320  fiber = new Fiber(coroutine, state);
321  SetAction(() => InternalAction ());
322  }
323 
339  public YieldableTask (Func<object, FiberInstruction> coroutine, object state, CancellationToken cancellationToken) :
340  base(() => {}, cancellationToken)
341  {
342  fiber = new Fiber(coroutine, state);
343  SetAction(() => InternalAction ());
344  }
345 
364  public YieldableTask (Func<object, FiberInstruction> coroutine, object state, CancellationToken cancellationToken, TaskCreationOptions creationOptions) :
365  base(() => {}, cancellationToken, creationOptions)
366  {
367  fiber = new Fiber(coroutine, state);
368  SetAction(() => InternalAction ());
369  }
370 
371  private void InternalAction()
372  {
374  throw new InvalidOperationException("A YieldableTask can only be queued to a FiberTaskScheduler.");
375 
376  if(fiberException != null)
377  throw fiberException;
378  }
379  }
380 }
YieldableTask(Func< FiberInstruction > coroutine, TaskCreationOptions creationOptions)
Initializes a new instance of the SpicyPixel.Threading.Tasks.YieldableTask class. ...
Yieldable task for execution on a fiber.
YieldableTask(Func< FiberInstruction > coroutine, CancellationToken cancellationToken)
Initializes a new instance of the SpicyPixel.Threading.Tasks.YieldableTask class. ...
A Fiber is a lightweight means of scheduling work that enables multiple units of processing to execut...
Represents a fiber instruction to be processed by a FiberScheduler.
YieldableTask(Func< FiberInstruction > coroutine)
Initializes a new instance of the SpicyPixel.Threading.Tasks.YieldableTask class. ...
YieldableTask(Func< object, FiberInstruction > coroutine, object state)
Initializes a new instance of the SpicyPixel.Threading.Tasks.YieldableTask class. ...
YieldableTask(Func< object, FiberInstruction > coroutine, object state, TaskCreationOptions creationOptions)
Initializes a new instance of the SpicyPixel.Threading.Tasks.YieldableTask class. ...
YieldableTask(Func< object, FiberInstruction > coroutine, object state, CancellationToken cancellationToken, TaskCreationOptions creationOptions)
Initializes a new instance of the SpicyPixel.Threading.Tasks.YieldableTask class. ...
YieldableTask(FiberInstruction instruction, TaskCreationOptions creationOptions)
Initializes a new instance of the SpicyPixel.Threading.Tasks.YieldableTask class. ...
YieldableTask(IEnumerator coroutine, CancellationToken cancellationToken, TaskCreationOptions creationOptions)
Initializes a new instance of the SpicyPixel.Threading.Tasks.YieldableTask class. ...
YieldableTask(Func< object, FiberInstruction > coroutine, object state, CancellationToken cancellationToken)
Initializes a new instance of the SpicyPixel.Threading.Tasks.YieldableTask class. ...
YieldableTask(IEnumerator coroutine)
Initializes a new instance of the SpicyPixel.Threading.Tasks.YieldableTask class. ...
YieldableTask(FiberInstruction instruction, CancellationToken cancellationToken)
Initializes a new instance of the SpicyPixel.Threading.Tasks.YieldableTask class. ...
YieldableTask(IEnumerator coroutine, CancellationToken cancellationToken)
Initializes a new instance of the SpicyPixel.Threading.Tasks.YieldableTask class. ...
YieldableTask(IEnumerator coroutine, TaskCreationOptions creationOptions)
Initializes a new instance of the SpicyPixel.Threading.Tasks.YieldableTask class. ...
YieldableTask(Func< FiberInstruction > coroutine, CancellationToken cancellationToken, TaskCreationOptions creationOptions)
Initializes a new instance of the SpicyPixel.Threading.Tasks.YieldableTask class. ...
YieldableTask(FiberInstruction instruction, CancellationToken cancellationToken, TaskCreationOptions creationOptions)
Initializes a new instance of the SpicyPixel.Threading.Tasks.YieldableTask class. ...
YieldableTask(FiberInstruction instruction)
Initializes a new instance of the SpicyPixel.Threading.Tasks.YieldableTask class. ...
TaskScheduler that can execute fibers (yieldable coroutines).