SchedulerQueue.cs 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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. using System;
  5. namespace System.Reactive.Concurrency
  6. {
  7. /// <summary>
  8. /// Efficient scheduler queue that maintains scheduled items sorted by absolute time.
  9. /// </summary>
  10. /// <typeparam name="TAbsolute">Absolute time representation type.</typeparam>
  11. /// <remarks>This type is not thread safe; users should ensure proper synchronization.</remarks>
  12. [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Naming", "CA1711:IdentifiersShouldNotHaveIncorrectSuffix", Justification = "But it *is* a queue!")]
  13. public class SchedulerQueue<TAbsolute>
  14. where TAbsolute : IComparable<TAbsolute>
  15. {
  16. private readonly PriorityQueue<ScheduledItem<TAbsolute>> _queue;
  17. /// <summary>
  18. /// Creates a new scheduler queue with a default initial capacity.
  19. /// </summary>
  20. public SchedulerQueue()
  21. : this(1024)
  22. {
  23. }
  24. /// <summary>
  25. /// Creats a new scheduler queue with the specified initial capacity.
  26. /// </summary>
  27. /// <param name="capacity">Initial capacity of the scheduler queue.</param>
  28. /// <exception cref="ArgumentOutOfRangeException"><paramref name="capacity"/> is less than zero.</exception>
  29. public SchedulerQueue(int capacity)
  30. {
  31. if (capacity < 0)
  32. throw new ArgumentOutOfRangeException("capacity");
  33. _queue = new PriorityQueue<ScheduledItem<TAbsolute>>(capacity);
  34. }
  35. /// <summary>
  36. /// Gets the number of scheduled items in the scheduler queue.
  37. /// </summary>
  38. public int Count
  39. {
  40. get
  41. {
  42. return _queue.Count;
  43. }
  44. }
  45. /// <summary>
  46. /// Enqueues the specified work item to be scheduled.
  47. /// </summary>
  48. /// <param name="scheduledItem">Work item to be scheduled.</param>
  49. public void Enqueue(ScheduledItem<TAbsolute> scheduledItem)
  50. {
  51. _queue.Enqueue(scheduledItem);
  52. }
  53. /// <summary>
  54. /// Removes the specified work item from the scheduler queue.
  55. /// </summary>
  56. /// <param name="scheduledItem">Work item to be removed from the scheduler queue.</param>
  57. /// <returns>true if the item was found; false otherwise.</returns>
  58. public bool Remove(ScheduledItem<TAbsolute> scheduledItem)
  59. {
  60. return _queue.Remove(scheduledItem);
  61. }
  62. /// <summary>
  63. /// Dequeues the next work item from the scheduler queue.
  64. /// </summary>
  65. /// <returns>Next work item in the scheduler queue (removed).</returns>
  66. public ScheduledItem<TAbsolute> Dequeue()
  67. {
  68. return _queue.Dequeue();
  69. }
  70. /// <summary>
  71. /// Peeks the next work item in the scheduler queue.
  72. /// </summary>
  73. /// <returns>Next work item in the scheduler queue (not removed).</returns>
  74. public ScheduledItem<TAbsolute> Peek()
  75. {
  76. return _queue.Peek();
  77. }
  78. }
  79. }