SchedulerQueue.cs 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. // Licensed to the .NET Foundation under one or more agreements.
  2. // The .NET Foundation licenses this file to you under the Apache 2.0 License.
  3. // See the LICENSE file in the project root for more information.
  4. namespace System.Reactive.Concurrency
  5. {
  6. /// <summary>
  7. /// Efficient scheduler queue that maintains scheduled items sorted by absolute time.
  8. /// </summary>
  9. /// <typeparam name="TAbsolute">Absolute time representation type.</typeparam>
  10. /// <remarks>This type is not thread safe; users should ensure proper synchronization.</remarks>
  11. [Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Naming", "CA1711:IdentifiersShouldNotHaveIncorrectSuffix", Justification = "But it *is* a queue!")]
  12. public class SchedulerQueue<TAbsolute>
  13. where TAbsolute : IComparable<TAbsolute>
  14. {
  15. private readonly PriorityQueue<ScheduledItem<TAbsolute>> _queue;
  16. /// <summary>
  17. /// Creates a new scheduler queue with a default initial capacity.
  18. /// </summary>
  19. public SchedulerQueue()
  20. : this(1024)
  21. {
  22. }
  23. /// <summary>
  24. /// Creates a new scheduler queue with the specified initial capacity.
  25. /// </summary>
  26. /// <param name="capacity">Initial capacity of the scheduler queue.</param>
  27. /// <exception cref="ArgumentOutOfRangeException"><paramref name="capacity"/> is less than zero.</exception>
  28. public SchedulerQueue(int capacity)
  29. {
  30. if (capacity < 0)
  31. {
  32. throw new ArgumentOutOfRangeException(nameof(capacity));
  33. }
  34. _queue = new PriorityQueue<ScheduledItem<TAbsolute>>(capacity);
  35. }
  36. /// <summary>
  37. /// Gets the number of scheduled items in the scheduler queue.
  38. /// </summary>
  39. public int Count => _queue.Count;
  40. /// <summary>
  41. /// Enqueues the specified work item to be scheduled.
  42. /// </summary>
  43. /// <param name="scheduledItem">Work item to be scheduled.</param>
  44. public void Enqueue(ScheduledItem<TAbsolute> scheduledItem)
  45. {
  46. _queue.Enqueue(scheduledItem);
  47. }
  48. /// <summary>
  49. /// Removes the specified work item from the scheduler queue.
  50. /// </summary>
  51. /// <param name="scheduledItem">Work item to be removed from the scheduler queue.</param>
  52. /// <returns><c>true</c> if the item was found; <c>false</c> otherwise.</returns>
  53. public bool Remove(ScheduledItem<TAbsolute> scheduledItem) => _queue.Remove(scheduledItem);
  54. /// <summary>
  55. /// Dequeues the next work item from the scheduler queue.
  56. /// </summary>
  57. /// <returns>Next work item in the scheduler queue (removed).</returns>
  58. public ScheduledItem<TAbsolute> Dequeue() => _queue.Dequeue();
  59. /// <summary>
  60. /// Peeks the next work item in the scheduler queue.
  61. /// </summary>
  62. /// <returns>Next work item in the scheduler queue (not removed).</returns>
  63. public ScheduledItem<TAbsolute> Peek() => _queue.Peek();
  64. }
  65. }