Switch lock backport package to #if defs instead (#3217)

This commit is contained in:
enonibobble
2025-12-31 19:42:25 +00:00
committed by GitHub
parent 4e95dd7123
commit b386a0e22a
14 changed files with 60 additions and 11 deletions

View File

@@ -12,7 +12,11 @@ namespace Discord.Commands
private readonly ConcurrentDictionary<string, CommandMapNode> _nodes; private readonly ConcurrentDictionary<string, CommandMapNode> _nodes;
private readonly string _name; private readonly string _name;
#if NET9_0_OR_GREATER
private readonly Lock _lockObj = new(); private readonly Lock _lockObj = new();
#else
private readonly object _lockObj = new();
#endif
private ImmutableArray<CommandInfo> _commands; private ImmutableArray<CommandInfo> _commands;
public bool IsEmpty => _commands.Length == 0 && _nodes.Count == 0; public bool IsEmpty => _commands.Length == 0 && _nodes.Count == 0;

View File

@@ -19,9 +19,5 @@
<PackageReference Include="IDisposableAnalyzers" Version="4.0.8"> <PackageReference Include="IDisposableAnalyzers" Version="4.0.8">
<PrivateAssets>all</PrivateAssets> <PrivateAssets>all</PrivateAssets>
</PackageReference> </PackageReference>
<PackageReference Condition="!$([MSBuild]::IsTargetFrameworkCompatible('$(TargetFramework)', 'net9.0'))" Include="Backport.System.Threading.Lock" Version="3.1.5">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>analyzers</IncludeAssets>
</PackageReference>
</ItemGroup> </ItemGroup>
</Project> </Project>

View File

@@ -33,7 +33,11 @@ namespace Discord.Rest
/// </summary> /// </summary>
internal new RestCommandBaseData Data { get; private set; } internal new RestCommandBaseData Data { get; private set; }
#if NET9_0_OR_GREATER
private readonly Lock _lock = new(); private readonly Lock _lock = new();
#else
private readonly object _lock = new();
#endif
internal RestCommandBase(DiscordRestClient client, Model model) internal RestCommandBase(DiscordRestClient client, Model model)
: base(client, model.Id) : base(client, model.Id)

View File

@@ -25,7 +25,11 @@ namespace Discord.Rest
/// <inheritdoc cref="IComponentInteraction.Message"/> /// <inheritdoc cref="IComponentInteraction.Message"/>
public RestUserMessage Message { get; private set; } public RestUserMessage Message { get; private set; }
#if NET9_0_OR_GREATER
private readonly Lock _lock = new(); private readonly Lock _lock = new();
#else
private readonly object _lock = new();
#endif
internal RestMessageComponent(BaseDiscordClient client, Model model) internal RestMessageComponent(BaseDiscordClient client, Model model)
: base(client, model.Id) : base(client, model.Id)
@@ -457,7 +461,7 @@ namespace Discord.Rest
} }
/// <summary> /// <summary>
/// ///
/// </summary> /// </summary>
/// <param name="ephemeral"></param> /// <param name="ephemeral"></param>
/// <param name="options"></param> /// <param name="options"></param>

View File

@@ -42,7 +42,11 @@ namespace Discord.Rest
return entity; return entity;
} }
#if NET9_0_OR_GREATER
private readonly Lock _lock = new(); private readonly Lock _lock = new();
#else
private readonly object _lock = new();
#endif
/// <summary> /// <summary>
/// Acknowledges this interaction with the <see cref="InteractionResponseType.DeferredUpdateMessage"/> if the modal was created /// Acknowledges this interaction with the <see cref="InteractionResponseType.DeferredUpdateMessage"/> if the modal was created
@@ -139,7 +143,7 @@ namespace Discord.Rest
MessageComponent component = null, MessageComponent component = null,
Embed embed = null, Embed embed = null,
RequestOptions options = null, RequestOptions options = null,
PollProperties poll = null, PollProperties poll = null,
MessageFlags flags = MessageFlags.None) MessageFlags flags = MessageFlags.None)
{ {
if (!IsValidToken) if (!IsValidToken)

View File

@@ -20,7 +20,11 @@ namespace Discord.Rest
/// </summary> /// </summary>
public new RestAutocompleteInteractionData Data { get; } public new RestAutocompleteInteractionData Data { get; }
#if NET9_0_OR_GREATER
private readonly Lock _lock = new(); private readonly Lock _lock = new();
#else
private readonly object _lock = new();
#endif
internal RestAutocompleteInteraction(DiscordRestClient client, Model model) internal RestAutocompleteInteraction(DiscordRestClient client, Model model)
: base(client, model.Id) : base(client, model.Id)

View File

@@ -17,7 +17,11 @@ namespace Discord.Net.Queue
{ {
private const int MinimumSleepTimeMs = 750; private const int MinimumSleepTimeMs = 750;
#if NET9_0_OR_GREATER
private readonly Lock _lock; private readonly Lock _lock;
#else
private readonly object _lock;
#endif
private readonly RequestQueue _queue; private readonly RequestQueue _queue;
private int _semaphore; private int _semaphore;
private DateTimeOffset? _resetTick; private DateTimeOffset? _resetTick;

View File

@@ -21,7 +21,11 @@ namespace Discord.WebSocket
private ImmutableArray<StickerPack<SocketSticker>> _defaultStickers; private ImmutableArray<StickerPack<SocketSticker>> _defaultStickers;
private int _totalShards; private int _totalShards;
private SemaphoreSlim[] _identifySemaphores; private SemaphoreSlim[] _identifySemaphores;
#if NET9_0_OR_GREATER
private readonly Lock _semaphoreResetLock; private readonly Lock _semaphoreResetLock;
#else
private readonly object _semaphoreResetLock;
#endif
private Task _semaphoreResetTask; private Task _semaphoreResetTask;
private bool _isDisposed; private bool _isDisposed;
@@ -524,7 +528,7 @@ namespace Discord.WebSocket
client.AuditLogCreated += (arg1, arg2) => _auditLogCreated.InvokeAsync(arg1, arg2); client.AuditLogCreated += (arg1, arg2) => _auditLogCreated.InvokeAsync(arg1, arg2);
client.VoiceChannelStatusUpdated += (arg1, arg2, arg3) => _voiceChannelStatusUpdated.InvokeAsync(arg1, arg2, arg3); client.VoiceChannelStatusUpdated += (arg1, arg2, arg3) => _voiceChannelStatusUpdated.InvokeAsync(arg1, arg2, arg3);
client.EntitlementCreated += (arg1) => _entitlementCreated.InvokeAsync(arg1); client.EntitlementCreated += (arg1) => _entitlementCreated.InvokeAsync(arg1);
client.EntitlementUpdated += (arg1, arg2) => _entitlementUpdated.InvokeAsync(arg1, arg2); client.EntitlementUpdated += (arg1, arg2) => _entitlementUpdated.InvokeAsync(arg1, arg2);
client.EntitlementDeleted += (arg1) => _entitlementDeleted.InvokeAsync(arg1); client.EntitlementDeleted += (arg1) => _entitlementDeleted.InvokeAsync(arg1);

View File

@@ -111,8 +111,13 @@ namespace Discord.WebSocket
private bool _usersDownloaded; private bool _usersDownloaded;
#if NET9_0_OR_GREATER
private readonly Lock _downloadLock = new(); private readonly Lock _downloadLock = new();
private readonly Lock _ownerLock = new(); private readonly Lock _ownerLock = new();
#else
private readonly object _downloadLock = new();
private readonly object _ownerLock = new();
#endif
private ulong _ownerId; private ulong _ownerId;

View File

@@ -21,11 +21,15 @@ namespace Discord.WebSocket
/// Gets the data received with this interaction, contains the button that was clicked. /// Gets the data received with this interaction, contains the button that was clicked.
/// </summary> /// </summary>
public new SocketMessageComponentData Data { get; } public new SocketMessageComponentData Data { get; }
/// <inheritdoc cref="IComponentInteraction.Message"/> /// <inheritdoc cref="IComponentInteraction.Message"/>
public SocketUserMessage Message { get; private set; } public SocketUserMessage Message { get; private set; }
#if NET9_0_OR_GREATER
private readonly Lock _lock = new(); private readonly Lock _lock = new();
#else
private readonly object _lock = new();
#endif
public override bool HasResponded { get; internal set; } = false; public override bool HasResponded { get; internal set; } = false;
internal SocketMessageComponent(DiscordSocketClient client, Model model, ISocketMessageChannel channel, SocketUser user) internal SocketMessageComponent(DiscordSocketClient client, Model model, ISocketMessageChannel channel, SocketUser user)
@@ -359,7 +363,7 @@ namespace Discord.WebSocket
flags |= MessageFlags.Ephemeral; flags |= MessageFlags.Ephemeral;
Preconditions.ValidateMessageFlags(flags); Preconditions.ValidateMessageFlags(flags);
var args = new API.Rest.CreateWebhookMessageParams var args = new API.Rest.CreateWebhookMessageParams
{ {

View File

@@ -67,7 +67,11 @@ namespace Discord.WebSocket
/// <inheritdoc/> /// <inheritdoc/>
public override bool HasResponded { get; internal set; } public override bool HasResponded { get; internal set; }
#if NET9_0_OR_GREATER
private readonly Lock _lock = new(); private readonly Lock _lock = new();
#else
private readonly object _lock = new();
#endif
/// <inheritdoc/> /// <inheritdoc/>
public override async Task RespondWithFilesAsync( public override async Task RespondWithFilesAsync(
@@ -440,7 +444,7 @@ namespace Discord.WebSocket
} }
/// <inheritdoc/> /// <inheritdoc/>
/// <remarks> /// <remarks>
/// Acknowledges this interaction with the <see cref="InteractionResponseType.DeferredUpdateMessage"/> if the modal was created /// Acknowledges this interaction with the <see cref="InteractionResponseType.DeferredUpdateMessage"/> if the modal was created
/// in a response to a message component interaction, <see cref="InteractionResponseType.DeferredChannelMessageWithSource"/> otherwise. /// in a response to a message component interaction, <see cref="InteractionResponseType.DeferredChannelMessageWithSource"/> otherwise.
/// </remarks> /// </remarks>

View File

@@ -22,7 +22,11 @@ namespace Discord.WebSocket
/// <inheritdoc/> /// <inheritdoc/>
public override bool HasResponded { get; internal set; } public override bool HasResponded { get; internal set; }
#if NET9_0_OR_GREATER
private readonly Lock _lock = new(); private readonly Lock _lock = new();
#else
private readonly object _lock = new();
#endif
internal SocketAutocompleteInteraction(DiscordSocketClient client, Model model, ISocketMessageChannel channel, SocketUser user) internal SocketAutocompleteInteraction(DiscordSocketClient client, Model model, ISocketMessageChannel channel, SocketUser user)
: base(client, model.Id, channel, user) : base(client, model.Id, channel, user)

View File

@@ -36,7 +36,11 @@ namespace Discord.WebSocket
/// <inheritdoc/> /// <inheritdoc/>
public override bool HasResponded { get; internal set; } public override bool HasResponded { get; internal set; }
#if NET9_0_OR_GREATER
private readonly Lock _lock = new(); private readonly Lock _lock = new();
#else
private readonly object _lock = new();
#endif
internal SocketCommandBase(DiscordSocketClient client, Model model, ISocketMessageChannel channel, SocketUser user) internal SocketCommandBase(DiscordSocketClient client, Model model, ISocketMessageChannel channel, SocketUser user)
: base(client, model.Id, channel, user) : base(client, model.Id, channel, user)

View File

@@ -32,7 +32,11 @@ namespace Discord.WebSocket
/// <inheritdoc /> /// <inheritdoc />
internal override SocketGlobalUser GlobalUser { get => this; set => throw new NotImplementedException(); } internal override SocketGlobalUser GlobalUser { get => this; set => throw new NotImplementedException(); }
#if NET9_0_OR_GREATER
private readonly Lock _lockObj = new(); private readonly Lock _lockObj = new();
#else
private readonly object _lockObj = new();
#endif
private ushort _references; private ushort _references;
private SocketGlobalUser(DiscordSocketClient discord, ulong id) private SocketGlobalUser(DiscordSocketClient discord, ulong id)