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: Improve and fix CompositeDisposable #505

Merged

Conversation

akarnokd
Copy link
Collaborator

This PR cleans up the internal logic of CompositeDisposable by avoiding some allocations and making sure publicly visible state is available in a thread-safe manner.

The main safety offender was the IsDisposed reading the _disposed plain field without a barrier. Assuming the .NET memory modell is compatible with the C++ 11/Java memory model, such reads could be cached and the reader may never see _disposed turn true:

var composite = new CompositeDisposable();

handOff(composite);

while (!composite.IsDisposed) Thread.SpinWait();

// resume

without a barrier, this could end up like this:

var isDisposed = composite.IsDisposed;
while (!isDisposed) Thread.SpinWait();

and never complete.

@clairernovotny clairernovotny merged commit 34f7bb1 into dotnet:master May 26, 2018
@akarnokd akarnokd deleted the CompositeDisposableFixAndEnhancements branch May 26, 2018 14:54
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