Skip to content

Commit f50b360

Browse files
chore: Leverage new Lock types (#4983)
1 parent 9aba2d4 commit f50b360

File tree

11 files changed

+18
-15
lines changed

11 files changed

+18
-15
lines changed

GlobalUsings.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
// These are used in all projects except samples.
22

3+
// Use the new System.Threading.Lock type on .NET 9+, which enables compiler optimisations.
4+
// On earlier targets, alias it to object so the same lock pattern works everywhere,
5+
// but don't use the variant of "Polyfill", as it may cause issues on Runtimes that support "Thread.Abort" (e.g. .NET Framework)
6+
#if !NET9_0_OR_GREATER
7+
global using Lock = object;
8+
#endif
9+
310
global using System.Buffers;
411
global using System.Collections;
512
global using System.Collections.Concurrent;

src/Sentry.Log4Net/SentryAppender.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ public class SentryAppender : AppenderSkeleton
1010
private readonly Func<string, IDisposable> _initAction;
1111
private volatile IDisposable? _sdkHandle;
1212

13-
private readonly object _initSync = new();
13+
private readonly Lock _initSync = new();
1414

1515
internal static readonly SdkVersion NameAndVersion
1616
= typeof(SentryAppender).Assembly.GetNameAndVersion();

src/Sentry.Maui/Internal/MauiDeviceData.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,8 @@ namespace Sentry.Maui.Internal;
66

77
internal static class MauiDeviceData
88
{
9-
#if NET9_0_OR_GREATER && ANDROID
9+
#if ANDROID
1010
private static readonly Lock JniLock = new();
11-
#elif ANDROID
12-
private static readonly object JniLock = new();
1311
#endif
1412

1513
public static void ApplyMauiDeviceData(this Device device, IDiagnosticLogger? logger,

src/Sentry.Maui/Internal/ScreenshotAttachment.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,8 @@ internal class ScreenshotAttachmentContent : IAttachmentContent
2727
{
2828
private readonly SentryMauiOptions _options;
2929

30-
#if NET9_0_OR_GREATER && ANDROID
30+
#if ANDROID
3131
private static readonly Lock JniLock = new();
32-
#elif ANDROID
33-
private static readonly object JniLock = new();
3432
#endif
3533

3634
public ScreenshotAttachmentContent(SentryMauiOptions options)

src/Sentry/Internal/Hub.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ namespace Sentry.Internal;
88

99
internal class Hub : IHub, IDisposable
1010
{
11-
private readonly object _sessionPauseLock = new();
11+
private readonly Lock _sessionPauseLock = new();
1212

1313
private readonly ISystemClock _clock;
1414
private readonly ISessionManager _sessionManager;

src/Sentry/Internal/InstallationIdHelper.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ namespace Sentry.Internal;
55

66
internal class InstallationIdHelper(SentryOptions options)
77
{
8-
private readonly object _installationIdLock = new();
8+
private readonly Lock _installationIdLock = new();
99
private string? _installationId;
1010

1111
public string? TryGetInstallationId()

src/Sentry/Internal/Signal.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ namespace Sentry.Internal;
22

33
internal class Signal : IDisposable
44
{
5-
private readonly object _lock = new();
5+
private readonly Lock _lock = new();
66
private readonly SemaphoreSlim _semaphore = new(0, 1);
77

88
public Signal(bool isReleasedInitially = false)

src/Sentry/Scope.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public class Scope : IEventLike
2222

2323
internal bool Locked { get; set; }
2424

25-
private readonly object _lastEventIdSync = new();
25+
private readonly Lock _lastEventIdSync = new();
2626
private SentryId _lastEventId;
2727

2828
internal SentryId LastEventId
@@ -43,7 +43,7 @@ internal SentryId LastEventId
4343
}
4444
}
4545

46-
private readonly object _evaluationSync = new();
46+
private readonly Lock _evaluationSync = new();
4747
private volatile bool _hasEvaluated;
4848

4949
/// <summary>

src/Sentry/SentryMessageHandler.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ public abstract class SentryMessageHandler : DelegatingHandler
1010
{
1111
private readonly IHub _hub;
1212
private readonly SentryOptions? _options;
13-
private readonly object _innerHandlerLock = new();
13+
private readonly Lock _innerHandlerLock = new();
1414

1515
/// <summary>
1616
/// Constructs an instance of <see cref="SentryMessageHandler"/>.

src/Sentry/TransactionTracer.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -316,7 +316,7 @@ private void AddChildSpan(SpanTracer span)
316316

317317
private class LastActiveSpanTracker
318318
{
319-
private readonly object _lock = new object();
319+
private readonly Lock _lock = new();
320320

321321
private readonly Lazy<Stack<ISpan>> _trackedSpans = new();
322322
private Stack<ISpan> TrackedSpans => _trackedSpans.Value;

0 commit comments

Comments
 (0)