Esempio n. 1
0
        public static void Log(ThreadWorker worker, string message, bool logToConsole = false)
        {
                                #if WDEBUG
            //seems that conditional does not work in some cases

            if (!logging)
            {
                return;
            }

            if (log.Count > 50000)
            {
                log.RemoveRange(0, 20000);
            }

            LogEntity logEntity = new LogEntity()
            {
                worker = worker, name = message, stage = worker.stage, stop = worker.stop, threadId = Thread.CurrentThread.ManagedThreadId
            };
            if (worker.threadCondition != null)
            {
                if (worker.threadCondition())
                {
                    logEntity.threadCondition = LogEntity.ConditionMet.True;
                }
                else
                {
                    logEntity.threadCondition = LogEntity.ConditionMet.False;
                }
            }
            if (worker.applyCondition != null)
            {
                if (worker.applyCondition())
                {
                    logEntity.applyCondition = LogEntity.ConditionMet.True;
                }
                else
                {
                    logEntity.applyCondition = LogEntity.ConditionMet.False;
                }
            }
            logEntity.time = DateTime.Now;

            logEntity.locked = worker.lockWasTaken;

            log.Add(logEntity);

            if (logToConsole)
            {
                Debug.LogError(worker.name + " " + message);
            }
                                #endif
        }
Esempio n. 2
0
        public static void UpdateApply()
        {
            if (!on || queue.Count == 0)
            {
                return;
            }

                        #if WDEBUG
            Profiler.BeginSample("Update Apply");
                        #endif

            System.Diagnostics.Stopwatch timer = new System.Diagnostics.Stopwatch();
            timer.Start();

            while (timer.ElapsedMilliseconds < maxApplyTime)
            {
                //if couroutine has started - moving coroutine
                if (CurrentCoroutine != null)
                {
                    currentCoroutineWorker.CoroutineFn(); continue;
                }

                //finding suitable worker with highest priority
                float maxProirity = -2000000;
//				int maxProirityNum = -1;
                ThreadWorker maxPriorityWorker = null;

                int queueCount = queue.Count;
                for (int i = 0; i < queueCount; i++)
                {
                    ThreadWorker worker = queue[i];

                    if (worker == null)
                    {
                        continue;                                   //if object destroyed
                    }
                    if (worker.priority < maxProirity)
                    {
                        continue;
                    }
                    if (worker.stage != Stage.applyEnqueued && worker.stage != Stage.prepareEnqueued && worker.stage != Stage.coroutineEnqueued && worker.stage != Stage.coroutineRunning)
                    {
                        continue;                                                                                                                                                                                //other stage
                    }
                    if (worker.stage == Stage.prepareEnqueued && worker.prepareCondition != null && !worker.prepareCondition())
                    {
                        continue;
                    }
                    if (worker.stage == Stage.applyEnqueued && worker.applyCondition != null && !worker.applyCondition())
                    {
                        continue;                                                                                                                   //if apply condition has not met
                    }
                    if (worker.stage == Stage.coroutineEnqueued && worker.coroutineCondition != null && !worker.coroutineCondition())
                    {
                        continue;                                                                                                                               //if coroutine condition has not met (note that conditions is checked only before starting coroutine)
                    }
                    maxPriorityWorker = worker;
                    maxProirity       = worker.priority;
//					maxProirityNum = i;
                }

                //no suitable applies
                if (maxPriorityWorker == null)
                {
                    break;
                }

                //apply
                //lock (maxPriorityWorker.locker)
                Monitor.Enter(maxPriorityWorker.locker); maxPriorityWorker.lockWasTaken = true;
                try
                {
                    if (logging)
                    {
                        Log(maxPriorityWorker, "Refresh:ApplyPrepSelected");
                    }

                    if (maxPriorityWorker.stage == Stage.prepareEnqueued)
                    {
                        //maxPriorityWorker.SwitchStage(Stage.applyRunning);
                        maxPriorityWorker.PrepareFn();
                    }

                    if (maxPriorityWorker.stage == Stage.applyEnqueued)
                    {
                        //maxPriorityWorker.SwitchStage(Stage.applyRunning);
                        maxPriorityWorker.ApplyFn();
                    }

                    if (maxPriorityWorker.stage == Stage.coroutineEnqueued || maxPriorityWorker.stage == Stage.coroutineRunning)
                    {
                        maxPriorityWorker.CoroutineFn();
                    }
                }
                finally { Monitor.Exit(maxPriorityWorker.locker); maxPriorityWorker.lockWasTaken = false; }
            }

                        #if WDEBUG
            Profiler.EndSample();
                        #endif
        }