Пример #1
0
        public override void Tick()
        {
            Async.ContextThreadManager.Instance.mTPoolTrigger.WaitOne(20);

            var events = Async.ContextThreadManager.Instance.TPoolEvents;

            while (true)
            {
                Async.PostEvent e = null;
                lock (events)
                {
                    if (events.Count > 0)
                    {
                        e = events.Dequeue();
                    }
                    else
                    {
                        break;
                    }
                }
                if (e != null)
                {
                    try
                    {
                        e.PostAction();
                    }
                    catch (Exception ex)
                    {
                        Profiler.Log.WriteException(ex);
                        e.ExceptionInfo = ex;
                    }
                    CEngine.Instance.EventPoster.mRunOnPEAllocator.ReleaseObject(e);
                }
            }
            Async.ContextThreadManager.Instance.mTPoolTrigger.Reset();

            TickAwaitEvent();
        }
Пример #2
0
        public void OnCompleted(Action continuation)
        {
            bool isTargetIsCurrent = false;

            if (PEvent.AsyncType == EAsyncType.Normal)
            {
                if (ContextThread.CurrentContext == null)
                {
                    isTargetIsCurrent = false;
                }
                else
                {
                    isTargetIsCurrent = PEvent.AsyncTarget == ContextThread.CurrentContext;
                }
            }
            if (ContextThreadManager.ImmidiateMode || isTargetIsCurrent)
            {
                if (PEvent != null && PEvent.PostAction != null)
                {
                    var ret = PEvent.PostAction();
                    Result = ret;
                }
                ContinueAction = continuation;
                DoContinueAction();
                return;
            }
#if HAS_DebugInfo
            PEvent.CallStackTrace = new System.Diagnostics.StackTrace(2);
#endif
            PEvent.Awaiter.ContinueAction = continuation;
            switch (PEvent.AsyncType)
            {
            case EAsyncType.Normal:
            {
                lock (PEvent.AsyncTarget.AsyncEvents)
                {
                    PEvent.AsyncTarget.AsyncEvents.Enqueue(PEvent);
                }
            }
            break;

            case EAsyncType.AsyncIOEmpty:
            {
                System.Diagnostics.Debug.Assert(PEvent.AsyncTarget == null);
                System.Diagnostics.Debug.Assert(PEvent.PostAction == null);
                lock (ContextThreadManager.Instance.AsyncIOEmptys)
                {
                    ContextThreadManager.Instance.AsyncIOEmptys.Add(PEvent);
                }
            }
            break;

            case EAsyncType.Semaphore:
            {
                if (PEvent.Tag.GetCount() == 0)
                {
                    //EqueueContinue做了防止重复Enqueue的处理
                    //如果Release导致提前Enqueue了,这里就不会真的在入队列一次
                    //否则会出现已经完成的任务再转换的异常
                    //为什么这里还要入队一次,因为有低概率在Release的时候,PostEvent等待任务
                    //依然没有赋值好,这里做一次擦屁股的处理
                    PEvent.Tag.EqueueContinue();
                }
            }
            break;

            case EAsyncType.ParallelTasks:
            {
                lock (ContextThreadManager.Instance.TPoolEvents)
                {
                    ContextThreadManager.Instance.TPoolEvents.Enqueue(PEvent);
                    ContextThreadManager.Instance.mTPoolTrigger.Set();
                }
            }
            break;

            case EAsyncType.Delay:
            {
                System.Threading.ThreadPool.QueueUserWorkItem((data) =>
                    {
                        PEvent.PostAction();
                        lock (PEvent.ContinueThread.ContinueEvents)
                        {
                            PEvent.ContinueThread.ContinueEvents.Enqueue(PEvent);
                        }
                    });
            }
            break;
            }
        }