Skip to content

Replace Interlocked.{Compare}Exchange Int32 overloads with generic <bool> variants #9480

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Mar 3, 2025
Merged
Show file tree
Hide file tree
Changes from all 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
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
using System.Collections;
using System.Windows.Media.Composition;
using System.Windows.Threading;
using System.Threading;
using MS.Internal;

using SafeNativeMethods = MS.Win32.PresentationCore.SafeNativeMethods;
Expand Down Expand Up @@ -144,12 +145,12 @@ internal static void Shutdown(MediaContext mc)
/// </summary>
internal static void PropagateDirtyRectangleSettings()
{
int oldValue = s_DisableDirtyRectangles;
int disableDirtyRectangles = CoreAppContextSwitches.DisableDirtyRectangles ? 1 : 0;
bool oldValue = s_disableDirtyRectangles;
bool disableDirtyRectangles = CoreAppContextSwitches.DisableDirtyRectangles;

if (disableDirtyRectangles != oldValue)
{
if (System.Threading.Interlocked.CompareExchange(ref s_DisableDirtyRectangles, disableDirtyRectangles, oldValue) == oldValue)
if (Interlocked.CompareExchange(ref s_disableDirtyRectangles, disableDirtyRectangles, oldValue) == oldValue)
{
NotifyRedirectionEnvironmentChanged();
}
Expand All @@ -158,7 +159,7 @@ internal static void PropagateDirtyRectangleSettings()

internal static bool DisableDirtyRectangles
{
get { return (s_DisableDirtyRectangles != 0); }
get => s_disableDirtyRectangles;
}

/// <summary>
Expand Down Expand Up @@ -355,9 +356,10 @@ internal static bool AnimationSmoothing
/// </summary>
private static bool s_forceSoftareForGraphicsStreamMagnifier;

// 1 if app is requesting to disable D3D dirty rectangle work, 0 otherwise.
// We use Interlocked.CompareExchange to change this, which supports int but not bool.
private static int s_DisableDirtyRectangles = 0;
/// <summary>
/// <see langword="true"/> if the app is requesting to disable D3D dirty rectangle work, <see langword="false"/> otherwise.
/// </summary>
private static bool s_disableDirtyRectangles = false;
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -677,7 +677,7 @@ internal bool UseSizingWorkaroundForTextBox
//-------------------------------------------------------------------
// Is this page already disposed?
//-------------------------------------------------------------------
internal bool IsDisposed { get { return (_disposed != 0) || _structuralCache.PtsContext.Disposed; } }
internal bool IsDisposed { get { return _disposed || _structuralCache.PtsContext.Disposed; } }

//-------------------------------------------------------------------
// Size of content on page.
Expand Down Expand Up @@ -798,7 +798,7 @@ internal Rect Viewport
private void Dispose(bool disposing)
{
// Do actual dispose only once.
if (Interlocked.CompareExchange(ref _disposed, 1, 0) == 0)
if (Interlocked.CompareExchange(ref _disposed, true, false) == false)
{
if (disposing)
{
Expand Down Expand Up @@ -1098,7 +1098,7 @@ private PageVisual PageVisual
//-------------------------------------------------------------------
// Is it already disposed?
//-------------------------------------------------------------------
private int _disposed;
private bool _disposed;

//-------------------------------------------------------------------
// Max of dcpDepend for page
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ private void Dispose(bool disposing)
PtsContext ptsContext = null;

// Do actual dispose only once.
if (Interlocked.CompareExchange(ref _disposed, 1, 0) == 0)
if (Interlocked.CompareExchange(ref _disposed, true, false) == false)
{
// Dispose PTS break record.
// According to following article the entire reachable graph from
Expand Down Expand Up @@ -183,7 +183,7 @@ private void Dispose(bool disposing)
/// <summary>
/// Whether object is already disposed.
/// </summary>
private int _disposed;
private bool _disposed;

#endregion Private Fields
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ internal static bool IsDisposed()
PtsCache ptsCache = Dispatcher.CurrentDispatcher.PtsCache as PtsCache;
if (ptsCache != null)
{
disposed = (ptsCache._disposed == 1);
disposed = ptsCache._disposed;
}
}
return disposed;
Expand Down Expand Up @@ -163,7 +163,7 @@ private PtsCache(Dispatcher dispatcher)
~PtsCache()
{
// After shutdown is initiated, do not allow Finalizer thread to the cleanup.
if (0 == Interlocked.CompareExchange(ref _disposed, 1, 0))
if (Interlocked.CompareExchange(ref _disposed, true, false) == false)
{
// Destroy all PTS contexts
DestroyPTSContexts();
Expand Down Expand Up @@ -225,7 +225,7 @@ private void ReleaseContextCore(PtsContext ptsContext)
{
// After shutdown is initiated, do not allow Finalizer thread to add any
// items to _releaseQueue.
if (_disposed == 0)
if (_disposed == false)
{
// Add PtsContext to collection of released PtsContexts.
// If the queue is empty, schedule Dispatcher time to dispose
Expand Down Expand Up @@ -292,7 +292,7 @@ private void Shutdown()

// After shutdown is initiated, do not allow Finalizer thread to add any
// items to _releaseQueue.
if (0 == Interlocked.CompareExchange(ref _disposed, 1, 0))
if (Interlocked.CompareExchange(ref _disposed, true, false) == false)
{
// Dispose any pending PtsContexts stored in _releaseQueue
OnPtsContextReleased(false);
Expand Down Expand Up @@ -763,7 +763,7 @@ private unsafe void InitTableObjInfo(PtsHost ptsHost, ref PTS.FSTABLEOBJINIT tab
/// <summary>
/// Whether object is already disposed.
/// </summary>
private int _disposed;
private bool _disposed;

#endregion Private Fields

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public void Dispose()
int index;

// Do actual dispose only once.
if (Interlocked.CompareExchange(ref _disposed, 1, 0) == 0)
if (Interlocked.CompareExchange(ref _disposed, true, false) == false)
{
// Destroy all page break records. The collection is allocated during creation
// of the context, and can be only destroyed during dispose process.
Expand Down Expand Up @@ -370,7 +370,7 @@ internal void OnPageBreakRecordDisposed(IntPtr br, bool disposing)
/// </summary>
internal bool Disposed
{
get { return (_disposed != 0); }
get { return _disposed; }
}

/// <summary>
Expand Down Expand Up @@ -609,7 +609,7 @@ private object OnDestroyBreakRecord(object args)
/// <summary>
/// Whether object is already disposed.
/// </summary>
private int _disposed;
private bool _disposed;

/// <summary>
/// Whether Dispose has been completed. It may be set to 'false' even when
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -705,7 +705,7 @@ internal bool UseSizingWorkaroundForTextBox
/// </remarks>
private void Dispose(bool disposing)
{
if (Interlocked.CompareExchange(ref _disposed, 1, 0) == 0)
if (Interlocked.CompareExchange(ref _disposed, true, false) == false)
{
// Destroy PTS page.
// According to following article the entire reachable graph from
Expand Down Expand Up @@ -1522,7 +1522,7 @@ private bool IsEmpty
// ------------------------------------------------------------------
// Is object already disposed.
// ------------------------------------------------------------------
private int _disposed;
private bool _disposed;

#endregion Private Fields
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,9 +116,8 @@ internal void StopListening()
private void HandleShutDown(object sender, EventArgs e)
{
// The dispatcher and AppDomain events might arrive on separate threads
// at the same time. The interlock assures that we only do the work
// once.
if (Interlocked.Exchange(ref _inShutDown, 1) == 0)
// at the same time. The interlock assures that we only do the work once.
if (Interlocked.Exchange(ref _inShutDown, true) == false)
{
// ShutDown is a one-time event. Stop listening (thus releasing
// references to the ShutDownListener).
Expand Down Expand Up @@ -146,6 +145,6 @@ enum PrivateFlags : ushort

PrivateFlags _flags;
WeakReference _dispatcherWR;
int _inShutDown;
bool _inShutDown;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ internal static class AccessibilitySwitches
/// <summary>
/// Guards against multiple verifications of the switch values.
/// </summary>
static int s_SwitchesVerified = 0;
private static bool s_switchesVerified = false;

#endregion

Expand Down Expand Up @@ -183,7 +183,7 @@ internal static void SetSwitchDefaults(string platformIdentifier, int targetFram
[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal static void VerifySwitches(Dispatcher dispatcher)
{
if (Interlocked.CompareExchange(ref s_SwitchesVerified, 1, 0) == 0)
if (Interlocked.CompareExchange(ref s_switchesVerified, true, false) == false)
{
bool netFx47 = UseNetFx47CompatibleAccessibilityFeatures;
bool netFx471 = UseNetFx471CompatibleAccessibilityFeatures;
Expand Down