diff options
author | riperiperi <rhy3756547@hotmail.com> | 2022-07-30 00:16:29 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-07-29 19:16:29 -0300 |
commit | 14ce9e15672d03cb6fc067316f90d81471398ebc (patch) | |
tree | 39befb3c65a2548fe803bf746545de3fd8851ffd /Ryujinx.Common/Memory/PartialUnmaps/NativeReaderWriterLock.cs | |
parent | 952d013c67a1809fae3b3c7ade9a0757598d9e18 (diff) |
Move partial unmap handler to the native signal handler (#3437)1.1.199
* Initial commit with a lot of testing stuff.
* Partial Unmap Cleanup Part 1
* Fix some minor issues, hopefully windows tests.
* Disable partial unmap tests on macos for now
Weird issue.
* Goodbye magic number
* Add COMPlus_EnableAlternateStackCheck for tests
`COMPlus_EnableAlternateStackCheck` is needed for NullReferenceException handling to work on linux after registering the signal handler, due to how dotnet registers its own signal handler.
* Address some feedback
* Force retry when memory is mapped in memory tracking
This case existed before, but returning `false` no longer retries, so it would crash immediately after unprotecting the memory... Now, we return `true` to deliberately retry.
This case existed before (was just broken by this change) and I don't really want to look into fixing the issue right now. Technically, this means that on guest code partial unmaps will retry _due to this_ rather than hitting the handler. I don't expect this to cause any issues.
This should fix random crashes in Xenoblade Chronicles 2.
* Use IsRangeMapped
* Suppress MockMemoryManager.UnmapEvent warning
This event is not signalled by the mock memory manager.
* Remove 4kb mapping
Diffstat (limited to 'Ryujinx.Common/Memory/PartialUnmaps/NativeReaderWriterLock.cs')
-rw-r--r-- | Ryujinx.Common/Memory/PartialUnmaps/NativeReaderWriterLock.cs | 80 |
1 files changed, 80 insertions, 0 deletions
diff --git a/Ryujinx.Common/Memory/PartialUnmaps/NativeReaderWriterLock.cs b/Ryujinx.Common/Memory/PartialUnmaps/NativeReaderWriterLock.cs new file mode 100644 index 00000000..5419b340 --- /dev/null +++ b/Ryujinx.Common/Memory/PartialUnmaps/NativeReaderWriterLock.cs @@ -0,0 +1,80 @@ +using System.Runtime.InteropServices; +using System.Threading; + +using static Ryujinx.Common.Memory.PartialUnmaps.PartialUnmapHelpers; + +namespace Ryujinx.Common.Memory.PartialUnmaps +{ + /// <summary> + /// A simple implementation of a ReaderWriterLock which can be used from native code. + /// </summary> + [StructLayout(LayoutKind.Sequential, Pack = 1)] + public struct NativeReaderWriterLock + { + public int WriteLock; + public int ReaderCount; + + public static int WriteLockOffset; + public static int ReaderCountOffset; + + /// <summary> + /// Populates the field offsets for use when emitting native code. + /// </summary> + static NativeReaderWriterLock() + { + NativeReaderWriterLock instance = new NativeReaderWriterLock(); + + WriteLockOffset = OffsetOf(ref instance, ref instance.WriteLock); + ReaderCountOffset = OffsetOf(ref instance, ref instance.ReaderCount); + } + + /// <summary> + /// Acquires the reader lock. + /// </summary> + public void AcquireReaderLock() + { + // Must take write lock for a very short time to become a reader. + + while (Interlocked.CompareExchange(ref WriteLock, 1, 0) != 0) { } + + Interlocked.Increment(ref ReaderCount); + + Interlocked.Exchange(ref WriteLock, 0); + } + + /// <summary> + /// Releases the reader lock. + /// </summary> + public void ReleaseReaderLock() + { + Interlocked.Decrement(ref ReaderCount); + } + + /// <summary> + /// Upgrades to a writer lock. The reader lock is temporarily released while obtaining the writer lock. + /// </summary> + public void UpgradeToWriterLock() + { + // Prevent any more threads from entering reader. + // If the write lock is already taken, wait for it to not be taken. + + Interlocked.Decrement(ref ReaderCount); + + while (Interlocked.CompareExchange(ref WriteLock, 1, 0) != 0) { } + + // Wait for reader count to drop to 0, then take the lock again as the only reader. + + while (Interlocked.CompareExchange(ref ReaderCount, 1, 0) != 0) { } + } + + /// <summary> + /// Downgrades from a writer lock, back to a reader one. + /// </summary> + public void DowngradeFromWriterLock() + { + // Release the WriteLock. + + Interlocked.Exchange(ref WriteLock, 0); + } + } +} |