// this code is borrowed from RxOfficial(rx.codeplex.com) and modified
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.txt in the project root for license information.
using System;
using System.Collections.Generic;
namespace UniRx.InternalUtil
{
///
/// Abstract base class for scheduled work items.
///
internal class ScheduledItem : IComparable
{
private readonly BooleanDisposable _disposable = new BooleanDisposable();
private readonly TimeSpan _dueTime;
private readonly Action _action;
///
/// Creates a new scheduled work item to run at the specified time.
///
/// Absolute time at which the work item has to be executed.
public ScheduledItem(Action action, TimeSpan dueTime)
{
_dueTime = dueTime;
_action = action;
}
///
/// Gets the absolute time at which the item is due for invocation.
///
public TimeSpan DueTime
{
get { return _dueTime; }
}
///
/// Invokes the work item.
///
public void Invoke()
{
if (!_disposable.IsDisposed)
{
_action();
}
}
#region Inequality
///
/// Compares the work item with another work item based on absolute time values.
///
/// Work item to compare the current work item to.
/// Relative ordering between this and the specified work item.
/// The inequality operators are overloaded to provide results consistent with the IComparable implementation. Equality operators implement traditional reference equality semantics.
public int CompareTo(ScheduledItem other)
{
// MSDN: By definition, any object compares greater than null, and two null references compare equal to each other.
if (object.ReferenceEquals(other, null))
return 1;
return DueTime.CompareTo(other.DueTime);
}
///
/// Determines whether one specified ScheduledItem<TAbsolute> object is due before a second specified ScheduledItem<TAbsolute> object.
///
/// The first object to compare.
/// The second object to compare.
/// true if the DueTime value of left is earlier than the DueTime value of right; otherwise, false.
/// This operator provides results consistent with the IComparable implementation.
public static bool operator <(ScheduledItem left, ScheduledItem right)
{
return left.CompareTo(right) < 0;
}
///
/// Determines whether one specified ScheduledItem<TAbsolute> object is due before or at the same of a second specified ScheduledItem<TAbsolute> object.
///
/// The first object to compare.
/// The second object to compare.
/// true if the DueTime value of left is earlier than or simultaneous with the DueTime value of right; otherwise, false.
/// This operator provides results consistent with the IComparable implementation.
public static bool operator <=(ScheduledItem left, ScheduledItem right)
{
return left.CompareTo(right) <= 0;
}
///
/// Determines whether one specified ScheduledItem<TAbsolute> object is due after a second specified ScheduledItem<TAbsolute> object.
///
/// The first object to compare.
/// The second object to compare.
/// true if the DueTime value of left is later than the DueTime value of right; otherwise, false.
/// This operator provides results consistent with the IComparable implementation.
public static bool operator >(ScheduledItem left, ScheduledItem right)
{
return left.CompareTo(right) > 0;
}
///
/// Determines whether one specified ScheduledItem<TAbsolute> object is due after or at the same time of a second specified ScheduledItem<TAbsolute> object.
///
/// The first object to compare.
/// The second object to compare.
/// true if the DueTime value of left is later than or simultaneous with the DueTime value of right; otherwise, false.
/// This operator provides results consistent with the IComparable implementation.
public static bool operator >=(ScheduledItem left, ScheduledItem right)
{
return left.CompareTo(right) >= 0;
}
#endregion
#region Equality
///
/// Determines whether two specified ScheduledItem<TAbsolute, TValue> objects are equal.
///
/// The first object to compare.
/// The second object to compare.
/// true if both ScheduledItem<TAbsolute, TValue> are equal; otherwise, false.
/// This operator does not provide results consistent with the IComparable implementation. Instead, it implements reference equality.
public static bool operator ==(ScheduledItem left, ScheduledItem right)
{
return object.ReferenceEquals(left, right);
}
///
/// Determines whether two specified ScheduledItem<TAbsolute, TValue> objects are inequal.
///
/// The first object to compare.
/// The second object to compare.
/// true if both ScheduledItem<TAbsolute, TValue> are inequal; otherwise, false.
/// This operator does not provide results consistent with the IComparable implementation. Instead, it implements reference equality.
public static bool operator !=(ScheduledItem left, ScheduledItem right)
{
return !(left == right);
}
///
/// Determines whether a ScheduledItem<TAbsolute> object is equal to the specified object.
///
/// The object to compare to the current ScheduledItem<TAbsolute> object.
/// true if the obj parameter is a ScheduledItem<TAbsolute> object and is equal to the current ScheduledItem<TAbsolute> object; otherwise, false.
public override bool Equals(object obj)
{
return object.ReferenceEquals(this, obj);
}
///
/// Returns the hash code for the current ScheduledItem<TAbsolute> object.
///
/// A 32-bit signed integer hash code.
public override int GetHashCode()
{
return base.GetHashCode();
}
#endregion
public IDisposable Cancellation
{
get
{
return _disposable;
}
}
///
/// Gets whether the work item has received a cancellation request.
///
public bool IsCanceled
{
get { return _disposable.IsDisposed; }
}
}
///
/// Efficient scheduler queue that maintains scheduled items sorted by absolute time.
///
/// This type is not thread safe; users should ensure proper synchronization.
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Naming", "CA1711:IdentifiersShouldNotHaveIncorrectSuffix", Justification = "But it *is* a queue!")]
internal class SchedulerQueue
{
private readonly PriorityQueue _queue;
///
/// Creates a new scheduler queue with a default initial capacity.
///
public SchedulerQueue()
: this(1024)
{
}
///
/// Creats a new scheduler queue with the specified initial capacity.
///
/// Initial capacity of the scheduler queue.
/// is less than zero.
public SchedulerQueue(int capacity)
{
if (capacity < 0)
throw new ArgumentOutOfRangeException("capacity");
_queue = new PriorityQueue(capacity);
}
///
/// Gets the number of scheduled items in the scheduler queue.
///
public int Count
{
get
{
return _queue.Count;
}
}
///
/// Enqueues the specified work item to be scheduled.
///
/// Work item to be scheduled.
public void Enqueue(ScheduledItem scheduledItem)
{
_queue.Enqueue(scheduledItem);
}
///
/// Removes the specified work item from the scheduler queue.
///
/// Work item to be removed from the scheduler queue.
/// true if the item was found; false otherwise.
public bool Remove(ScheduledItem scheduledItem)
{
return _queue.Remove(scheduledItem);
}
///
/// Dequeues the next work item from the scheduler queue.
///
/// Next work item in the scheduler queue (removed).
public ScheduledItem Dequeue()
{
return _queue.Dequeue();
}
///
/// Peeks the next work item in the scheduler queue.
///
/// Next work item in the scheduler queue (not removed).
public ScheduledItem Peek()
{
return _queue.Peek();
}
}
}