SchedulerQueue.cs 3.1 KB

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