Пример #1
0
        public static Task Run(Action action, CancellationToken cancellationToken)
        {
            if (CoyoteRuntime.IsExecutionControlled)
            {
                var runtime = CoyoteRuntime.Current;
                var options = OperationContext.CreateOperationExecutionOptions();
                return(new Task(runtime, runtime.ScheduleAction(action, null, options, false, cancellationToken)));
            }

            return(new Task(null, SystemTasks.Task.Run(action, cancellationToken)));
        }
Пример #2
0
        public static bool QueueUserWorkItem <TState>(Action <TState> callBack, TState state, bool preferLocal)
        {
            if (CoyoteRuntime.IsExecutionControlled)
            {
                // TODO: check if we need to capture the execution context.

                // We set `failException` to true to mimic the production behavior of treating an unhandled
                // exception as an error that crashes the application.
                var options = OperationContext.CreateOperationExecutionOptions(failOnException: true);
                CoyoteRuntime.Current.ScheduleAction(() => callBack(state), null, options);
                return(true);
            }

            return(SystemThreading.ThreadPool.QueueUserWorkItem(callBack, state, preferLocal));
        }
Пример #3
0
        public static ParallelLoopResult ForEach <TSource>(IEnumerable <TSource> source, ParallelOptions parallelOptions, Action <TSource> body)
        {
            if (CoyoteRuntime.IsExecutionControlled)
            {
                ValidateParallelOptions(parallelOptions);

                var runtime    = CoyoteRuntime.Current;
                var sourceList = source.ToList();

                int numIterations = sourceList.Count;
                int numTasks      = Math.Min(numIterations, parallelOptions.MaxDegreeOfParallelism);

                var groups = Enumerable.Range(0, numIterations)
                             .Select((item, index) => new { index, item })
                             .GroupBy(x => x.index % numTasks)
                             .Select(x => x.Select(y => y.item));

                int    index = 0;
                Task[] tasks = new Task[numTasks];

                var options = OperationContext.CreateOperationExecutionOptions();
                foreach (var group in groups)
                {
                    tasks[index] = runtime.ScheduleAction(() =>
                    {
                        foreach (var iteration in group)
                        {
                            body(sourceList[iteration]);
                        }
                    }, null, options, false, parallelOptions.CancellationToken);
                    index++;
                }

                runtime.WaitAllTasksComplete(tasks);
                return(CompletedResult);
            }

            return(SystemTasks.Parallel.ForEach(source, parallelOptions, body));
        }
Пример #4
0
 public static Task Run(Action action, CancellationToken cancellationToken) => CoyoteRuntime.IsExecutionControlled ?
 CoyoteRuntime.Current.ScheduleAction(action, null, OperationContext.CreateOperationExecutionOptions(),
                                      false, cancellationToken) :
 Task.Run(action, cancellationToken);