Skip to content

Commit a483fd3

Browse files
authored
Use nint for native-sized integers in Interlocked (#118336)
* Fix CA2020: Prevent behavioral change
1 parent bca7bfa commit a483fd3

File tree

1 file changed

+22
-26
lines changed
  • src/libraries/System.Private.CoreLib/src/System/Threading

1 file changed

+22
-26
lines changed

src/libraries/System.Private.CoreLib/src/System/Threading/Interlocked.cs

Lines changed: 22 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -192,38 +192,36 @@ public static float Exchange(ref float location1, float value)
192192
public static double Exchange(ref double location1, double value)
193193
=> Unsafe.BitCast<long, double>(Exchange(ref Unsafe.As<double, long>(ref location1), Unsafe.BitCast<double, long>(value)));
194194

195-
/// <summary>Sets a platform-specific handle or pointer to a specified value and returns the original value, as an atomic operation.</summary>
195+
/// <summary>Sets a native-sized signed integer to a specified value and returns the original value, as an atomic operation.</summary>
196196
/// <param name="location1">The variable to set to the specified value.</param>
197197
/// <param name="value">The value to which the <paramref name="location1"/> parameter is set.</param>
198198
/// <returns>The original value of <paramref name="location1"/>.</returns>
199199
/// <exception cref="NullReferenceException">The address of location1 is a null pointer.</exception>
200200
[Intrinsic]
201201
[MethodImpl(MethodImplOptions.AggressiveInlining)]
202-
public static IntPtr Exchange(ref IntPtr location1, IntPtr value)
202+
public static nint Exchange(ref nint location1, nint value)
203203
{
204-
#pragma warning disable CA2020 // Prevent from behavioral change
205204
#if TARGET_64BIT
206-
return (IntPtr)Interlocked.Exchange(ref Unsafe.As<IntPtr, long>(ref location1), (long)value);
205+
return (nint)Exchange(ref Unsafe.As<nint, long>(ref location1), (long)value);
207206
#else
208-
return (IntPtr)Exchange(ref Unsafe.As<IntPtr, int>(ref location1), (int)value);
207+
return (nint)Exchange(ref Unsafe.As<nint, int>(ref location1), (int)value);
209208
#endif
210-
#pragma warning restore CA2020
211209
}
212210

213-
/// <summary>Sets a platform-specific handle or pointer to a specified value and returns the original value, as an atomic operation.</summary>
211+
/// <summary>Sets a native-sized unsigned integer to a specified value and returns the original value, as an atomic operation.</summary>
214212
/// <param name="location1">The variable to set to the specified value.</param>
215213
/// <param name="value">The value to which the <paramref name="location1"/> parameter is set.</param>
216214
/// <returns>The original value of <paramref name="location1"/>.</returns>
217215
/// <exception cref="NullReferenceException">The address of location1 is a null pointer.</exception>
218216
[Intrinsic]
219217
[CLSCompliant(false)]
220218
[MethodImpl(MethodImplOptions.AggressiveInlining)]
221-
public static UIntPtr Exchange(ref UIntPtr location1, UIntPtr value)
219+
public static nuint Exchange(ref nuint location1, nuint value)
222220
{
223221
#if TARGET_64BIT
224-
return (UIntPtr)Interlocked.Exchange(ref Unsafe.As<UIntPtr, long>(ref location1), (long)value);
222+
return (nuint)Exchange(ref Unsafe.As<nuint, long>(ref location1), (long)value);
225223
#else
226-
return (UIntPtr)Exchange(ref Unsafe.As<UIntPtr, int>(ref location1), (int)value);
224+
return (nuint)Exchange(ref Unsafe.As<nuint, int>(ref location1), (int)value);
227225
#endif
228226
}
229227

@@ -441,40 +439,38 @@ public static float CompareExchange(ref float location1, float value, float comp
441439
public static double CompareExchange(ref double location1, double value, double comparand)
442440
=> Unsafe.BitCast<long, double>(CompareExchange(ref Unsafe.As<double, long>(ref location1), Unsafe.BitCast<double, long>(value), Unsafe.BitCast<double, long>(comparand)));
443441

444-
/// <summary>Compares two platform-specific handles or pointers for equality and, if they are equal, replaces the first one.</summary>
445-
/// <param name="location1">The destination <see cref="IntPtr"/>, whose value is compared with the value of <paramref name="comparand"/> and possibly replaced by <paramref name="value"/>.</param>
446-
/// <param name="value">The <see cref="IntPtr"/> that replaces the destination value if the comparison results in equality.</param>
447-
/// <param name="comparand">The <see cref="IntPtr"/> that is compared to the value at <paramref name="location1"/>.</param>
442+
/// <summary>Compares two native-sized signed integers for equality and, if they are equal, replaces the first one.</summary>
443+
/// <param name="location1">The destination, whose value is compared with the value of <paramref name="comparand"/> and possibly replaced by <paramref name="value"/>.</param>
444+
/// <param name="value">The value that replaces the destination value if the comparison results in equality.</param>
445+
/// <param name="comparand">The value that is compared to the value at <paramref name="location1"/>.</param>
448446
/// <returns>The original value in <paramref name="location1"/>.</returns>
449447
/// <exception cref="NullReferenceException">The address of <paramref name="location1"/> is a null pointer.</exception>
450448
[Intrinsic]
451449
[MethodImpl(MethodImplOptions.AggressiveInlining)]
452-
public static IntPtr CompareExchange(ref IntPtr location1, IntPtr value, IntPtr comparand)
450+
public static nint CompareExchange(ref nint location1, nint value, nint comparand)
453451
{
454-
#pragma warning disable CA2020 // Prevent from behavioral change
455452
#if TARGET_64BIT
456-
return (IntPtr)Interlocked.CompareExchange(ref Unsafe.As<IntPtr, long>(ref location1), (long)value, (long)comparand);
453+
return (nint)CompareExchange(ref Unsafe.As<nint, long>(ref location1), (long)value, (long)comparand);
457454
#else
458-
return (IntPtr)CompareExchange(ref Unsafe.As<IntPtr, int>(ref location1), (int)value, (int)comparand);
455+
return (nint)CompareExchange(ref Unsafe.As<nint, int>(ref location1), (int)value, (int)comparand);
459456
#endif
460-
#pragma warning restore CA2020
461457
}
462458

463-
/// <summary>Compares two platform-specific handles or pointers for equality and, if they are equal, replaces the first one.</summary>
464-
/// <param name="location1">The destination <see cref="UIntPtr"/>, whose value is compared with the value of <paramref name="comparand"/> and possibly replaced by <paramref name="value"/>.</param>
465-
/// <param name="value">The <see cref="UIntPtr"/> that replaces the destination value if the comparison results in equality.</param>
466-
/// <param name="comparand">The <see cref="UIntPtr"/> that is compared to the value at <paramref name="location1"/>.</param>
459+
/// <summary>Compares two native-sized unsigned integers for equality and, if they are equal, replaces the first one.</summary>
460+
/// <param name="location1">The destination, whose value is compared with the value of <paramref name="comparand"/> and possibly replaced by <paramref name="value"/>.</param>
461+
/// <param name="value">The value that replaces the destination value if the comparison results in equality.</param>
462+
/// <param name="comparand">The value that is compared to the value at <paramref name="location1"/>.</param>
467463
/// <returns>The original value in <paramref name="location1"/>.</returns>
468464
/// <exception cref="NullReferenceException">The address of <paramref name="location1"/> is a null pointer.</exception>
469465
[Intrinsic]
470466
[CLSCompliant(false)]
471467
[MethodImpl(MethodImplOptions.AggressiveInlining)]
472-
public static UIntPtr CompareExchange(ref UIntPtr location1, UIntPtr value, UIntPtr comparand)
468+
public static nuint CompareExchange(ref nuint location1, nuint value, nuint comparand)
473469
{
474470
#if TARGET_64BIT
475-
return (UIntPtr)Interlocked.CompareExchange(ref Unsafe.As<UIntPtr, long>(ref location1), (long)value, (long)comparand);
471+
return (nuint)CompareExchange(ref Unsafe.As<nuint, long>(ref location1), (long)value, (long)comparand);
476472
#else
477-
return (UIntPtr)CompareExchange(ref Unsafe.As<UIntPtr, int>(ref location1), (int)value, (int)comparand);
473+
return (nuint)CompareExchange(ref Unsafe.As<nuint, int>(ref location1), (int)value, (int)comparand);
478474
#endif
479475
}
480476

0 commit comments

Comments
 (0)