Skip to content

Commit

Permalink
4.x: Inline disposability into ScheduledItem (#561)
Browse files Browse the repository at this point in the history
  • Loading branch information
akarnokd authored and danielcweber committed Jun 6, 2018
1 parent f4f6a62 commit 8a9a254
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ public override IDisposable Schedule<TState>(TState state, TimeSpan dueTime, Fun
queue.Enqueue(si);
}

return Disposable.Create(si.Cancel);
return si;
}

private static class Trampoline
Expand Down
17 changes: 11 additions & 6 deletions Rx.NET/Source/src/System.Reactive/Concurrency/ScheduledItem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ namespace System.Reactive.Concurrency
/// Abstract base class for scheduled work items.
/// </summary>
/// <typeparam name="TAbsolute">Absolute time representation type.</typeparam>
public abstract class ScheduledItem<TAbsolute> : IScheduledItem<TAbsolute>, IComparable<ScheduledItem<TAbsolute>>
public abstract class ScheduledItem<TAbsolute> : IScheduledItem<TAbsolute>, IComparable<ScheduledItem<TAbsolute>>, IDisposable
where TAbsolute : IComparable<TAbsolute>
{
private readonly SingleAssignmentDisposable _disposable = new SingleAssignmentDisposable();
private IDisposable _disposable;
private readonly IComparer<TAbsolute> _comparer;

/// <summary>
Expand Down Expand Up @@ -42,9 +42,9 @@ protected ScheduledItem(TAbsolute dueTime, IComparer<TAbsolute> comparer)
/// </summary>
public void Invoke()
{
if (!_disposable.IsDisposed)
if (!Disposable.GetIsDisposed(ref _disposable))
{
_disposable.Disposable = InvokeCore();
Disposable.SetSingle(ref _disposable, InvokeCore());
}
}

Expand Down Expand Up @@ -149,12 +149,17 @@ public int CompareTo(ScheduledItem<TAbsolute> other)
/// <summary>
/// Cancels the work item by disposing the resource returned by <see cref="InvokeCore"/> as soon as possible.
/// </summary>
public void Cancel() => _disposable.Dispose();
public void Cancel() => Disposable.TryDispose(ref _disposable);

/// <summary>
/// Gets whether the work item has received a cancellation request.
/// </summary>
public bool IsCanceled => _disposable.IsDisposed;
public bool IsCanceled => Disposable.GetIsDisposed(ref _disposable);

void IDisposable.Dispose()
{
Cancel();
}
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -348,7 +348,7 @@ namespace System.Reactive.Concurrency
public System.IDisposable SchedulePeriodic<TState>(TState state, System.TimeSpan period, System.Func<TState, TState> action) { }
public override System.Reactive.Concurrency.IStopwatch StartStopwatch() { }
}
public abstract class ScheduledItem<TAbsolute> : System.IComparable<System.Reactive.Concurrency.ScheduledItem<TAbsolute>>, System.Reactive.Concurrency.IScheduledItem<TAbsolute>
public abstract class ScheduledItem<TAbsolute> : System.IComparable<System.Reactive.Concurrency.ScheduledItem<TAbsolute>>, System.IDisposable, System.Reactive.Concurrency.IScheduledItem<TAbsolute>
where TAbsolute : System.IComparable<>
{
protected ScheduledItem(TAbsolute dueTime, System.Collections.Generic.IComparer<TAbsolute> comparer) { }
Expand Down

0 comments on commit 8a9a254

Please sign in to comment.