Example #1
0
        // Loop to run on thread
        private void ProcessBackgroundQueueOnThread()
        {
            while (!ForceSingleThreaded)
            {
                if (Paused)
                {
                    Thread.Sleep(PausedSleepMS);
                    continue;
                }

                AsyncWorkUnit unit = null;
                lock (m_LockObject)
                {
                    if (m_BackgroundQueue.Count > 0)
                    {
                        unit = m_BackgroundQueue.Peek();
                    }
                }

                if (unit == null)
                {
                    Thread.Sleep(StarvationSleepMS);
                    continue;
                }

                AsyncWorkUnit.StepResult result = AsyncWorkUnit.StepResult.Incomplete;
                while (result.IsIncomplete() && !ForceSingleThreaded && !Paused)
                {
                    result = unit.ThreadedStep();
                    if (result.TickDelay > 0)
                    {
                        Thread.Sleep(TimeSpan.FromTicks(result.TickDelay));
                    }
                }

                if (!result.IsIncomplete())
                {
                    if (result.Type == AsyncWorkUnit.StepResultType.Complete)
                    {
                        m_Scheduler.Log("Completed {0}", unit);
                    }

                    lock (m_LockObject)
                    {
                        m_BackgroundQueue.Dequeue();
                    }

                    m_Scheduler.FreeUnit(unit);
                }
            }

            lock (m_LockObject)
            {
                m_Thread = null;
            }
        }
Example #2
0
        // Processes scheduled work by time-slicing.
        private void ProcessBlockingQueue(Stopwatch inStopwatch, long inTotalBudget, long inSliceBudget, Queue <AsyncWorkUnit> ioQueue, bool inbCheckThread, ref long ioTicksRemaining)
        {
            #if SUPPORTS_THREADING
            if (inbCheckThread)
            {
                // if we have a running thread, don't process this frame
                lock (m_LockObject)
                {
                    if (m_Thread != null)
                    {
                        ioTicksRemaining = inTotalBudget - inStopwatch.ElapsedTicks;
                        return;
                    }
                }
            }
            #endif // SUPPORTS_THREADING

            if (m_NextBlockingTick > 0)
            {
                long current = Stopwatch.GetTimestamp();
                if (current < m_NextBlockingTick)
                {
                    ioTicksRemaining = inTotalBudget - inStopwatch.ElapsedTicks;
                    return;
                }

                m_NextBlockingTick = 0;
            }

            long timestamp = inStopwatch.ElapsedTicks;
            long cutoff    = timestamp + inSliceBudget;
            while (!Paused && ioTicksRemaining > 0 && ioQueue.Count > 0 && timestamp < cutoff && m_NextBlockingTick <= 0)
            {
                AsyncWorkUnit unit = ioQueue.Peek();

                AsyncWorkUnit.StepResult result = AsyncWorkUnit.StepResult.Incomplete;
                while (result.IsIncomplete() && ioTicksRemaining > 0 && !Paused && timestamp < cutoff && m_NextBlockingTick <= 0)
                {
                    result = unit.Step();
                    if (result.TickDelay > 0)
                    {
                        m_NextBlockingTick = Stopwatch.GetTimestamp() + result.TickDelay;
                    }

                    timestamp        = inStopwatch.ElapsedTicks;
                    ioTicksRemaining = inTotalBudget - timestamp;
                }

                if (!result.IsIncomplete())
                {
                    if (result.Type == AsyncWorkUnit.StepResultType.Complete)
                    {
                        m_Scheduler.Log("Completed {0}", unit);
                    }

                    ioQueue.Dequeue();
                    m_Scheduler.FreeUnit(unit);

                    timestamp        = inStopwatch.ElapsedTicks;
                    ioTicksRemaining = inTotalBudget - timestamp;
                }
            }
        }