Exemplo n.º 1
0
            /// <summary>
            /// 任务的创建
            /// </summary>
            /// <param name="resultType">result类型</param>
            /// <exception cref="ArgumentNullException"></exception>
            public TaskCompletionSource(Type resultType)
            {
                if (resultType == null)
                {
                    throw new ArgumentNullException();
                }
                var type = typeof(TaskSource <>).MakeGenericType(resultType);

                this.taskSource = Activator.CreateInstance(type) as ITaskSource;
            }
Exemplo n.º 2
0
        public ExecutorThread(int executorID, ITaskSource taskSource)
        {
            this.executorID = executorID;
            this.taskSource = taskSource;

            cancelTokenSource   = new CancellationTokenSource();
            wakeEvent           = new ManualResetEventSlim();
            thread              = new Thread(ExecuteLoop);
            thread.Name         = $"taskExecutor_{executorID}";
            thread.IsBackground = true;
            thread.Priority     = ThreadPriority.AboveNormal;
            thread.Start();
        }
Exemplo n.º 3
0
        public void ScheduleFrom(ITaskSource taskSource)
        {
            var count = taskSource.Tasks(out var tasks);

            for (var i = 0; i < count; i++)
            {
                var task = tasks[i];
                AdvanceWorkerIndex();
                Interlocked.Increment(ref activeTasks);
                workers[index].queue.Enqueue(task);
                workers[index].TaskEnqueuedEvent.Set();
            }
        }
            protected override ReturnMessage Invoke(MethodCallMessage mcm)
            {
                MethodData md;

                if (!this.methodCache.TryGetValue(mcm.MethodBase.Name, out md))
                {
                    throw new NotImplementedException();
                }

                object result;

                if (md.Operation.AsyncPattern == AsyncPattern.None)
                {
                    result = this.client.RequestAsync(md, mcm, mcm.InArgs.Length).GetAwaiter().GetResult();
                }
                else if (md.Operation.AsyncPattern == AsyncPattern.Task)
                {
                    Fx.Assert(md.TcsType != null, "Must be a task method");
                    Type tcsType = md.TcsType;
                    if (md.ReturnType.Type.IsGenericParameter)
                    {
                        tcsType = typeof(TaskSource <>).MakeGenericType(typeof(T),
                                                                        mcm.GenericTypes[md.ReturnType.Type.GenericParameterPosition]);
                    }

                    ITaskSource ts = (ITaskSource)Activator.CreateInstance(tcsType);
                    ts.Proxy       = this;
                    ts.MethodData  = md;
                    ts.CallMessage = mcm;
                    this.client.RequestAsync(md, mcm, mcm.InArgs.Length)
                    .ContinueWith(t =>
                    {
                        if (t.Status == TaskStatus.RanToCompletion)
                        {
                            ts.Complete(t.Result, null);
                        }
                        else
                        {
                            ts.Complete(null, t.Exception);
                        }
                    });
                    result = ts.TaskObject;
                }
                else
                {
                    throw new InvalidOperationException();
                }

                return(new ReturnMessage(result, null, 0, mcm));
            }
Exemplo n.º 5
0
        internal ExecutorThread(int executorId, ITaskSource taskSource, Logger logger = null)
        {
            this.executorId = executorId;
            this.taskSource = taskSource;
            this.logger     = logger;

            cancelTokenSource   = new CancellationTokenSource();
            wakeEvent           = new ManualResetEventSlim();
            thread              = new Thread(ExecuteLoop);
            thread.Name         = $"Executor_{executorId}";
            thread.IsBackground = true;
            thread.Priority     = ThreadPriority.AboveNormal;
            thread.Start();
        }
Exemplo n.º 6
0
        /// <summary>
        /// 任务的创建
        /// </summary>
        /// <param name="resultType">result类型</param>
        public TaskCompletionSource(Type resultType)
        {
            var type = typeof(TaskSource <>).MakeGenericType(resultType);

            this.taskSource = Activator.CreateInstance(type) as ITaskSource;
        }
Exemplo n.º 7
0
 public TestService(ITaskSource taskSource, IUnitOfWork unitOfWork, IMapper mapper)
     : base(unitOfWork, mapper)
 {
     this.taskSource = taskSource;
 }