| 1 |
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.txt in the project root for license information. |
| 2 |
|
| 3 |
using System; |
| 4 |
|
| 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 |
|
| 18 |
/// <summary> |
| 19 |
/// Creates a new scheduler queue with a default initial capacity. |
| 20 |
/// </summary> |
| 21 |
public SchedulerQueue() |
| 22 |
: this(1024) |
| 23 |
{ |
| 24 |
} |
| 25 |
|
| 26 |
/// <summary> |
| 27 |
/// Creats a new scheduler queue with the specified initial capacity. |
| 28 |
/// </summary> |
| 29 |
/// <param name="capacity">Initial capacity of the scheduler queue.</param> |
| 30 |
/// <exception cref="ArgumentOutOfRangeException"><paramref name="capacity"/> is less than zero.</exception> |
| 31 |
public SchedulerQueue(int capacity) |
| 32 |
{ |
| 33 |
if (capacity < 0) |
| 34 |
throw new ArgumentOutOfRangeException("capacity"); |
| 35 |
|
| 36 |
_queue = new PriorityQueue<ScheduledItem<TAbsolute>>(capacity); |
| 37 |
} |
| 38 |
|
| 39 |
/// <summary> |
| 40 |
/// Gets the number of scheduled items in the scheduler queue. |
| 41 |
/// </summary> |
| 42 |
public int Count |
| 43 |
{ |
| 44 |
get |
| 45 |
{ |
| 46 |
return _queue.Count; |
| 47 |
} |
| 48 |
} |
| 49 |
|
| 50 |
/// <summary> |
| 51 |
/// Enqueues the specified work item to be scheduled. |
| 52 |
/// </summary> |
| 53 |
/// <param name="scheduledItem">Work item to be scheduled.</param> |
| 54 |
public void Enqueue(ScheduledItem<TAbsolute> scheduledItem) |
| 55 |
{ |
| 56 |
_queue.Enqueue(scheduledItem); |
| 57 |
} |
| 58 |
|
| 59 |
/// <summary> |
| 60 |
/// Removes the specified work item from the scheduler queue. |
| 61 |
/// </summary> |
| 62 |
/// <param name="scheduledItem">Work item to be removed from the scheduler queue.</param> |
| 63 |
/// <returns>true if the item was found; false otherwise.</returns> |
| 64 |
public bool Remove(ScheduledItem<TAbsolute> scheduledItem) |
| 65 |
{ |
| 66 |
return _queue.Remove(scheduledItem); |
| 67 |
} |
| 68 |
|
| 69 |
/// <summary> |
| 70 |
/// Dequeues the next work item from the scheduler queue. |
| 71 |
/// </summary> |
| 72 |
/// <returns>Next work item in the scheduler queue (removed).</returns> |
| 73 |
public ScheduledItem<TAbsolute> Dequeue() |
| 74 |
{ |
| 75 |
return _queue.Dequeue(); |
| 76 |
} |
| 77 |
|
| 78 |
/// <summary> |
| 79 |
/// Peeks the next work item in the scheduler queue. |
| 80 |
/// </summary> |
| 81 |
/// <returns>Next work item in the scheduler queue (not removed).</returns> |
| 82 |
public ScheduledItem<TAbsolute> Peek() |
| 83 |
{ |
| 84 |
return _queue.Peek(); |
| 85 |
} |
| 86 |
} |
| 87 |
} |