From a5c2aead6795192158caeb79e785543a15b5b2f4 Mon Sep 17 00:00:00 2001 From: merry Date: Tue, 29 Nov 2022 13:47:57 +0000 Subject: [PATCH] ConcurrentBitmap: Use Interlocked Or/And (#3937) --- Ryujinx.Memory/Tracking/ConcurrentBitmap.cs | 33 ++++++++------------- 1 file changed, 12 insertions(+), 21 deletions(-) diff --git a/Ryujinx.Memory/Tracking/ConcurrentBitmap.cs b/Ryujinx.Memory/Tracking/ConcurrentBitmap.cs index 2e007bb560..994fda9216 100644 --- a/Ryujinx.Memory/Tracking/ConcurrentBitmap.cs +++ b/Ryujinx.Memory/Tracking/ConcurrentBitmap.cs @@ -41,7 +41,7 @@ namespace Ryujinx.Memory.Tracking { for (int i = 0; i < Masks.Length; i++) { - if (Volatile.Read(ref Masks[i]) != 0) + if (Interlocked.Read(ref Masks[i]) != 0) { return true; } @@ -62,7 +62,7 @@ namespace Ryujinx.Memory.Tracking long wordMask = 1L << wordBit; - return (Volatile.Read(ref Masks[wordIndex]) & wordMask) != 0; + return (Interlocked.Read(ref Masks[wordIndex]) & wordMask) != 0; } /// @@ -86,7 +86,7 @@ namespace Ryujinx.Memory.Tracking int endBit = end & IntMask; long endMask = (long)(ulong.MaxValue >> (IntMask - endBit)); - long startValue = Volatile.Read(ref Masks[startIndex]); + long startValue = Interlocked.Read(ref Masks[startIndex]); if (startIndex == endIndex) { @@ -100,13 +100,13 @@ namespace Ryujinx.Memory.Tracking for (int i = startIndex + 1; i < endIndex; i++) { - if (Volatile.Read(ref Masks[i]) != 0) + if (Interlocked.Read(ref Masks[i]) != 0) { return true; } } - long endValue = Volatile.Read(ref Masks[endIndex]); + long endValue = Interlocked.Read(ref Masks[endIndex]); if ((endValue & endMask) != 0) { @@ -128,23 +128,14 @@ namespace Ryujinx.Memory.Tracking long wordMask = 1L << wordBit; - long existing; - long newValue; - - do + if (value) { - existing = Volatile.Read(ref Masks[wordIndex]); - - if (value) - { - newValue = existing | wordMask; - } - else - { - newValue = existing & ~wordMask; - } + Interlocked.Or(ref Masks[wordIndex], wordMask); + } + else + { + Interlocked.And(ref Masks[wordIndex], ~wordMask); } - while (Interlocked.CompareExchange(ref Masks[wordIndex], newValue, existing) != existing); } /// @@ -154,7 +145,7 @@ namespace Ryujinx.Memory.Tracking { for (int i = 0; i < Masks.Length; i++) { - Volatile.Write(ref Masks[i], 0); + Interlocked.Exchange(ref Masks[i], 0); } } }