// Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the Apache 2.0 License. // See the LICENSE file in the project root for more information. using System.Collections.Generic; using System.Reactive.Disposables; namespace System.Reactive.Concurrency { /// /// Abstract base class for scheduled work items. /// /// Absolute time representation type. public abstract class ScheduledItem : IScheduledItem, IComparable> where TAbsolute : IComparable { private readonly SingleAssignmentDisposable _disposable = new SingleAssignmentDisposable(); private readonly IComparer _comparer; /// /// Creates a new scheduled work item to run at the specified time. /// /// Absolute time at which the work item has to be executed. /// Comparer used to compare work items based on their scheduled time. /// is null. protected ScheduledItem(TAbsolute dueTime, IComparer comparer) { if (comparer == null) throw new ArgumentNullException(nameof(comparer)); DueTime = dueTime; _comparer = comparer; } /// /// Gets the absolute time at which the item is due for invocation. /// public TAbsolute DueTime { get; } /// /// Invokes the work item. /// public void Invoke() { if (!_disposable.IsDisposed) { _disposable.Disposable = InvokeCore(); } } /// /// Implement this method to perform the work item invocation, returning a disposable object for deep cancellation. /// /// Disposable object used to cancel the work item and/or derived work items. protected abstract IDisposable InvokeCore(); #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 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 (ReferenceEquals(other, null)) { return 1; } return _comparer.Compare(DueTime, other.DueTime); } /// /// Determines whether one specified object is due before a second specified object. /// /// The first object to compare. /// The second object to compare. /// true if the value of left is earlier than the value of right; otherwise, false. /// This operator provides results consistent with the implementation. public static bool operator <(ScheduledItem left, ScheduledItem right) => Comparer>.Default.Compare(left, right) < 0; /// /// Determines whether one specified object is due before or at the same of a second specified object. /// /// The first object to compare. /// The second object to compare. /// true if the value of left is earlier than or simultaneous with the value of right; otherwise, false. /// This operator provides results consistent with the implementation. public static bool operator <=(ScheduledItem left, ScheduledItem right) => Comparer>.Default.Compare(left, right) <= 0; /// /// Determines whether one specified object is due after a second specified object. /// /// The first object to compare. /// The second object to compare. /// true if the value of left is later than the value of right; otherwise, false. /// This operator provides results consistent with the implementation. public static bool operator >(ScheduledItem left, ScheduledItem right) => Comparer>.Default.Compare(left, right) > 0; /// /// Determines whether one specified object is due after or at the same time of a second specified object. /// /// The first object to compare. /// The second object to compare. /// true if the value of left is later than or simultaneous with the value of right; otherwise, false. /// This operator provides results consistent with the implementation. public static bool operator >=(ScheduledItem left, ScheduledItem right) => Comparer>.Default.Compare(left, right) >= 0; #endregion #region Equality /// /// Determines whether two specified objects are equal. /// /// The first object to compare. /// The second object to compare. /// true if both 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) => ReferenceEquals(left, right); /// /// Determines whether two specified objects are inequal. /// /// The first object to compare. /// The second object to compare. /// true if both 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) => !(left == right); /// /// Determines whether a object is equal to the specified object. /// /// The object to compare to the current object. /// true if the obj parameter is a object and is equal to the current object; otherwise, false. public override bool Equals(object obj) => ReferenceEquals(this, obj); /// /// Returns the hash code for the current object. /// /// A 32-bit signed integer hash code. public override int GetHashCode() => base.GetHashCode(); #endregion /// /// Cancels the work item by disposing the resource returned by as soon as possible. /// public void Cancel() => _disposable.Dispose(); /// /// Gets whether the work item has received a cancellation request. /// public bool IsCanceled => _disposable.IsDisposed; } /// /// Represents a scheduled work item based on the materialization of an IScheduler.Schedule method call. /// /// Absolute time representation type. /// Type of the state passed to the scheduled action. public sealed class ScheduledItem : ScheduledItem where TAbsolute : IComparable { private readonly IScheduler _scheduler; private readonly TValue _state; private readonly Func _action; /// /// Creates a materialized work item. /// /// Recursive scheduler to invoke the scheduled action with. /// State to pass to the scheduled action. /// Scheduled action. /// Time at which to run the scheduled action. /// Comparer used to compare work items based on their scheduled time. /// or or is null. public ScheduledItem(IScheduler scheduler, TValue state, Func action, TAbsolute dueTime, IComparer comparer) : base(dueTime, comparer) { if (scheduler == null) throw new ArgumentNullException(nameof(scheduler)); if (action == null) throw new ArgumentNullException(nameof(action)); _scheduler = scheduler; _state = state; _action = action; } /// /// Creates a materialized work item. /// /// Recursive scheduler to invoke the scheduled action with. /// State to pass to the scheduled action. /// Scheduled action. /// Time at which to run the scheduled action. /// or is null. public ScheduledItem(IScheduler scheduler, TValue state, Func action, TAbsolute dueTime) : this(scheduler, state, action, dueTime, Comparer.Default) { } /// /// Invokes the scheduled action with the supplied recursive scheduler and state. /// /// Cancellation resource returned by the scheduled action. protected override IDisposable InvokeCore() => _action(_scheduler, _state); } }