Example #1
0
        internal void Enqueue(Action <Camera> queueAction, Camera camera, PriorityValue priority)
        {
            _queue.Enqueue(new ActionInfo(queueAction, true, _dispatcher, camera), new Priority(priority));

            if (_queue.Count > 1)
            {
                return;
            }

            ActionInfo result = null;

            result = _queue.Peek();
            //_queue.TryPeek(out result);
            if (result != null)
            {
                _queue.UpdatePriority(result, new Priority(PriorityValue.Critical));
                try
                {
                    result.Exec();
                }
                catch (Exception e)
                {
                    Debug.WriteLine("message: {0}; stacktrace: {1}", e.Message, e.StackTrace);
                }
            }
        }
Example #2
0
        onRunningTask(PriorityValuePair <Task> elem)
        {
            #region Increment counters

            // Based on its priority, increment the appropriate active counter.
            if (elem.Priority >= MinCriticalPriority)
            {
                // Lock the thread -- CRITICAL SECTION BEGIN
                Monitor.Enter(__runningCriticalTasksLock);
                try
                {
                    __runningCriticalTasks++;
                }
                finally
                {
                    Monitor.Exit(__runningCriticalTasksLock);
                    // Unlock the thread -- CRITICAL SECTION END
                }
            }
            else
            {
                // Lock the thread -- CRITICAL SECTION BEGIN
                Monitor.Enter(__runningNoncriticalTasksLock);
                try
                {
                    __runningNoncriticalTasks++;
                }
                finally
                {
                    Monitor.Exit(__runningNoncriticalTasksLock);
                    // Unlock the thread -- CRITICAL SECTION END
                }
            }

            #endregion

            #region Emit events

            // Emit an event to notify listeners that a task has begun executing.
            if (TaskStarted != null)
            {
                TaskStarted();
            }

            // Emit an event to notify listeners that the queue is empty (if so).
            if (QueueEmpty != null && __queue.Peek() != null)
            {
                QueueEmpty();
            }

            #endregion
        }
Example #3
0
        public void Peek()
        {
            // Create a new priority queue.
            ConcurrentPriorityQueue <int> queue = new ConcurrentPriorityQueue <int>();

            // Ensure that the heap is empty.
            Assert.That(queue.Count, Is.EqualTo(0));

            // Expect Peek() to return null for an empty heap.
            Assert.That(queue.Peek(), Is.EqualTo(null));

            // Ensure that the queue is empty.
            Assert.That(queue.Count, Is.EqualTo(0));

            // Store an element and insert it into the queue.
            PriorityValuePair <int> elem1 = new PriorityValuePair <int>(1.0, 2);

            queue.Enqueue(elem1);

            // Ensure that the element was inserted into the queue at the front.
            Assert.That(queue.Count, Is.EqualTo(1));
            Assert.That(queue.Peek(), Is.EqualTo(elem1));

            // Ensure that the element was not removed from the heap.
            Assert.That(queue.Count, Is.EqualTo(1));

            // Insert another element with higher priority than the last.
            PriorityValuePair <int> elem2 = new PriorityValuePair <int>(2.0, 4);

            queue.Enqueue(elem2);

            // Ensure that Peek() returns the new front element.
            Assert.That(queue.Peek(), Is.EqualTo(elem2));

            // Insert another element with the same priority as the last.
            PriorityValuePair <int> elem3 = new PriorityValuePair <int>(2.0, 6);

            queue.Enqueue(elem3);

            // Ensure that Peek() returns still returns the first value with that priority.
            Assert.That(queue.Peek(), Is.EqualTo(elem2));

            // Remove the element from the queue.
            queue.Dequeue();

            // Ensure that Peek() returns now returns the other value with the same priorty.
            Assert.That(queue.Peek(), Is.EqualTo(elem3));
        }
Example #4
0
        public void Dequeue()
        {
            // Create a new priority queue.
            ConcurrentPriorityQueue <int> queue = new ConcurrentPriorityQueue <int>();

            // Ensure that the heap is empty.
            Assert.That(queue.Count, Is.EqualTo(0));

            // Expect Dequeue() to return null for an empty heap.
            Assert.That(queue.Dequeue(), Is.EqualTo(null));

            // Ensure that the heap is empty.
            Assert.That(queue.Count, Is.EqualTo(0));

            // Store an element and insert it into the heap.
            PriorityValuePair <int> elem = new PriorityValuePair <int>(1.0, 2);

            queue.Enqueue(elem);

            // Ensure that the element was inserted into the heap.
            Assert.That(queue.Count, Is.EqualTo(1));
            Assert.That(queue.Peek(), Is.EqualTo(elem));

            // Ensure that the PriorityAdjustment was incremented.
            Assert.That(queue.PriorityAdjustment, Is.EqualTo(ConcurrentPriorityQueue <int> .EPSILON));

            // Ensure that the returned element points to the same object we stored earlier.
            Assert.That(queue.Dequeue(), Is.EqualTo(elem));

            // Ensure that the element was removed from the heap.
            Assert.That(queue.Count, Is.EqualTo(0));

            // Ensure that the PriorityAdjustment was reset once the queue became empty.
            Assert.That(queue.PriorityAdjustment, Is.EqualTo(0.0));

            // Enqueue 5 items with the same priority.
            PriorityValuePair <int> elem2 = new PriorityValuePair <int>(2.0, 0);
            PriorityValuePair <int> elem3 = new PriorityValuePair <int>(2.0, 2);
            PriorityValuePair <int> elem4 = new PriorityValuePair <int>(2.0, 4);
            PriorityValuePair <int> elem5 = new PriorityValuePair <int>(2.0, 6);
            PriorityValuePair <int> elem6 = new PriorityValuePair <int>(2.0, 8);

            queue.Enqueue(elem2);
            queue.Enqueue(elem3);
            queue.Enqueue(elem4);
            queue.Enqueue(elem5);
            queue.Enqueue(elem6);

            //// Ensure that Dequeue() returns the items in the order they were enqueued.
            Assert.That(queue.Dequeue(), Is.EqualTo(elem2));
            Assert.That(queue.Dequeue(), Is.EqualTo(elem3));
            Assert.That(queue.Dequeue(), Is.EqualTo(elem4));
            Assert.That(queue.Dequeue(), Is.EqualTo(elem5));
            Assert.That(queue.Dequeue(), Is.EqualTo(elem6));
        }
Example #5
0
        public void EnqueuePriorityValue()
        {
            // Create a new priority queue.
            ConcurrentPriorityQueue <int> queue = new ConcurrentPriorityQueue <int>();

            // Ensure that queue is empty.
            Assert.That(queue.Count, Is.EqualTo(0));

            // Store an element and insert it into the queue.
            queue.Enqueue(1.0, 2);

            // Ensure that the element was inserted into the queue.
            Assert.That(queue.Peek().Value, Is.EqualTo(2));

            // Store another element with higher priority and insert it as well.
            queue.Enqueue(2.0, 4);

            // Ensure that the element was inserted into the queue.
            Assert.That(queue.Peek().Value, Is.EqualTo(4));
        }
Example #6
0
        public void EnqueueElement()
        {
            // Create a new priority queue.
            ConcurrentPriorityQueue <int> queue = new ConcurrentPriorityQueue <int>();

            // Ensure that the queue is empty.
            Assert.That(queue.Count, Is.EqualTo(0));

            // Store an element and insert it into the queue.
            PriorityValuePair <int> elem = new PriorityValuePair <int>(1.0, 2);

            queue.Enqueue(elem);

            // Ensure that the element was inserted into the queue.
            Assert.That(queue.Peek(), Is.EqualTo(elem));

            // Store another element with higher priority and insert it as well.
            elem = new PriorityValuePair <int>(2.0, 4);
            queue.Enqueue(elem);

            // Ensure that the element was inserted into the queue and is at the front.
            Assert.That(queue.Peek(), Is.EqualTo(elem));
        }
Example #7
0
            public string Allocate(int startTick, int duration)
            {
                if (startTick < lastStartTick)
                {
                    throw new InvalidOperationException("startTick must not be less than last called value.");
                }
                while (UsedIdentifiers.Count > 0 && UsedIdentifiers.Peek().Item1 < startTick)
                {
                    IdentifierStack.Push(UsedIdentifiers.Dequeue().Item2);
                }
                string s       = IdentifierStack.Pop();
                int    endTick = startTick + duration;

                UsedIdentifiers.Enqueue(Tuple.Create(endTick, s), -endTick);
                lastStartTick = startTick;
                return(s);
            }