RefCountAsyncDisposable.cs 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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 RefCountAsyncDisposable : IAsyncDisposable
  9. {
  10. private readonly AsyncGate _gate = new();
  11. private IAsyncDisposable _disposable;
  12. private bool _primaryDisposed;
  13. private int _count;
  14. public RefCountAsyncDisposable(IAsyncDisposable disposable)
  15. {
  16. _disposable = disposable ?? throw new ArgumentNullException(nameof(disposable));
  17. _primaryDisposed = false;
  18. _count = 0;
  19. }
  20. public async ValueTask<IAsyncDisposable> GetDisposableAsync()
  21. {
  22. using (await _gate.LockAsync().ConfigureAwait(false))
  23. {
  24. if (_disposable == null)
  25. {
  26. return AsyncDisposable.Nop;
  27. }
  28. else
  29. {
  30. _count++;
  31. return new Inner(this);
  32. }
  33. }
  34. }
  35. public async ValueTask DisposeAsync()
  36. {
  37. var disposable = default(IAsyncDisposable);
  38. using (await _gate.LockAsync().ConfigureAwait(false))
  39. {
  40. if (_disposable != null && !_primaryDisposed)
  41. {
  42. _primaryDisposed = true;
  43. if (_count == 0)
  44. {
  45. disposable = _disposable;
  46. _disposable = null;
  47. }
  48. }
  49. }
  50. if (disposable != null)
  51. {
  52. await disposable.DisposeAsync().ConfigureAwait(false);
  53. }
  54. }
  55. private async ValueTask ReleaseAsync()
  56. {
  57. var disposable = default(IAsyncDisposable);
  58. using (await _gate.LockAsync().ConfigureAwait(false))
  59. {
  60. if (_disposable != null)
  61. {
  62. _count--;
  63. if (_primaryDisposed && _count == 0)
  64. {
  65. disposable = _disposable;
  66. _disposable = null;
  67. }
  68. }
  69. }
  70. if (disposable != null)
  71. {
  72. await disposable.DisposeAsync().ConfigureAwait(false);
  73. }
  74. }
  75. private sealed class Inner : IAsyncDisposable
  76. {
  77. private RefCountAsyncDisposable _parent;
  78. public Inner(RefCountAsyncDisposable parent)
  79. {
  80. _parent = parent;
  81. }
  82. public ValueTask DisposeAsync() => Interlocked.Exchange(ref _parent, null)?.ReleaseAsync() ?? default;
  83. }
  84. }
  85. }