SerialDisposable.cs 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. // Licensed to the .NET Foundation under one or more agreements.
  2. // The .NET Foundation licenses this file to you under the Apache 2.0 License.
  3. // See the LICENSE file in the project root for more information.
  4. namespace System.Reactive.Disposables
  5. {
  6. /// <summary>
  7. /// Represents a disposable resource whose underlying disposable resource can be replaced by another disposable resource, causing automatic disposal of the previous underlying disposable resource.
  8. /// </summary>
  9. public sealed class SerialDisposable : ICancelable
  10. {
  11. private readonly object _gate = new object();
  12. private IDisposable _current;
  13. private bool _disposed;
  14. /// <summary>
  15. /// Initializes a new instance of the <see cref="T:System.Reactive.Disposables.SerialDisposable"/> class.
  16. /// </summary>
  17. public SerialDisposable()
  18. {
  19. }
  20. /// <summary>
  21. /// Gets a value that indicates whether the object is disposed.
  22. /// </summary>
  23. public bool IsDisposed
  24. {
  25. get
  26. {
  27. lock (_gate)
  28. {
  29. return _disposed;
  30. }
  31. }
  32. }
  33. /// <summary>
  34. /// Gets or sets the underlying disposable.
  35. /// </summary>
  36. /// <remarks>If the SerialDisposable has already been disposed, assignment to this property causes immediate disposal of the given disposable object. Assigning this property disposes the previous disposable object.</remarks>
  37. public IDisposable Disposable
  38. {
  39. get
  40. {
  41. return _current;
  42. }
  43. set
  44. {
  45. var shouldDispose = false;
  46. var old = default(IDisposable);
  47. lock (_gate)
  48. {
  49. shouldDispose = _disposed;
  50. if (!shouldDispose)
  51. {
  52. old = _current;
  53. _current = value;
  54. }
  55. }
  56. if (old != null)
  57. old.Dispose();
  58. if (shouldDispose && value != null)
  59. value.Dispose();
  60. }
  61. }
  62. /// <summary>
  63. /// Disposes the underlying disposable as well as all future replacements.
  64. /// </summary>
  65. public void Dispose()
  66. {
  67. var old = default(IDisposable);
  68. lock (_gate)
  69. {
  70. if (!_disposed)
  71. {
  72. _disposed = true;
  73. old = _current;
  74. _current = null;
  75. }
  76. }
  77. if (old != null)
  78. old.Dispose();
  79. }
  80. }
  81. }