Exemple #1
0
        public bool DoOnePriorityEvent(out Async.PostEvent oe)
        {
            oe = null;
            Async.PostEvent e;
            lock (PriorityEvents)
            {
                if (PriorityEvents.Count == 0)
                {
                    return(false);
                }
                e = PriorityEvents.Dequeue();
            }

            try
            {
                if (e.PostAction != null)
                {
                    var ret = e.PostAction();
                    var caw = e.Awaiter as Async.TaskAwaiter;
                    if (caw != null)
                    {
                        caw.SetResult(ret);
                    }
                }
                ExecuteContinue(e.Awaiter);
            }
            catch (Exception ex)
            {
                Profiler.Log.WriteException(ex);
                e.ExceptionInfo = ex;
            }
            oe = e;
            return(true);
        }
Exemple #2
0
        public async System.Threading.Tasks.Task AwaitAsyncIOEmpty()
        {
            var eh = new PostEvent();

            eh.PostAction     = null;
            eh.AsyncTarget    = null;
            eh.ContinueThread = ContextThread.CurrentContext;
            eh.AsyncType      = EAsyncType.AsyncIOEmpty;
            eh.Tag            = null;

            var source = new System.Threading.Tasks.TaskCompletionSource <object>();
            await source.Task.AwaitPost(eh);
        }
Exemple #3
0
        public async System.Threading.Tasks.Task <T> Post <T>(FPostEventReturn <T> evt, EAsyncTarget target = EAsyncTarget.AsyncIO)
        {
            var           eh  = new PostEvent();
            ContextThread ctx = GetContext(target);

            eh.AsyncTarget    = ctx;
            eh.ContinueThread = ContextThread.CurrentContext;
            if (eh.ContinueThread == this.ContextRHI)
            {
                switch (eh.ContinueThread.TickFlag)
                {
                case 1:
                    eh.ContinueThread = this.ContextMain;
                    break;

                case 2:
                    eh.ContinueThread = this.ContextRHI;
                    break;

                case 3:
                    eh.ContinueThread = this.ContextEditor;
                    break;
                }
            }
            FPostEvent ue = () =>
            {
                try
                {
                    var result = evt();
                    return(result);
                }
                catch (Exception ex)
                {
                    Profiler.Log.WriteException(ex);
                    return(default(T));
                }
            };

            eh.PostAction = ue;
            var    source = new System.Threading.Tasks.TaskCompletionSource <object>();
            object ret    = await source.Task.AwaitPost(eh);

            try
            {
                return((T)ret);
            }
            catch
            {
                return(default(T));
            }
        }
Exemple #4
0
        public async System.Threading.Tasks.Task DelayTime(int time)
        {
            var eh = new PostEvent();

            eh.PostAction     = null;
            eh.ContinueThread = ContextThread.CurrentContext;
            eh.AsyncType      = EAsyncType.Delay;
            eh.PostAction     = () =>
            {
                System.Threading.Thread.Sleep(time);
                return(null);
            };
            var source = new System.Threading.Tasks.TaskCompletionSource <object>();
            await source.Task.AwaitPost(eh);
        }
Exemple #5
0
        public async System.Threading.Tasks.Task AwaitSemaphore(ASyncSemaphore smp)
        {
            if (smp == null)
            {
                Profiler.Log.WriteLine(Profiler.ELogTag.Error, "", $"AwaitSemaphore is null");
                return;
            }
            var eh = new PostEvent();

            eh.PostAction     = null;
            eh.AsyncTarget    = null;
            eh.ContinueThread = ContextThread.CurrentContext;
            eh.AsyncType      = EAsyncType.Semaphore;
            eh.Tag            = smp;
            smp.PostEvent     = eh;

            var source = new System.Threading.Tasks.TaskCompletionSource <object>();
            await source.Task.AwaitPost(eh);

            smp = null;
        }
Exemple #6
0
        //protected void DoPriorityEvents()
        //{
        //    while(PriorityEvents.Count>0)
        //    {
        //        Async.PostEvent e;
        //        lock (PriorityEvents)
        //        {
        //            e = PriorityEvents.Dequeue();
        //        }
        //        try
        //        {
        //            if (e.PostAction != null)
        //            {
        //                var ret = e.PostAction();
        //                var caw = e.Awaiter as Async.TaskAwaiter;
        //                if(caw!=null)
        //                    caw.SetResult(ret);
        //            }
        //            ExecuteContinue(e.Awaiter);
        //        }
        //        catch (Exception ex)
        //        {
        //            Profiler.Log.WriteException(ex);
        //            e.ExceptionInfo = ex;
        //        }
        //    }
        //}
        protected bool DoOneAsyncEvent(out Async.PostEvent oe)
        {
            oe = null;
            Async.PostEvent e;
            lock (AsyncEvents)
            {
                if (AsyncEvents.Count == 0)
                {
                    return(false);
                }
                e = AsyncEvents.Dequeue();
            }
            try
            {
                var ret = e.PostAction();
                var caw = e.Awaiter as Async.TaskAwaiter;
                caw.SetResult(ret);
            }
            catch (Exception ex)
            {
                Profiler.Log.WriteException(ex);
                e.ExceptionInfo = ex;
                var caw = e.Awaiter as Async.TaskAwaiter;
                caw.SetResult(null);
            }
            if (e.Tag == null)
            {
                if (e.ContinueThread != null)
                {
                    lock (e.ContinueThread.ContinueEvents)
                    {
                        e.ContinueThread.ContinueEvents.Enqueue(e);
                    }
                }
            }

            oe = e;
            return(true);
        }
Exemple #7
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();
        }
Exemple #8
0
 protected bool DoOneContnueEvent(out Async.PostEvent oe)
 {
     oe = null;
     Async.PostEvent e;
     lock (ContinueEvents)
     {
         if (ContinueEvents.Count == 0)
         {
             return(false);
         }
         e = ContinueEvents.Dequeue();
     }
     try
     {
         ExecuteContinue(e.Awaiter);
     }
     catch (Exception ex)
     {
         Profiler.Log.WriteException(ex);
         e.ExceptionInfo = ex;
     }
     oe = e;
     return(true);
 }
Exemple #9
0
 public static TaskAwaiter AwaitPost(this System.Threading.Tasks.Task task, PostEvent pe)
 {
     pe.Awaiter = new TaskAwaiter(task, pe);
     return(pe.Awaiter);
 }
Exemple #10
0
 public TaskAwaiter(System.Threading.Tasks.Task task, PostEvent pe)
 {
     PEvent     = pe;
     pe.Awaiter = this;
     this.task  = task;
 }