Example #1
0
        public void EndAll()
        {
            for (byte i = 0; i < ECG_COROUTINE_SCHEDULE_MAX; ++i)
            {
                PoolIndicies[EMCgCoroutineSchedule.Get()[i]] = 0;
            }

            for (byte i = 0; i < ECG_COROUTINE_SCHEDULE_MAX; ++i)
            {
                FECgCoroutineSchedule schedule = EMCgCoroutineSchedule.Get()[i];
                int count = RoutinesRunning[schedule].Count;

                for (int j = 0; j < count; ++j)
                {
                    RoutinesRunning[schedule][j].End(ECgCoroutineEndReason.Shutdown);
                    RoutinesRunning[schedule][j].Reset();
                }
            }

            for (byte i = 0; i < ECG_COROUTINE_SCHEDULE_MAX; ++i)
            {
                Heads[EMCgCoroutineSchedule.Get()[i]] = null;
            }

            for (byte i = 0; i < ECG_COROUTINE_SCHEDULE_MAX; ++i)
            {
                Tails[EMCgCoroutineSchedule.Get()[i]] = null;
            }

            for (ushort i = 0; i < POOL_SIZE; ++i)
            {
                Payloads[i].Reset();
            }
        }
Example #2
0
        public void InsertRoutine(FECgCoroutineSchedule schedule, FCgRoutine pivot, FCgRoutine insert)
        {
            // Detach insert

            // insert is the Tail
            if (insert.Next == null)
            {
                Tails[schedule] = insert.Prev;
            }
            else
            {
                insert.Next.Prev = insert.Prev;
            }
            insert.Prev.Next = insert.Next;

            // Insert insert ahead of pivot

            // pivot is the Head
            if (pivot.Prev == null)
            {
                Heads[schedule] = insert;

                pivot.Prev  = insert;
                insert.Next = pivot;
                insert.Prev = null;
            }
            else
            {
                pivot.Prev.Next = insert;
                insert.Prev     = pivot.Prev;
                insert.Next     = pivot;
                pivot.Prev      = insert;
            }
        }
Example #3
0
        public FCgRoutine Start(FECgCoroutineSchedule schedule, IEnumerator fiber)
        {
            FCgCoroutinePayload payload = AllocatePayload();

            payload.Schedule = schedule;
            payload.Fiber    = fiber;

            return(Start(payload));
        }
Example #4
0
        public FCgCoroutineScheduler()
        {
            Pools = new Dictionary <FECgCoroutineSchedule, List <FCgRoutine> >(new FECgCoroutineScheduleEqualityComparer());

            for (byte i = 0; i < ECG_COROUTINE_SCHEDULE_MAX; ++i)
            {
                List <FCgRoutine>     routines = new List <FCgRoutine>();
                FECgCoroutineSchedule schedule = EMCgCoroutineSchedule.Get()[i];

                for (ushort j = 0; j < POOL_SIZE; ++j)
                {
                    routines.Add(new FCgRoutine(j, schedule, InsertRoutine));
                }
                Pools[EMCgCoroutineSchedule.Get()[i]] = routines;
            }

            PoolIndicies = new Dictionary <FECgCoroutineSchedule, int>(new FECgCoroutineScheduleEqualityComparer());

            for (byte i = 0; i < ECG_COROUTINE_SCHEDULE_MAX; ++i)
            {
                PoolIndicies[EMCgCoroutineSchedule.Get()[i]] = 0;
            }

            RoutinesRunning = new Dictionary <FECgCoroutineSchedule, List <FCgRoutine> >(new FECgCoroutineScheduleEqualityComparer());

            for (byte i = 0; i < ECG_COROUTINE_SCHEDULE_MAX; ++i)
            {
                RoutinesRunning[EMCgCoroutineSchedule.Get()[i]] = new List <FCgRoutine>();
            }

            Heads = new Dictionary <FECgCoroutineSchedule, FCgRoutine>(new FECgCoroutineScheduleEqualityComparer());

            for (byte i = 0; i < ECG_COROUTINE_SCHEDULE_MAX; ++i)
            {
                Heads[EMCgCoroutineSchedule.Get()[i]] = null;
            }

            Tails = new Dictionary <FECgCoroutineSchedule, FCgRoutine>(new FECgCoroutineScheduleEqualityComparer());

            for (byte i = 0; i < ECG_COROUTINE_SCHEDULE_MAX; ++i)
            {
                Tails[EMCgCoroutineSchedule.Get()[i]] = null;
            }

            Payloads = new List <FCgCoroutinePayload>();

            for (ushort i = 0; i < POOL_SIZE; ++i)
            {
                Payloads.Add(new FCgCoroutinePayload());
            }
        }
Example #5
0
 public void Reset()
 {
     bAllocated  = false;
     Schedule    = EMCgCoroutineSchedule.Get().GetMAX();
     StartTime   = 0.0f;
     Fiber       = null;
     Owner       = null;
     OwnerName   = "";
     RoutineType = FCgRoutine.INVALID_TYPE;
     Stop.Clear();
     Add    = null;
     Remove = null;
     Name   = "";
 }
Example #6
0
        public FCgRoutine Allocate(FCgCoroutinePayload payload)
        {
            FECgCoroutineSchedule schedule = payload.Schedule;

            FCgRoutine r = Allocate_Internal(schedule);

            r.Start(payload.Fiber, payload.Stop, payload.Owner, payload.OwnerName, payload.StartTime, payload.Add, payload.Remove, payload.RoutineType);
            r.State = ECgRoutineState.Allocating;

            LogTransaction(ECgCoroutineSchedulerCached.Str.Allocate, ECgCoroutineTransaction.Allocate, r);

            payload.Reset();

            return(r);
        }
Example #7
0
        public void BroadcastMessage(FECgCoroutineSchedule schedule, ECgCoroutineMessage msgType, string msg, object owner = null)
        {
            int count = RoutinesRunning[schedule].Count;

            for (int i = 0; i < count; ++i)
            {
                FCgRoutine r = RoutinesRunning[schedule][i];

                if (owner != null && owner != r.Owner.Get())
                {
                    continue;
                }

                r.ReceiveMessage(msgType, msg);
            }
        }
Example #8
0
        private FCgRoutine Allocate_Internal(FECgCoroutineSchedule schedule)
        {
            for (ushort i = 0; i < POOL_SIZE; ++i)
            {
                PoolIndicies[schedule] = (PoolIndicies[schedule] + 1) % POOL_SIZE;

                FCgRoutine r = Pools[schedule][PoolIndicies[schedule]];

                if (r.State != ECgRoutineState.Free)
                {
                    continue;
                }

                return(r);
            }
            Debug.LogError("FCgRoutine.Allocate: No free Routines. Pools[" + schedule.ToString() + "] is exhausted. Look for runaway Coroutines or consider raising the pool size.");
            return(null);
        }
Example #9
0
        public FCgRoutine Start(FCgCoroutinePayload payload)
        {
            FECgCoroutineSchedule schedule = payload.Schedule;

            FCgRoutine r = Allocate_Internal(schedule);

            if (r == null)
            {
                payload.Reset();
                return(null);
            }

            // If NO Head, Make r the Head
            if (Heads[schedule] == null)
            {
                Heads[schedule] = r;
                Tails[schedule] = r;
            }
            // Add r end of the list, Make r the Tail
            else
            {
                FCgRoutine tail = Tails[schedule];
                tail.Next       = r;
                r.Prev          = tail;
                Tails[schedule] = r;
            }

            RoutinesRunning[schedule].Add(r);

            // TODO: get Time from Manager_Time
            r.Start(payload.Fiber, payload.Stop, payload.Owner, payload.OwnerName, payload.StartTime, payload.Add, payload.Remove, payload.RoutineType);
            r.State = ECgRoutineState.Running;

            LogTransaction(ECgCoroutineSchedulerCached.Str.Start, ECgCoroutineTransaction.Start, r);

            payload.Reset();
            r.Run(0.0f);
            return(r);
        }
Example #10
0
        public FCgRoutine Start(FECgCoroutineSchedule schedule, FCgRoutine r)
        {
            // If NO Head, Make r the Head
            if (Heads[schedule] == null)
            {
                Heads[schedule] = r;
                Tails[schedule] = r;
            }
            // Add r end of the list, Make r the Tail
            else
            {
                FCgRoutine tail = Tails[schedule];
                tail.Next       = r;
                r.Prev          = tail;
                Tails[schedule] = r;
            }

            RoutinesRunning[schedule].Add(r);
            r.State = ECgRoutineState.Running;
            r.Run(0.0f);
            return(r);
        }
Example #11
0
        // Constructor

        public FCgRoutine(int index, FECgCoroutineSchedule schedule, InsertRoutineAheadOf insertRoutine)
        {
            Index    = index;
            Schedule = schedule;

            Prev = null;
            Next = null;

            State = ECgRoutineState.Free;

            Fiber       = null;
            Name        = "";
            RoutineType = INVALID_TYPE;

            bWaitingFor = false;
            WaitingFor  = null;
            Blocking    = null;

            InsertRoutine = insertRoutine;

            Parent   = null;
            Children = new List <FCgRoutine>();

            Owner         = new FCgAttribute();
            OwnerName     = "";
            StopCondition = new FCoroutineStopCondition();

            StopMessages      = new List <string>();
            Messages_Recieved = new Dictionary <ECgCoroutineMessage, List <string> >(new ECgCoroutineMessageEqualityComparer());

            for (byte i = 0; i < (byte)ECgCoroutineMessage.MAX; ++i)
            {
                Messages_Recieved[(ECgCoroutineMessage)i] = new List <string>();
            }

            Add    = new FAddRoutine();
            Remove = new FRemoveRoutine();

            StartTime   = 0.0f;
            ElapsedTime = 0.0f;
            DeltaTime   = 0.0f;
            TickCount   = 0;
            Delay       = 0.0f;

            bWaitForFrame            = false;
            WaitForFrameCounter      = 0;
            WaitForFrame             = 0;
            WaitForFrameType         = null;
            bWaitForTime             = false;
            WaitForTime              = 0.0f;
            WaitForTimeTimer         = 0.0f;
            WaitForTimeType          = null;
            bWaitForFlag             = false;
            WaitForBoolType          = null;
            WaitForFlagType          = null;
            bWaitForListenMessage    = false;
            WaitForListenMessage     = INVALID_LISTEN_MESSAGE;
            WaitForListenMessageType = null;

            EndReason = ECgCoroutineEndReason.MAX;

            Ints   = new int[INT_SIZE];
            Floats = new float[FLOAT_SIZE];
        }
Example #12
0
        public void Update(FECgCoroutineSchedule schedule, float deltaTime)
        {
            // Iterate through List
            FCgRoutine current = Heads[schedule];

            while (current != null)
            {
                if (current.State == ECgRoutineState.Running)
                {
                    current.Run(deltaTime);
                }

                if (current.State == ECgRoutineState.End)
                {
                    // Remove from List, Update Linkage. Prev / Next
                    if (current.Prev != null)
                    {
                        current.Prev.Next = current.Next;

                        if (current.Next != null)
                        {
                            current.Next.Prev = current.Prev;
                        }
                        // Update Tail
                        else
                        {
                            Tails[schedule] = current.Prev;
                        }
                    }
                    // Update Head
                    else
                    {
                        if (current.Next != null)
                        {
                            current.Next.Prev = null;
                            Heads[schedule]   = current.Next;
                        }
                        // Last node in List
                        else
                        {
                            Heads[schedule] = null;
                            Tails[schedule] = null;
                        }
                    }
                    LogTransaction("FCgRoutine.Update", ECgCoroutineTransaction.End, current);

                    FCgRoutine r = current;
                    current = current.Next;
                    r.Reset();
                }
                else
                {
                    current = current.Next;
                }
            }

            // Check RoutinesRunning for any Routines that have Ended
            int count = RoutinesRunning[schedule].Count;

            for (int i = count - 1; i >= 0; --i)
            {
                FCgRoutine r = RoutinesRunning[schedule][i];

                if (r.State == ECgRoutineState.Free)
                {
                    RoutinesRunning[schedule].RemoveAt(i);
                }
                else
                if (r.State == ECgRoutineState.End)
                {
                    if (r.EndReason == ECgCoroutineEndReason.EndOfExecution ||
                        r.EndReason == ECgCoroutineEndReason.Parent)
                    {
                        Debug.LogError("CgCoroutineSchedule.Update: Dangling Routine. Check iteration through List or if Routine was tampered with.");
                    }
                    r.Reset();
                    RoutinesRunning[schedule].RemoveAt(i);
                }
            }
        }
Example #13
0
 public bool HasCoroutines(FECgCoroutineSchedule schedule)
 {
     return(Heads[schedule] != null);
 }
Example #14
0
 public FCgRoutine PreAllocate(FECgCoroutineSchedule schedule)
 {
     return(Allocate_Internal(schedule));
 }