RefCountAsyncDisposable.cs 2.9 KB

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