Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

4.x: Inline disposability into ScheduledItem #561

Merged
merged 4 commits into from
Jun 6, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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