示例#1
0
 /// <summary>
 /// Applies the context changes over thread pool.
 /// </summary>
 private void ApplyContextChangesOverThreadPool(LinkChain <ContextChange> changeChain)
 {
     unchecked
     {
         ContextChange nextLink;
         ContextChange context      = changeChain.Head;
         int           contextCount = changeChain.Count;
         int           contextIndex;
         for (contextIndex = 0; contextIndex <= (contextCount - 5); contextIndex += 5)
         {
             ContextChange context0 = context; nextLink = context0.NextLink; context0.NextLink = null;  //+ paranoia
             ContextChange context1 = nextLink; nextLink = context1.NextLink; context1.NextLink = null; //+ paranoia
             ContextChange context2 = nextLink; nextLink = context2.NextLink; context2.NextLink = null; //+ paranoia
             ContextChange context3 = nextLink; nextLink = context3.NextLink; context3.NextLink = null; //+ paranoia
             ContextChange context4 = nextLink; nextLink = context4.NextLink; context4.NextLink = null; //+ paranoia
             context = nextLink;
             _threadPool.Add(new ContextChange[] { context0, context1, context2, context3, context4 });
         }
         if (contextIndex != contextCount)
         {
             contextCount -= contextIndex;
             ContextChange[] contextArray = new ContextChange[contextCount];
             contextIndex = 0;
             //for (; context != null; context = nextLink)
             for (; contextIndex < contextCount; context = nextLink)
             {
                 nextLink = context.NextLink; context.NextLink = null; //+ paranoia
                 contextArray[contextIndex++] = context;
             }
             _threadPool.Add(contextArray);
         }
         //+ clear
         changeChain.Clear();
     }
 }
示例#2
0
 internal static void SetActiveContext(IntPtr context)
 {
     if (IsActive(context))
     {
         return;
     }
     Glfw.MakeContextCurrent(context);
     ContextChange?.Invoke(new ContextInfo(context));
 }
示例#3
0
 /// <summary>
 /// Applies the context changes inline.
 /// </summary>
 private void ApplyContextChangesInline(LinkChain <ContextChange> changeChain)
 {
     unchecked
     {
         ContextChange nextLink;
         for (ContextChange context = changeChain.Head; context != null; context = nextLink)
         {
             nextLink = context.NextLink; context.NextLink = null; //+ paranoia
             context.Execute(null);
         }
         //+ clear
         changeChain.Clear();
     }
 }
示例#4
0
 /// <summary>
 /// Adds the state change.
 /// </summary>
 /// <param name="context">The context.</param>
 /// <param name="stateFlag">The state flag.</param>
 public void AddContextChange(ContextChange change, ulong updateVectors)
 {
     if ((change.NextContextChange != null) || (_contextChangeLink == change))
     {
         throw new System.InvalidOperationException(); //+ paranoia
     }
     lock (_addContextLock)
     {
         _contextChangeLinkCount++;
         change.UpdateVectors     = updateVectors;
         change.NextContextChange = _contextChangeLink;
         _contextChangeLink       = change;
     }
 }
示例#5
0
 /// <summary>
 /// Adds the change.
 /// </summary>
 /// <param name="change">The change.</param>
 /// <param name="updateVectors">The update vectors.</param>
 public void AddChange(ContextChange change, ulong updateVectors)
 {
     _timeEngine.AddChange(this, change, updateVectors);
 }
示例#6
0
 /// <summary>
 /// Adds the state change.
 /// </summary>
 /// <param name="context">The context.</param>
 /// <param name="stateFlag">The state flag.</param>
 internal void AddChange(ThreadContext context, ContextChange change, ulong updateVectors)
 {
     change.UpdateVectors = updateVectors;
     context.ChangeChain.AddFirst(change);
 }
示例#7
0
        /// <summary>
        /// Evaluates the frame.
        /// </summary>
        /// <param name="mSecond">The m second.</param>
        public void EvaluateFrame(ulong time)
        {
            //System.Console.WriteLine("Timeline: EvaluateFrame {" + time.ToString() + "}");
            _isRebuildList = true;
            _isRebuildLink = true;
            ulong listFractionTime;
            ulong linkFractionTime;
            int   listFractionTimeIndex = 0;
            int   linkFractionTimeIndex = 0;
            bool  isFirstLoop           = true;

            while (true)
            {
                //+ check for escape
                if (time <= 0)
                {
                    return;
                }
                //+ new timelice
                Timeslice timeslice = _timeslices[_timesliceIndex];
                Dictionary <ulong, List <TElement> > list = timeslice.List;
                Dictionary <ulong, LinkHead>         link = timeslice.Link;
                listFractionTime = FillWorkingFractionArray <List <TElement> >(list, ref _listWorkingFractionArray, TimePrecision.ListWorkingFractionSize, ref listFractionTimeIndex, ref _listMaxWorkingFraction);
                linkFractionTime = FillWorkingFractionArray <LinkHead>(link, ref _linkWorkingFractionArray, TimePrecision.LinkWorkingFractionSize, ref linkFractionTimeIndex, ref _linkMaxWorkingFraction);
                //+ process time-slices
                ulong fractionTime = System.Math.Min(listFractionTime, linkFractionTime);
                if (isFirstLoop == true)
                {
                    isFirstLoop = false;
                    time       += fractionTime;
                }
                ulong lastFractionTime = 0;
                while (fractionTime < ulong.MaxValue)
                {
                    //+ advance time-slice + check for escape
                    time -= (fractionTime - lastFractionTime);
                    if (time <= 0)
                    {
                        return;
                    }
                    //+ evaluate time
                    #region Evalutate Time
                    if (listFractionTime == fractionTime)
                    {
                        List <TElement> objectList = list[listFractionTime];
                        int             objectIndex;
                        int             objectListCount = objectList.Count;
                        for (objectIndex = 0; objectIndex <= (objectListCount - 5); objectIndex += 5)
                        {
                            _threadPool.Add(objectList.GetRange(objectIndex, 5));
                        }
                        if (objectIndex != objectListCount)
                        {
                            _threadPool.Add(objectList.GetRange(objectIndex, objectListCount - objectIndex));
                        }
                        //+ next
                        list.Remove(listFractionTime);
                        listFractionTime = (listFractionTimeIndex > 0 ? _listWorkingFractionArray[--listFractionTimeIndex] : ulong.MaxValue);
                    }
                    if (linkFractionTime == fractionTime)
                    {
                        LinkBase nextItem;
                        LinkHead head      = link[linkFractionTime];
                        LinkBase item      = head.Chain;
                        int      itemCount = head.Count;
                        int      itemIndex;
                        for (itemIndex = 0; itemIndex <= (itemCount - 5); itemIndex += 5)
                        {
                            LinkBase item0 = item; nextItem = item0.NextLink; item0.NextLink = null;     //+ paranoia
                            LinkBase item1 = nextItem; nextItem = item1.NextLink; item1.NextLink = null; //+ paranoia
                            LinkBase item2 = nextItem; nextItem = item2.NextLink; item2.NextLink = null; //+ paranoia
                            LinkBase item3 = nextItem; nextItem = item3.NextLink; item3.NextLink = null; //+ paranoia
                            LinkBase item4 = nextItem; nextItem = item4.NextLink; item4.NextLink = null; //+ paranoia
                            item = nextItem;
                            _threadPool.Add(new LinkBase[] { item0, item1, item2, item3, item4 });
                        }
                        if (itemIndex != itemCount)
                        {
                            itemCount -= itemIndex;
                            LinkBase[] itemArray = new LinkBase[itemCount];
                            itemIndex = 0;
                            //for (; itemIndex < itemCount; /*item != null;*/ item = nextItem)
                            //for (; item != null; item = nextItem)
                            for (; itemIndex < itemCount; item = nextItem)
                            {
                                nextItem = item.NextLink; item.NextLink = null; //+ paranoia
                                itemArray[itemIndex++] = item;
                            }
                            _threadPool.Add(itemArray);
                        }
                        //+ next
                        link.Remove(linkFractionTime);
                        linkFractionTime = (linkFractionTimeIndex > 0 ? _linkWorkingFractionArray[--linkFractionTimeIndex] : ulong.MaxValue);
                    }
                    #endregion Evalutate Time
                    _threadPool.Join();
                    //System.Console.WriteLine("Timeslice: " + time.ToString());
                    //+ process state-change
                    #region Process State-Change
                    ContextChange nextContext;
                    //for (Context context = m_contextChangeLink; context != null; context = nextContext)
                    //{
                    //    nextContext = context.NextContext;
                    //    context.NextContext = null; //+ paranoia
                    //    context.Execute();
                    //}
                    ContextChange context      = _contextChangeLink;
                    int           contextCount = _contextChangeLinkCount;
                    int           contextIndex;
                    for (contextIndex = 0; contextIndex <= (contextCount - 5); contextIndex += 5)
                    {
                        ContextChange context0 = context; nextContext = context0.NextContextChange; context0.NextContextChange = null;     //+ paranoia
                        ContextChange context1 = nextContext; nextContext = context1.NextContextChange; context1.NextContextChange = null; //+ paranoia
                        ContextChange context2 = nextContext; nextContext = context2.NextContextChange; context2.NextContextChange = null; //+ paranoia
                        ContextChange context3 = nextContext; nextContext = context3.NextContextChange; context3.NextContextChange = null; //+ paranoia
                        ContextChange context4 = nextContext; nextContext = context4.NextContextChange; context4.NextContextChange = null; //+ paranoia
                        context = nextContext;
                        _threadPool.Add(new ContextChange[] { context0, context1, context2, context3, context4 });
                    }
                    if (contextIndex != contextCount)
                    {
                        contextCount -= contextIndex;
                        ContextChange[] contextArray = new ContextChange[contextCount];
                        contextIndex = 0;
                        //for (; contextIndex < itemCount; /*context != null;*/ context = nextContext)
                        //for (; context != null; context = nextContext)
                        for (; contextIndex < contextCount; context = nextContext)
                        {
                            nextContext = context.NextContextChange; context.NextContextChange = null; //+ paranoia
                            contextArray[contextIndex++] = context;
                        }
                        _threadPool.Add(contextArray);
                    }
                    //+ clear
                    _contextChangeLink      = null;
                    _contextChangeLinkCount = 0;
                    #endregion Process State-Change
                    //+
                    if ((_isRebuildList == true) || (_listMaxWorkingFraction == fractionTime))
                    {
                        _isRebuildList   = false;
                        listFractionTime = FillWorkingFractionArray <List <TElement> >(list, ref _listWorkingFractionArray, TimePrecision.ListWorkingFractionSize, ref listFractionTimeIndex, ref _listMaxWorkingFraction);
                    }
                    if ((_isRebuildLink == true) || (_linkMaxWorkingFraction == fractionTime))
                    {
                        _isRebuildLink   = false;
                        linkFractionTime = FillWorkingFractionArray <LinkHead>(link, ref _linkWorkingFractionArray, TimePrecision.LinkWorkingFractionSize, ref linkFractionTimeIndex, ref _linkMaxWorkingFraction);
                    }
                    lastFractionTime = fractionTime;
                    fractionTime     = System.Math.Min(listFractionTime, linkFractionTime);
                    _threadPool.Join();
                }
                //+ advance time
                _isRebuildList = true;
                _isRebuildLink = true;
                time          -= (TimePrecision.TimeScaler - lastFractionTime);
                _timesliceIndex++;
                if (_timesliceIndex >= TimesliceArraySize)
                {
                    _timesliceIndex = 0;
                    //System.Console.WriteLine("Timeline: ccycle");
                    //+ dehibernate
                    #region Dehibernate
                    for (LinkedListNode <Hibernate> hibernateNode = _hibernateList.First; hibernateNode != null; hibernateNode = hibernateNode.Next)
                    {
                        Hibernate hibernate = hibernateNode.Value;
                        ulong     newTime   = (hibernate.Time -= TimesliceArrayTime);
                        if (newTime < TimesliceArrayTime)
                        {
                            if (newTime < 0)
                            {
                                throw new System.InvalidOperationException(); //+ paranoia
                            }
                            //+ remove node
                            _hibernateList.Remove(hibernateNode);
                            //+ add to timeline
                            System.Console.WriteLine("Timeline: Dehibernate {" + newTime.ToString() + "}");
                            Add(hibernate.Object, newTime);
                        }
                        else
                        {
                            hibernateNode.Value = hibernate;
                        }
                    }
                    LinkBase lastObject = _hibernateLink;
                    for (LinkBase @object = _hibernateLink; @object != null; @object = @object.NextLink)
                    {
                        ulong newTime = (@object.HibernateTime -= TimesliceArrayTime);
                        if (newTime < TimesliceArrayTime)
                        {
                            if (newTime < 0)
                            {
                                throw new System.InvalidOperationException(); //+ paranoia
                            }
                            //+ remove node
                            //lastObject = (m_hibernateLink != @object ? (lastObject.NextTimeLink = @object.NextTimeLink) : (m_hibernateLink = @object.NextTimeLink));
                            lastObject       = (lastObject.NextLink = @object.NextLink);
                            @object.NextLink = null; //+ paranoia
                            //+ add to timeline
                            System.Console.WriteLine("Timeline: Dehibernate {" + newTime.ToString() + "}");
                            Add(@object, newTime);
                        }
                        else
                        {
                            lastObject = @object;
                        }
                    }
                }
                #endregion Dehibernate
            }
        }