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

Use the helper methods for IDisposable-fields, avoid repeating code patterns #556

Merged
merged 1 commit into from
May 31, 2018
Merged

Use the helper methods for IDisposable-fields, avoid repeating code patterns #556

merged 1 commit into from
May 31, 2018

Conversation

danielcweber
Copy link
Collaborator

No description provided.

@@ -28,7 +28,7 @@ public ContextDisposable(SynchronizationContext context, IDisposable disposable)
throw new ArgumentNullException(nameof(disposable));

Context = context;
_disposable = disposable;
Disposable.TrySetSingle(ref _disposable, disposable);
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is TrySetSingle required here?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is in the constructor so nothing else should be able to call ContextDisposable.Dispose() at this point.

@@ -28,7 +28,7 @@ public ScheduledDisposable(IScheduler scheduler, IDisposable disposable)
throw new ArgumentNullException(nameof(disposable));

Scheduler = scheduler;
_disposable = disposable;
Disposables.Disposable.TrySetSingle(ref _disposable, disposable);
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is TrySetSingle required here?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as above, this is in the constructor where it can't be disposed yet.

Drain();
}
}
if (Disposable.TrySetSerial(ref currentSubscription, null))
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this still capture the original semantics?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is a loop in TrySetSerial where a concurrent dispose failing the CAS will have it repeat the loop, detect that it needs to dispose d and then quits. This logic relies on the fact that if the single CAS fails, it can be only due to cancellation and there is nothing to do further.

@danielcweber
Copy link
Collaborator Author

@akarnokd I added some notes for further review, thx.

@@ -131,7 +129,8 @@ void Drain()
else
{
var sad = new SingleAssignmentDisposable();
if (Interlocked.CompareExchange(ref currentSubscription, sad, null) == null)

if (Disposable.TrySetSingle(ref currentSubscription, sad) == TrySetSingleResult.Success)
Copy link
Collaborator Author

@danielcweber danielcweber May 31, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@akarnokd This change was added (the original reason to split SetSingle and TrySetSingle).

@danielcweber danielcweber merged commit 389881c into dotnet:master May 31, 2018
@danielcweber danielcweber deleted the UseDisposableHelperMethods branch May 31, 2018 09:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants