/// <summary>
        /// Creates a new task and starts executing it.
        /// </summary>
        /// <returns>
        /// The new executing task.
        /// </returns>
        /// <param name='taskFactory'>
        /// Task factory to start with.
        /// </param>
        /// <param name='instruction'>
        /// The instruction to start.
        /// </param>
        /// <param name='cancellationToken'>
        /// Cancellation token.
        /// </param>
        /// <param name='creationOptions'>
        /// Creation options.
        /// </param>
        /// <param name='scheduler'>
        /// Scheduler.
        /// </param>
        public static Task StartNew(this TaskFactory taskFactory, FiberInstruction instruction, CancellationToken cancellationToken, TaskCreationOptions creationOptions, TaskScheduler scheduler)
        {
            var task = new YieldableTask(instruction, cancellationToken, creationOptions);

            task.Start(scheduler);
            return(task);
        }
        /// <summary>
        /// Continues the task with a coroutine.
        /// </summary>
        /// <returns>
        /// The continued task.
        /// </returns>
        /// <param name='task'>
        /// Task to continue.
        /// </param>
        /// <param name='instruction'>
        /// The instruction to continue with.
        /// </param>
        /// <param name='cancellationToken'>
        /// Cancellation token.
        /// </param>
        /// <param name='continuationOptions'>
        /// Continuation options.
        /// </param>
        /// <param name='scheduler'>
        /// Scheduler to use when scheduling the task.
        /// </param>
        public static Task ContinueWith(this Task task, FiberInstruction instruction, CancellationToken cancellationToken, TaskContinuationOptions continuationOptions, TaskScheduler scheduler)
        {
            if (instruction == null)
            {
                throw new ArgumentNullException("instruction");
            }
            if (scheduler == null)
            {
                throw new ArgumentNullException("scheduler");
            }
            if (!(scheduler is FiberTaskScheduler))
            {
                throw new ArgumentException("The scheduler for a YieldableTask must be a FiberTaskScheduler", "scheduler");
            }

            // This creates a continuation that runs on the default scheduler (e.g. ThreadPool)
            // where it's OK to wait on a child task to complete. The child task is scheduled
            // on the given scheduler and attached to the parent.

            //var outerScheduler = TaskScheduler.Current;
            //if(outerScheduler is MonoBehaviourTaskScheduler)
            //  outerScheduler = TaskScheduler.Default;

            var outerScheduler = TaskScheduler.Default;

            // The thread pool scheduler cannot be used in web scenarios. The outer scheduler
            // must be the fiber scheduler.
            //var outerScheduler = scheduler;

            return(task.ContinueWith((Task antecedent) => {
                var yieldableTask = new YieldableTask(instruction, cancellationToken, TaskCreationOptions.AttachedToParent);
                yieldableTask.Start(scheduler);
            }, cancellationToken, continuationOptions, outerScheduler));
        }
Ejemplo n.º 3
0
 /// <summary>
 /// Initializes a new instance of the <see cref="SpicyPixel.Threading.Tasks.YieldableTask"/> class.
 /// </summary>
 /// <returns>
 /// The task.
 /// </returns>
 /// <param name='instruction'>
 /// The instruction to execute.
 /// </param>
 /// <param name='cancellationToken'>
 /// Cancellation token.
 /// </param>
 /// <param name='creationOptions'>
 /// Creation options.
 /// </param>
 public YieldableTask(FiberInstruction instruction, CancellationToken cancellationToken, TaskCreationOptions creationOptions) :
     base(() => {}, cancellationToken, creationOptions)
 {
     fiber = new Fiber(() => instruction);
     SetAction(() => InternalAction());
 }
Ejemplo n.º 4
0
 /// <summary>
 /// Initializes a new instance of the <see cref="SpicyPixel.Threading.Tasks.YieldableTask"/> class.
 /// </summary>
 /// <returns>
 /// The task.
 /// </returns>
 /// <param name='instruction'>
 /// The coroutine to execute.
 /// </param>
 public YieldableTask(FiberInstruction instruction) :
     base(() => {})
 {
     fiber = new Fiber(() => instruction);
     SetAction(() => InternalAction());
 }
 /// <summary>
 /// Initializes a new instance of the <see cref="SpicyPixel.Threading.Tasks.YieldableTask"/> class.
 /// </summary>
 /// <returns>
 /// The task.
 /// </returns>
 /// <param name='instruction'>
 /// The instruction to execute.
 /// </param>
 /// <param name='cancellationToken'>
 /// Cancellation token.
 /// </param>
 /// <param name='creationOptions'>
 /// Creation options.
 /// </param>
 public YieldableTask(FiberInstruction instruction, CancellationToken cancellationToken, TaskCreationOptions creationOptions)
     : base(() => InternalAction(), cancellationToken, creationOptions)
 {
     fiber = new Fiber(() => instruction);
 }
 /// <summary>
 /// Initializes a new instance of the <see cref="SpicyPixel.Threading.Tasks.YieldableTask"/> class.
 /// </summary>
 /// <returns>
 /// The task.
 /// </returns>
 /// <param name='instruction'>
 /// The coroutine to execute.
 /// </param>
 public YieldableTask(FiberInstruction instruction)
     : base(() => InternalAction())
 {
     fiber = new Fiber(() => instruction);
 }
Ejemplo n.º 7
0
 /// <summary>
 /// Initializes a new instance of the <see cref="SpicyPixel.Threading.Tasks.YieldableTask"/> class.
 /// </summary>
 /// <returns>
 /// The task.
 /// </returns>
 /// <param name='instruction'>
 /// The coroutine to execute.
 /// </param>
 /// <param name='cancellationToken'>
 /// Cancellation token.
 /// </param>
 public YieldableTask(FiberInstruction instruction, CancellationToken cancellationToken)
     : base(() => {}, cancellationToken)
 {
     fiber = new Fiber(() => instruction);
     SetAction(() => InternalAction ());
 }
Ejemplo n.º 8
0
 /// <summary>
 /// Initializes a new instance of the <see cref="SpicyPixel.Threading.Tasks.YieldableTask"/> class.
 /// </summary>
 /// <returns>
 /// The task.
 /// </returns>
 /// <param name='instruction'>
 /// The coroutine to execute.
 /// </param>
 /// <param name='creationOptions'>
 /// Creation options.
 /// </param>
 public YieldableTask(FiberInstruction instruction, TaskCreationOptions creationOptions)
     : base(() => {}, creationOptions)
 {
     fiber = new Fiber(() => instruction);
     SetAction(() => InternalAction ());
 }
 /// <summary>
 /// Creates a new task and starts executing it.
 /// </summary>
 /// <returns>
 /// The new executing task.
 /// </returns>
 /// <param name='taskFactory'>
 /// Task factory to start with.
 /// </param>
 /// <param name='instruction'>
 /// The instruction to start.
 /// </param>
 /// <param name='creationOptions'>
 /// Creation options.
 /// </param>
 public static Task StartNew(this TaskFactory taskFactory, FiberInstruction instruction, TaskCreationOptions creationOptions)
 {
     return(StartNew(taskFactory, instruction, taskFactory.CancellationToken, creationOptions, taskFactory.Scheduler));
 }
 /// <summary>
 /// Continues the task with a coroutine.
 /// </summary>
 /// <returns>
 /// The continued task.
 /// </returns>
 /// <param name='task'>
 /// Task to continue.
 /// </param>
 /// <param name='instruction'>
 /// The instruction to continue with.
 /// </param>
 /// <param name='scheduler'>
 /// Scheduler.
 /// </param>
 public static Task ContinueWith(this Task task, FiberInstruction instruction, TaskScheduler scheduler)
 {
     return(ContinueWith(task, instruction, CancellationToken.None, TaskContinuationOptions.None, scheduler));
 }
 /// <summary>
 /// Continues the task with a coroutine.
 /// </summary>
 /// <returns>
 /// The continued task.
 /// </returns>
 /// <param name='task'>
 /// Task to continue.
 /// </param>
 /// <param name='instruction'>
 /// The instruction to continue with.
 /// </param>
 /// <param name='cancellationToken'>
 /// Cancellation token.
 /// </param>
 public static Task ContinueWith(this Task task, FiberInstruction instruction, CancellationToken cancellationToken)
 {
     return(ContinueWith(task, instruction, cancellationToken, TaskContinuationOptions.None, TaskScheduler.Current));
 }
 /// <summary>
 /// Continues the task with a coroutine.
 /// </summary>
 /// <returns>
 /// The continued task.
 /// </returns>
 /// <param name='task'>
 /// Task to continue.
 /// </param>
 /// <param name='instruction'>
 /// The instruction to continue with.
 /// </param>
 public static Task ContinueWith(this Task task, FiberInstruction instruction)
 {
     return(ContinueWith(task, instruction, TaskContinuationOptions.None));
 }
Ejemplo n.º 13
0
 /// <summary>
 /// Initializes a new instance of the <see cref="SpicyPixel.Threading.Tasks.YieldableTask"/> class.
 /// </summary>
 /// <returns>
 /// The task.
 /// </returns>
 /// <param name='instruction'>
 /// The coroutine to execute.
 /// </param>
 /// <param name='cancellationToken'>
 /// Cancellation token.
 /// </param>
 public YieldableTask(FiberInstruction instruction, CancellationToken cancellationToken) :
     base(() => InternalAction(), cancellationToken)
 {
     fiber = new Fiber(() => instruction);
 }
Ejemplo n.º 14
0
 /// <summary>
 /// Initializes a new instance of the <see cref="SpicyPixel.Threading.Tasks.YieldableTask"/> class.
 /// </summary>
 /// <returns>
 /// The task.
 /// </returns>
 /// <param name='instruction'>
 /// The coroutine to execute.
 /// </param>
 /// <param name='creationOptions'>
 /// Creation options.
 /// </param>
 public YieldableTask(FiberInstruction instruction, TaskCreationOptions creationOptions) :
     base(() => InternalAction(), creationOptions)
 {
     fiber = new Fiber(() => instruction);
 }