Skip to content

Commit 919329e

Browse files
committed
Changed all cases where int (or in one case long) was used instead of bool because of the lack of support in Interlocked.Exchange/CompareExchange to use the now-supported type, but with backwards-compatible support for .NET pre-9.0.
1 parent c8da336 commit 919329e

File tree

7 files changed

+88
-25
lines changed

7 files changed

+88
-25
lines changed

src/Sentry.EntityFramework/SentryDatabaseLogging.cs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,24 @@ namespace Sentry.EntityFramework;
55
/// </summary>
66
internal static class SentryDatabaseLogging
77
{
8-
private static int Init;
8+
#if NET9_0_OR_GREATER
9+
private static bool _init;
10+
11+
const bool TRUE = true;
12+
const bool FALSE = false;
13+
#else
14+
private static int _init;
15+
16+
const int TRUE = 1;
17+
const int FALSE = 0;
18+
#endif
919

1020
internal static SentryCommandInterceptor? UseBreadcrumbs(
1121
IQueryLogger? queryLogger = null,
1222
bool initOnce = true,
1323
IDiagnosticLogger? diagnosticLogger = null)
1424
{
15-
if (initOnce && Interlocked.Exchange(ref Init, 1) != 0)
25+
if (initOnce && Interlocked.Exchange(ref _init, TRUE) != FALSE)
1626
{
1727
diagnosticLogger?.LogWarning("{0}.{1} was already executed.",
1828
nameof(SentryDatabaseLogging), nameof(UseBreadcrumbs));

src/Sentry.Profiling/SampleProfilerSession.cs

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -56,18 +56,27 @@ private SampleProfilerSession(SentryStopwatch stopwatch, EventPipeSession sessio
5656

5757
public TraceLog TraceLog => EventSource.TraceLog;
5858

59-
// default is false, set 1 for true.
60-
private static int _throwOnNextStartupForTests = 0;
59+
#if NET9_0_OR_GREATER
60+
private static bool _throwOnNextStartupForTests = FALSE;
61+
62+
const bool TRUE = true;
63+
const bool FALSE = false;
64+
#else
65+
private static int _throwOnNextStartupForTests = FALSE;
66+
67+
const int TRUE = 1;
68+
const int FALSE = 0;
69+
#endif
6170

6271
internal static bool ThrowOnNextStartupForTests
6372
{
64-
get { return Interlocked.CompareExchange(ref _throwOnNextStartupForTests, 1, 1) == 1; }
73+
get { return _throwOnNextStartupForTests != FALSE; }
6574
set
6675
{
6776
if (value)
68-
Interlocked.CompareExchange(ref _throwOnNextStartupForTests, 1, 0);
77+
Interlocked.CompareExchange(ref _throwOnNextStartupForTests, TRUE, FALSE);
6978
else
70-
Interlocked.CompareExchange(ref _throwOnNextStartupForTests, 0, 1);
79+
Interlocked.CompareExchange(ref _throwOnNextStartupForTests, FALSE, TRUE);
7180
}
7281
}
7382

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

80-
if (Interlocked.CompareExchange(ref _throwOnNextStartupForTests, 0, 1) == 1)
89+
if (Interlocked.CompareExchange(ref _throwOnNextStartupForTests, FALSE, TRUE) == TRUE)
8190
{
8291
throw new Exception("Test exception");
8392
}

src/Sentry.Profiling/SamplingTransactionProfilerFactory.cs

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,21 @@ namespace Sentry.Profiling;
66
internal class SamplingTransactionProfilerFactory : IDisposable, ITransactionProfilerFactory
77
{
88
// We only allow a single profile so let's keep track of the current status.
9+
#if NET9_0_OR_GREATER
10+
internal bool _inProgress = FALSE;
11+
12+
const bool TRUE = true;
13+
const bool FALSE = false;
14+
#else
915
internal int _inProgress = FALSE;
1016

17+
const int TRUE = 1;
18+
const int FALSE = 0;
19+
#endif
20+
1121
// Whether the session startup took longer than the given timeout.
1222
internal bool StartupTimedOut { get; }
1323

14-
private const int TRUE = 1;
15-
private const int FALSE = 0;
16-
1724
// Stop profiling after the given number of milliseconds.
1825
private const int TIME_LIMIT_MS = 30_000;
1926

src/Sentry/Ben.BlockingDetector/DetectBlockingSynchronizationContext.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ internal sealed class DetectBlockingSynchronizationContext : SynchronizationCont
1010

1111
internal int _isSuppressed;
1212

13-
internal void Suppress() => Interlocked.Exchange(ref _isSuppressed, _isSuppressed + 1);
14-
internal void Restore() => Interlocked.Exchange(ref _isSuppressed, _isSuppressed - 1);
13+
internal void Suppress() => Interlocked.Increment(ref _isSuppressed);
14+
internal void Restore() => Interlocked.Decrement(ref _isSuppressed);
1515

1616
public DetectBlockingSynchronizationContext(IBlockingMonitor monitor)
1717
{

src/Sentry/Internal/Hub.cs

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,30 @@ internal class Hub : IHub, IDisposable
2424
private readonly MemoryMonitor? _memoryMonitor;
2525
#endif
2626

27+
#if NET9_0_OR_GREATER
28+
private bool _isPersistedSessionRecovered;
29+
30+
const bool TRUE = true;
31+
const bool FALSE = false;
32+
#else
2733
private int _isPersistedSessionRecovered;
2834

35+
const int TRUE = 1;
36+
const int FALSE = 0;
37+
#endif
38+
2939
// Internal for testability
3040
internal ConditionalWeakTable<Exception, ISpan> ExceptionToSpanMap { get; } = new();
3141

3242
internal IInternalScopeManager ScopeManager { get; }
3343

34-
private int _isEnabled = 1;
35-
public bool IsEnabled => _isEnabled == 1;
44+
#if NET9_0_OR_GREATER
45+
private bool _isEnabled = TRUE;
46+
#else
47+
private int _isEnabled = TRUE;
48+
#endif
49+
50+
public bool IsEnabled => _isEnabled != FALSE;
3651

3752
internal SentryOptions Options => _options;
3853

@@ -356,7 +371,7 @@ public TransactionContext ContinueTrace(
356371
public void StartSession()
357372
{
358373
// Attempt to recover persisted session left over from previous run
359-
if (Interlocked.Exchange(ref _isPersistedSessionRecovered, 1) != 1)
374+
if (Interlocked.Exchange(ref _isPersistedSessionRecovered, TRUE) != TRUE)
360375
{
361376
try
362377
{
@@ -835,7 +850,7 @@ public void Dispose()
835850
{
836851
_options.LogInfo("Disposing the Hub.");
837852

838-
if (Interlocked.Exchange(ref _isEnabled, 0) != 1)
853+
if (Interlocked.Exchange(ref _isEnabled, FALSE) != TRUE)
839854
{
840855
return;
841856
}

src/Sentry/Threading/ScopedCountdownLock.cs

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,23 @@ namespace Sentry.Threading;
1313
internal sealed class ScopedCountdownLock : IDisposable
1414
{
1515
private readonly CountdownEvent _event;
16+
17+
#if NET9_0_OR_GREATER
18+
private volatile bool _isEngaged;
19+
20+
const bool TRUE = true;
21+
const bool FALSE = false;
22+
#else
1623
private volatile int _isEngaged;
1724

25+
const int TRUE = 1;
26+
const int FALSE = 0;
27+
#endif
28+
1829
internal ScopedCountdownLock()
1930
{
2031
_event = new CountdownEvent(1);
21-
_isEngaged = 0;
32+
_isEngaged = FALSE;
2233
}
2334

2435
/// <summary>
@@ -31,13 +42,13 @@ internal ScopedCountdownLock()
3142
/// 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.
3243
/// When <see langword="0"/> and while a <see cref="LockScope"/> is active, no more <see cref="CounterScope"/> can be entered.
3344
/// </summary>
34-
internal int Count => _isEngaged == 1 ? _event.CurrentCount : _event.CurrentCount - 1;
45+
internal int Count => _isEngaged == TRUE ? _event.CurrentCount : _event.CurrentCount - 1;
3546

3647
/// <summary>
3748
/// 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"/>.
3849
/// 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.
3950
/// </summary>
40-
internal bool IsEngaged => _isEngaged == 1;
51+
internal bool IsEngaged => _isEngaged == TRUE;
4152

4253
/// <summary>
4354
/// 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"/>.
@@ -79,7 +90,7 @@ private void ExitCounterScope()
7990
/// </remarks>
8091
internal LockScope TryEnterLockScope()
8192
{
82-
if (Interlocked.CompareExchange(ref _isEngaged, 1, 0) == 0)
93+
if (Interlocked.CompareExchange(ref _isEngaged, TRUE, FALSE) == FALSE)
8394
{
8495
Debug.Assert(_event.CurrentCount >= 1);
8596
_ = _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
@@ -94,7 +105,7 @@ private void ExitLockScope()
94105
Debug.Assert(_event.IsSet);
95106
_event.Reset(); // reset the signaled event to the initial count of 1, so that new 'CounterScope' instances can be entered again
96107

97-
if (Interlocked.CompareExchange(ref _isEngaged, 0, 1) != 1)
108+
if (Interlocked.CompareExchange(ref _isEngaged, FALSE, TRUE) != TRUE)
98109
{
99110
Debug.Fail("The Lock should have not been disengaged without being engaged first.");
100111
}

src/Sentry/TransactionTracer.cs

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,20 @@ public class TransactionTracer : IBaseTracer, ITransactionTracer
1212
private readonly IHub _hub;
1313
private readonly SentryOptions? _options;
1414
private readonly Timer? _idleTimer;
15-
private long _cancelIdleTimeout;
1615
private readonly SentryStopwatch _stopwatch = SentryStopwatch.StartNew();
1716

17+
#if NET9_0_OR_GREATER
18+
private bool _cancelIdleTimeout;
19+
20+
const bool TRUE = true;
21+
const bool FALSE = false;
22+
#else
23+
private int _cancelIdleTimeout;
24+
25+
const int TRUE = 1;
26+
const int FALSE = 0;
27+
#endif
28+
1829
private readonly Instrumenter _instrumenter = Instrumenter.Sentry;
1930

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

0 commit comments

Comments
 (0)