1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374 |
- // Licensed to the .NET Foundation under one or more agreements.
- // The .NET Foundation licenses this file to you under the Apache 2.0 License.
- // See the LICENSE file in the project root for more information.
- namespace System.Reactive.Concurrency
- {
- /// <summary>
- /// Efficient scheduler queue that maintains scheduled items sorted by absolute time.
- /// </summary>
- /// <typeparam name="TAbsolute">Absolute time representation type.</typeparam>
- /// <remarks>This type is not thread safe; users should ensure proper synchronization.</remarks>
- [Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Naming", "CA1711:IdentifiersShouldNotHaveIncorrectSuffix", Justification = "But it *is* a queue!")]
- public class SchedulerQueue<TAbsolute>
- where TAbsolute : IComparable<TAbsolute>
- {
- private readonly PriorityQueue<ScheduledItem<TAbsolute>> _queue;
- /// <summary>
- /// Creates a new scheduler queue with a default initial capacity.
- /// </summary>
- public SchedulerQueue()
- : this(1024)
- {
- }
- /// <summary>
- /// Creates a new scheduler queue with the specified initial capacity.
- /// </summary>
- /// <param name="capacity">Initial capacity of the scheduler queue.</param>
- /// <exception cref="ArgumentOutOfRangeException"><paramref name="capacity"/> is less than zero.</exception>
- public SchedulerQueue(int capacity)
- {
- if (capacity < 0)
- {
- throw new ArgumentOutOfRangeException(nameof(capacity));
- }
- _queue = new PriorityQueue<ScheduledItem<TAbsolute>>(capacity);
- }
- /// <summary>
- /// Gets the number of scheduled items in the scheduler queue.
- /// </summary>
- public int Count => _queue.Count;
- /// <summary>
- /// Enqueues the specified work item to be scheduled.
- /// </summary>
- /// <param name="scheduledItem">Work item to be scheduled.</param>
- public void Enqueue(ScheduledItem<TAbsolute> scheduledItem)
- {
- _queue.Enqueue(scheduledItem);
- }
- /// <summary>
- /// Removes the specified work item from the scheduler queue.
- /// </summary>
- /// <param name="scheduledItem">Work item to be removed from the scheduler queue.</param>
- /// <returns><c>true</c> if the item was found; <c>false</c> otherwise.</returns>
- public bool Remove(ScheduledItem<TAbsolute> scheduledItem) => _queue.Remove(scheduledItem);
- /// <summary>
- /// Dequeues the next work item from the scheduler queue.
- /// </summary>
- /// <returns>Next work item in the scheduler queue (removed).</returns>
- public ScheduledItem<TAbsolute> Dequeue() => _queue.Dequeue();
- /// <summary>
- /// Peeks the next work item in the scheduler queue.
- /// </summary>
- /// <returns>Next work item in the scheduler queue (not removed).</returns>
- public ScheduledItem<TAbsolute> Peek() => _queue.Peek();
- }
- }
|