Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

## Unreleased

- Use new `Interlocked.Exchange`/`CompareExchange` overloads that support `bool` values ([#4585](https://github.com/getsentry/sentry-dotnet/pull/4585))
Comment thread
logiclrd marked this conversation as resolved.
Outdated

### Dependencies

- Bump Java SDK from v8.22.0 to v8.23.0 ([#4586](https://github.com/getsentry/sentry-dotnet/pull/4586))
Expand Down
14 changes: 12 additions & 2 deletions src/Sentry.EntityFramework/SentryDatabaseLogging.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,24 @@ namespace Sentry.EntityFramework;
/// </summary>
internal static class SentryDatabaseLogging
{
private static int Init;
#if NET9_0_OR_GREATER
private static bool _init;

const bool TRUE = true;
const bool FALSE = false;
#else
private static int _init;

const int TRUE = 1;
const int FALSE = 0;
#endif
Comment thread
jamescrosswell marked this conversation as resolved.
Outdated

internal static SentryCommandInterceptor? UseBreadcrumbs(
IQueryLogger? queryLogger = null,
bool initOnce = true,
IDiagnosticLogger? diagnosticLogger = null)
{
if (initOnce && Interlocked.Exchange(ref Init, 1) != 0)
if (initOnce && Interlocked.Exchange(ref _init, TRUE) != FALSE)
{
diagnosticLogger?.LogWarning("{0}.{1} was already executed.",
nameof(SentryDatabaseLogging), nameof(UseBreadcrumbs));
Expand Down
21 changes: 15 additions & 6 deletions src/Sentry.Profiling/SampleProfilerSession.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,18 +56,27 @@ private SampleProfilerSession(SentryStopwatch stopwatch, EventPipeSession sessio

public TraceLog TraceLog => EventSource.TraceLog;

// default is false, set 1 for true.
private static int _throwOnNextStartupForTests = 0;
#if NET9_0_OR_GREATER
private static bool _throwOnNextStartupForTests = FALSE;

const bool TRUE = true;
const bool FALSE = false;
#else
private static int _throwOnNextStartupForTests = FALSE;

const int TRUE = 1;
const int FALSE = 0;
#endif

internal static bool ThrowOnNextStartupForTests
{
get { return Interlocked.CompareExchange(ref _throwOnNextStartupForTests, 1, 1) == 1; }
get { return _throwOnNextStartupForTests != FALSE; }
Comment thread
cursor[bot] marked this conversation as resolved.
Outdated
set
{
if (value)
Interlocked.CompareExchange(ref _throwOnNextStartupForTests, 1, 0);
Interlocked.CompareExchange(ref _throwOnNextStartupForTests, TRUE, FALSE);
else
Interlocked.CompareExchange(ref _throwOnNextStartupForTests, 0, 1);
Interlocked.CompareExchange(ref _throwOnNextStartupForTests, FALSE, TRUE);
}
}

Expand All @@ -77,7 +86,7 @@ public static SampleProfilerSession StartNew(IDiagnosticLogger? logger = null)
{
var client = new DiagnosticsClient(Environment.ProcessId);

if (Interlocked.CompareExchange(ref _throwOnNextStartupForTests, 0, 1) == 1)
if (Interlocked.CompareExchange(ref _throwOnNextStartupForTests, FALSE, TRUE) == TRUE)
{
throw new Exception("Test exception");
}
Expand Down
13 changes: 10 additions & 3 deletions src/Sentry.Profiling/SamplingTransactionProfilerFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,21 @@ namespace Sentry.Profiling;
internal class SamplingTransactionProfilerFactory : IDisposable, ITransactionProfilerFactory
{
// We only allow a single profile so let's keep track of the current status.
#if NET9_0_OR_GREATER
internal bool _inProgress = FALSE;

const bool TRUE = true;
const bool FALSE = false;
#else
internal int _inProgress = FALSE;

const int TRUE = 1;
const int FALSE = 0;
#endif
Comment thread
logiclrd marked this conversation as resolved.
Outdated

// Whether the session startup took longer than the given timeout.
internal bool StartupTimedOut { get; }

private const int TRUE = 1;
private const int FALSE = 0;

// Stop profiling after the given number of milliseconds.
private const int TIME_LIMIT_MS = 30_000;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ internal sealed class DetectBlockingSynchronizationContext : SynchronizationCont

internal int _isSuppressed;

internal void Suppress() => Interlocked.Exchange(ref _isSuppressed, _isSuppressed + 1);
internal void Restore() => Interlocked.Exchange(ref _isSuppressed, _isSuppressed - 1);
internal void Suppress() => Interlocked.Increment(ref _isSuppressed);
Comment thread
logiclrd marked this conversation as resolved.
internal void Restore() => Interlocked.Decrement(ref _isSuppressed);

public DetectBlockingSynchronizationContext(IBlockingMonitor monitor)
{
Expand Down
23 changes: 19 additions & 4 deletions src/Sentry/Internal/Hub.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,30 @@ internal class Hub : IHub, IDisposable
private readonly MemoryMonitor? _memoryMonitor;
#endif

#if NET9_0_OR_GREATER
private bool _isPersistedSessionRecovered;

const bool TRUE = true;
const bool FALSE = false;
#else
private int _isPersistedSessionRecovered;

const int TRUE = 1;
const int FALSE = 0;
#endif

// Internal for testability
internal ConditionalWeakTable<Exception, ISpan> ExceptionToSpanMap { get; } = new();

internal IInternalScopeManager ScopeManager { get; }

private int _isEnabled = 1;
public bool IsEnabled => _isEnabled == 1;
#if NET9_0_OR_GREATER
private bool _isEnabled = TRUE;
#else
private int _isEnabled = TRUE;
#endif

public bool IsEnabled => _isEnabled != FALSE;

internal SentryOptions Options => _options;

Expand Down Expand Up @@ -356,7 +371,7 @@ public TransactionContext ContinueTrace(
public void StartSession()
{
// Attempt to recover persisted session left over from previous run
if (Interlocked.Exchange(ref _isPersistedSessionRecovered, 1) != 1)
if (Interlocked.Exchange(ref _isPersistedSessionRecovered, TRUE) != TRUE)
{
try
{
Expand Down Expand Up @@ -835,7 +850,7 @@ public void Dispose()
{
_options.LogInfo("Disposing the Hub.");

if (Interlocked.Exchange(ref _isEnabled, 0) != 1)
if (Interlocked.Exchange(ref _isEnabled, FALSE) != TRUE)
{
return;
}
Expand Down
21 changes: 16 additions & 5 deletions src/Sentry/Threading/ScopedCountdownLock.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,23 @@ namespace Sentry.Threading;
internal sealed class ScopedCountdownLock : IDisposable
{
private readonly CountdownEvent _event;

#if NET9_0_OR_GREATER
private volatile bool _isEngaged;

const bool TRUE = true;
const bool FALSE = false;
#else
private volatile int _isEngaged;

const int TRUE = 1;
const int FALSE = 0;
#endif

internal ScopedCountdownLock()
{
_event = new CountdownEvent(1);
_isEngaged = 0;
_isEngaged = FALSE;
}

/// <summary>
Expand All @@ -31,13 +42,13 @@ internal ScopedCountdownLock()
/// Gets the number of remaining <see cref="CounterScope"/> required to exit in order to set/signal the event while a <see cref="LockScope"/> is active.
/// When <see langword="0"/> and while a <see cref="LockScope"/> is active, no more <see cref="CounterScope"/> can be entered.
/// </summary>
internal int Count => _isEngaged == 1 ? _event.CurrentCount : _event.CurrentCount - 1;
internal int Count => _isEngaged == TRUE ? _event.CurrentCount : _event.CurrentCount - 1;

/// <summary>
/// Returns <see langword="true"/> when a <see cref="LockScope"/> is active and the event can be set/signaled by <see cref="Count"/> reaching <see langword="0"/>.
/// Returns <see langword="false"/> when the <see cref="Count"/> can only reach the initial count of <see langword="1"/> when no <see cref="CounterScope"/> is active any longer.
/// </summary>
internal bool IsEngaged => _isEngaged == 1;
internal bool IsEngaged => _isEngaged == TRUE;

/// <summary>
/// No <see cref="CounterScope"/> will be entered when the <see cref="Count"/> has reached <see langword="0"/>, or while the lock is engaged via an active <see cref="LockScope"/>.
Expand Down Expand Up @@ -79,7 +90,7 @@ private void ExitCounterScope()
/// </remarks>
internal LockScope TryEnterLockScope()
{
if (Interlocked.CompareExchange(ref _isEngaged, 1, 0) == 0)
if (Interlocked.CompareExchange(ref _isEngaged, TRUE, FALSE) == FALSE)
{
Debug.Assert(_event.CurrentCount >= 1);
_ = _event.Signal(); // decrement the initial count of 1, so that the event can be set with the count reaching 0 when all entered 'CounterScope' instances have exited
Expand All @@ -94,7 +105,7 @@ private void ExitLockScope()
Debug.Assert(_event.IsSet);
_event.Reset(); // reset the signaled event to the initial count of 1, so that new 'CounterScope' instances can be entered again

if (Interlocked.CompareExchange(ref _isEngaged, 0, 1) != 1)
if (Interlocked.CompareExchange(ref _isEngaged, FALSE, TRUE) != TRUE)
{
Debug.Fail("The Lock should have not been disengaged without being engaged first.");
}
Expand Down
17 changes: 14 additions & 3 deletions src/Sentry/TransactionTracer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,20 @@ public class TransactionTracer : IBaseTracer, ITransactionTracer
private readonly IHub _hub;
private readonly SentryOptions? _options;
private readonly Timer? _idleTimer;
private long _cancelIdleTimeout;
private readonly SentryStopwatch _stopwatch = SentryStopwatch.StartNew();

#if NET9_0_OR_GREATER
private bool _cancelIdleTimeout;

const bool TRUE = true;
const bool FALSE = false;
#else
private int _cancelIdleTimeout;

const int TRUE = 1;
const int FALSE = 0;
#endif

private readonly Instrumenter _instrumenter = Instrumenter.Sentry;

bool IBaseTracer.IsOtelInstrumenter => _instrumenter == Instrumenter.OpenTelemetry;
Expand Down Expand Up @@ -247,7 +258,7 @@ internal TransactionTracer(IHub hub, ITransactionContext context, TimeSpan? idle
// Set idle timer only if an idle timeout has been provided directly
if (idleTimeout.HasValue)
{
_cancelIdleTimeout = 1; // Timer will be cancelled once, atomically setting this back to 0
_cancelIdleTimeout = TRUE; // Timer will be cancelled once, atomically setting this back to false
_idleTimer = new Timer(state =>
{
if (state is not TransactionTracer transactionTracer)
Expand Down Expand Up @@ -362,7 +373,7 @@ public void Clear()
public void Finish()
{
_options?.LogDebug("Attempting to finish Transaction {0}.", SpanId);
if (Interlocked.Exchange(ref _cancelIdleTimeout, 0) == 1)
if (Interlocked.Exchange(ref _cancelIdleTimeout, FALSE) == TRUE)
{
_options?.LogDebug("Disposing of idle timer for Transaction {0}.", SpanId);
_idleTimer?.Change(Timeout.InfiniteTimeSpan, Timeout.InfiniteTimeSpan);
Expand Down
Loading