Skip to content

Commit 8a9a254

Browse files
akarnokddanielcweber
authored andcommitted
4.x: Inline disposability into ScheduledItem (#561)
1 parent f4f6a62 commit 8a9a254

File tree

3 files changed

+13
-8
lines changed

3 files changed

+13
-8
lines changed

Rx.NET/Source/src/System.Reactive/Concurrency/CurrentThreadScheduler.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ public override IDisposable Schedule<TState>(TState state, TimeSpan dueTime, Fun
103103
queue.Enqueue(si);
104104
}
105105

106-
return Disposable.Create(si.Cancel);
106+
return si;
107107
}
108108

109109
private static class Trampoline

Rx.NET/Source/src/System.Reactive/Concurrency/ScheduledItem.cs

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,10 @@ namespace System.Reactive.Concurrency
1111
/// Abstract base class for scheduled work items.
1212
/// </summary>
1313
/// <typeparam name="TAbsolute">Absolute time representation type.</typeparam>
14-
public abstract class ScheduledItem<TAbsolute> : IScheduledItem<TAbsolute>, IComparable<ScheduledItem<TAbsolute>>
14+
public abstract class ScheduledItem<TAbsolute> : IScheduledItem<TAbsolute>, IComparable<ScheduledItem<TAbsolute>>, IDisposable
1515
where TAbsolute : IComparable<TAbsolute>
1616
{
17-
private readonly SingleAssignmentDisposable _disposable = new SingleAssignmentDisposable();
17+
private IDisposable _disposable;
1818
private readonly IComparer<TAbsolute> _comparer;
1919

2020
/// <summary>
@@ -42,9 +42,9 @@ protected ScheduledItem(TAbsolute dueTime, IComparer<TAbsolute> comparer)
4242
/// </summary>
4343
public void Invoke()
4444
{
45-
if (!_disposable.IsDisposed)
45+
if (!Disposable.GetIsDisposed(ref _disposable))
4646
{
47-
_disposable.Disposable = InvokeCore();
47+
Disposable.SetSingle(ref _disposable, InvokeCore());
4848
}
4949
}
5050

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

154154
/// <summary>
155155
/// Gets whether the work item has received a cancellation request.
156156
/// </summary>
157-
public bool IsCanceled => _disposable.IsDisposed;
157+
public bool IsCanceled => Disposable.GetIsDisposed(ref _disposable);
158+
159+
void IDisposable.Dispose()
160+
{
161+
Cancel();
162+
}
158163
}
159164

160165
/// <summary>

Rx.NET/Source/tests/Tests.System.Reactive.ApiApprovals/Api/ApiApprovalTests.Core.approved.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -348,7 +348,7 @@ namespace System.Reactive.Concurrency
348348
public System.IDisposable SchedulePeriodic<TState>(TState state, System.TimeSpan period, System.Func<TState, TState> action) { }
349349
public override System.Reactive.Concurrency.IStopwatch StartStopwatch() { }
350350
}
351-
public abstract class ScheduledItem<TAbsolute> : System.IComparable<System.Reactive.Concurrency.ScheduledItem<TAbsolute>>, System.Reactive.Concurrency.IScheduledItem<TAbsolute>
351+
public abstract class ScheduledItem<TAbsolute> : System.IComparable<System.Reactive.Concurrency.ScheduledItem<TAbsolute>>, System.IDisposable, System.Reactive.Concurrency.IScheduledItem<TAbsolute>
352352
where TAbsolute : System.IComparable<>
353353
{
354354
protected ScheduledItem(TAbsolute dueTime, System.Collections.Generic.IComparer<TAbsolute> comparer) { }

0 commit comments

Comments
 (0)