// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT License.
// See the LICENSE file in the project root for more information.
using System.IO;
using System.Threading;
namespace System.Reactive
{
///
/// Utility methods to handle lock-free combining of Exceptions
/// as well as hosting a terminal-exception indicator for
/// lock-free termination support.
///
internal static class ExceptionHelper
{
///
/// The singleton instance of the exception indicating a terminal state,
/// DO NOT LEAK or signal this via OnError!
///
public static Exception Terminated { get; } = new EndOfStreamException("On[Error|Completed]");
///
/// Tries to atomically set the Exception on the given field if it is
/// still null.
///
/// The target field to try to set atomically.
/// The exception to set, not null (not verified).
/// True if the operation succeeded, false if the target was not null.
public static bool TrySetException(ref Exception? field, Exception ex)
{
return Interlocked.CompareExchange(ref field, ex, null) == null;
}
}
}