SerialAsyncDisposable.cs 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. // Licensed to the .NET Foundation under one or more agreements.
  2. // The .NET Foundation licenses this file to you under the MIT License.
  3. // See the LICENSE file in the project root for more information.
  4. using System.Threading;
  5. using System.Threading.Tasks;
  6. namespace System.Reactive.Disposables
  7. {
  8. public sealed class SerialAsyncDisposable : IAsyncDisposable
  9. {
  10. private readonly AsyncGate _gate = new();
  11. private IAsyncDisposable _disposable;
  12. private bool _disposed;
  13. public async ValueTask AssignAsync(IAsyncDisposable disposable)
  14. {
  15. if (disposable == null)
  16. throw new ArgumentNullException(nameof(disposable));
  17. var shouldDispose = false;
  18. var old = default(IAsyncDisposable);
  19. using (await _gate.LockAsync().ConfigureAwait(false))
  20. {
  21. if (_disposed)
  22. {
  23. shouldDispose = true;
  24. }
  25. else
  26. {
  27. old = _disposable;
  28. _disposable = disposable;
  29. }
  30. }
  31. if (old != null)
  32. {
  33. await old.DisposeAsync().ConfigureAwait(false);
  34. }
  35. if (shouldDispose && disposable != null)
  36. {
  37. await disposable.DisposeAsync().ConfigureAwait(false);
  38. }
  39. }
  40. public async ValueTask DisposeAsync()
  41. {
  42. var old = default(IAsyncDisposable);
  43. using (await _gate.LockAsync().ConfigureAwait(false))
  44. {
  45. if (!_disposed)
  46. {
  47. _disposed = true;
  48. old = _disposable;
  49. _disposable = null;
  50. }
  51. }
  52. if (old != null)
  53. {
  54. await old.DisposeAsync().ConfigureAwait(false);
  55. }
  56. }
  57. }
  58. }