SingleAssignmentDisposableValue.cs 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  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. namespace System.Reactive.Disposables
  6. {
  7. /// <summary>
  8. /// Represents a disposable resource which only allows a single assignment of its underlying disposable resource.
  9. /// If an underlying disposable resource has already been set, future attempts to set the underlying disposable resource will throw an <see cref="InvalidOperationException"/>.
  10. /// </summary>
  11. internal struct SingleAssignmentDisposableValue
  12. {
  13. private IDisposable? _current;
  14. /// <summary>
  15. /// Gets a value that indicates whether the object is disposed.
  16. /// </summary>
  17. public bool IsDisposed =>
  18. // We use a sentinel value to indicate we've been disposed. This sentinel never leaks
  19. // to the outside world (see the Disposable property getter), so no-one can ever assign
  20. // this value to us manually.
  21. Volatile.Read(ref _current) == BooleanDisposable.True;
  22. /// <summary>
  23. /// Gets or sets the underlying disposable. After disposal, the result of getting this property is undefined.
  24. /// </summary>
  25. /// <exception cref="InvalidOperationException">Thrown if the <see cref="SingleAssignmentDisposable"/> has already been assigned to.</exception>
  26. public IDisposable? Disposable
  27. {
  28. get => Disposables.Disposable.GetValueOrDefault(ref _current);
  29. set
  30. {
  31. var result = Disposables.Disposable.TrySetSingle(ref _current, value);
  32. if (result == TrySetSingleResult.AlreadyAssigned)
  33. {
  34. throw new InvalidOperationException(Strings_Core.DISPOSABLE_ALREADY_ASSIGNED);
  35. }
  36. }
  37. }
  38. /// <summary>
  39. /// Disposes the underlying disposable.
  40. /// </summary>
  41. public void Dispose()
  42. {
  43. Disposables.Disposable.Dispose(ref _current);
  44. }
  45. }
  46. }