RefCountAsyncDisposable.cs 2.9 KB

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