Skip to content

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