/// <summary>Returns true when new item was allocated</summary>
        public bool AllocateOrCreate(out T item)
        {
            bool flag = false;

            using (_activeLock.Acquire())
            {
                flag = _unused.Count == 0;
                item = !flag?_unused.Dequeue() : _activator();

                _active.Add(item);
            }
            return(flag);
        }
        private void DequeueDirtyChunk(out VoxelChunk chunk, out Vector3I coords)
        {
            coords = m_pendingChunksToWrite.Dequeue();
            m_cachedChunks.TryGetValue(coords, out chunk);

            Debug.Assert(chunk != null);
        }
예제 #3
0
        private void ParallelWorkCallback()
        {
            ProfilerShort.Begin("Planet::ParallelWork");

            int work    = PLANET_SECTOR_WORK_CYCLE;
            int discard = PLANET_SECTOR_IDLE_WORK_CYCLE;

            for (; work > 0 && SectorsToWorkParallel.Count > 0 && discard > 0;)
            {
                var sector = SectorsToWorkParallel.Dequeue();

                // When the sector is deleted it is sometimes impossible to ensure
                // it was not queued so we ignore it here
                if (sector.IsQueuedParallel)
                {
                    if (sector.DoParallelWork())
                    {
                        --work;
                    }
                    else
                    {
                        --discard;
                    }
                }

                // It may have new work already but will get re-scheduled eventually.
                sector.IsQueuedParallel = false;
            }
            ProfilerShort.End();
        }
예제 #4
0
        /// <summary>
        /// Returns true when new item was allocated
        /// </summary>
        public bool AllocateOrCreate(out T item)
        {
            bool create = (m_unused.Count == 0);

            if (create)
            {
                item = new T();
            }
            else
            {
                item = m_unused.Dequeue();
            }
            using (m_activeLock.Acquire())
            {
                m_active.Add(item);
            }
            return(create);
        }
예제 #5
0
        private void SerialWorkCallback()
        {
            int work = PLANET_SECTOR_WORK_CYCLE;

            for (; work > 0 && SectorsToWorkSerial.Count > 0; --work)
            {
                var sector = SectorsToWorkSerial.Dequeue();

                sector.DoSerialWork();
            }

            m_sectorsWorking = false;
        }
        private void RecordHistory(int lod, bool set)
        {
            if (m_lodHistory.Count > 10)
            {
                m_lodHistory.Dequeue();
            }

            m_lodHistory.Enqueue(new LodHEntry
            {
                Lod   = lod,
                Set   = set,
                Trace = new StackTrace(),
            });
        }
예제 #7
0
        /// <summary>
        /// Gets next frame for rendering, can return null in case there's nothing for rendering (no update frame submitted).
        /// When isPreFrame is true, don't handle draw messages, just process update messages and call method again.
        /// Pre frame must release messages and must be returned.
        /// Final frame is kept unmodified in queue, in case of slower update, so we can interpolate and draw frame again.
        /// </summary>
        public MyUpdateFrame GetRenderFrame(out bool isPreFrame)
        {
            if (m_updateDataQueue.Count > 1)
            {
                isPreFrame = true;
                return(m_updateDataQueue.Dequeue());
            }

            isPreFrame = false;

            MyUpdateFrame frame;

            return(m_updateDataQueue.TryPeek(out frame) ? frame : null);
        }
        private void ProcessNewlyAddedGrids()
        {
            List <TEntity> newEntities = new List <TEntity>();

            while (NewlyAddedEntities.Count > 0)
            {
                newEntities.Add(NewlyAddedEntities.Dequeue());
            }

            foreach (TEntity entity in newEntities)
            {
                HandleEntity(entity);
            }
        }
        private void SerialWorkCallback()
        {
            ProfilerShort.Begin("Planet::SerialWork");
            int work = m_sectorsToWorkSerial.Count;

            for (; work > 0 && m_sectorsToWorkSerial.Count > 0; --work)
            {
                var sector = m_sectorsToWorkSerial.Dequeue();

                if (!sector.HasParallelWorkPending)
                {
                    sector.DoSerialWork();
                }
                else
                {
                    m_sectorsToWorkSerial.Enqueue(sector); // Sometimes a sector is marked for serial when it has parallel pending= that should be done before.
                }
            }

            m_parallelInProgress = false;
            ProfilerShort.End();
        }
예제 #10
0
        public void IsEmptyTest()
        {
            //Enqueue
            for (int i = 0; i < threads.Length; i++)
            {
                threads[i] = new Thread(() => queue.Enqueue(5));
                threads[i].Start();
            }
            for (int i = 0; i < threads.Length; i++)
            {
                threads[i] = new Thread(() => queue.Dequeue());
                threads[i].Start();
            }

            foreach (var t in threads)
            {
                t.Join();
            }
            Console.WriteLine(queue.NumberOfElements);
            Assert.IsTrue(queue.NumberOfElements == 0);
            Assert.IsTrue(queue.IsEmpty());
        }
예제 #11
0
        /// <summary>
        ///     Processes the action queue
        /// </summary>
        public static void ProcessActionQueue()
        {
            if (Processing || ActionQueue.Count == 0)
            {
                return;
            }

            if (lastInvokeTask.Exceptions != null && lastInvokeTask.Exceptions.Length > 0)
            {
                throw lastInvokeTask.Exceptions[0];
            }

            Processing     = true;
            lastInvokeTask = MyAPIGateway.Parallel.Start(() =>
            {
                try
                {
                    var queueBlock = Profiler.Start(FullName, nameof(ProcessActionQueue));
                    while (ActionQueue.Count > 0)
                    {
                        Action action = ActionQueue.Dequeue();
                        action();
                    }
                    queueBlock.End();
                }
                catch (Exception ex)
                {
                    Logging.Instance.WriteLine("Exception in ProcessActionQueue: " + ex);
                    if (!SessionClosing && ShipyardCore.Debug)
                    {
                        throw;
                    }
                }
                finally
                {
                    Processing = false;
                }
            });
        }