aboutsummaryrefslogtreecommitdiff
path: root/Ryujinx.Common/Memory/PartialUnmaps/ThreadLocalMap.cs
diff options
context:
space:
mode:
authorriperiperi <rhy3756547@hotmail.com>2022-07-30 00:16:29 +0200
committerGitHub <noreply@github.com>2022-07-29 19:16:29 -0300
commit14ce9e15672d03cb6fc067316f90d81471398ebc (patch)
tree39befb3c65a2548fe803bf746545de3fd8851ffd /Ryujinx.Common/Memory/PartialUnmaps/ThreadLocalMap.cs
parent952d013c67a1809fae3b3c7ade9a0757598d9e18 (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/ThreadLocalMap.cs')
-rw-r--r--Ryujinx.Common/Memory/PartialUnmaps/ThreadLocalMap.cs92
1 files changed, 92 insertions, 0 deletions
diff --git a/Ryujinx.Common/Memory/PartialUnmaps/ThreadLocalMap.cs b/Ryujinx.Common/Memory/PartialUnmaps/ThreadLocalMap.cs
new file mode 100644
index 00000000..a3bd5be8
--- /dev/null
+++ b/Ryujinx.Common/Memory/PartialUnmaps/ThreadLocalMap.cs
@@ -0,0 +1,92 @@
+using System.Runtime.InteropServices;
+using System.Threading;
+
+using static Ryujinx.Common.Memory.PartialUnmaps.PartialUnmapHelpers;
+
+namespace Ryujinx.Common.Memory.PartialUnmaps
+{
+ /// <summary>
+ /// A simple fixed size thread safe map that can be used from native code.
+ /// Integer thread IDs map to corresponding structs.
+ /// </summary>
+ /// <typeparam name="T">The value type for the map</typeparam>
+ [StructLayout(LayoutKind.Sequential, Pack = 1)]
+ public struct ThreadLocalMap<T> where T : unmanaged
+ {
+ public const int MapSize = 20;
+
+ public Array20<int> ThreadIds;
+ public Array20<T> Structs;
+
+ public static int ThreadIdsOffset;
+ public static int StructsOffset;
+
+ /// <summary>
+ /// Populates the field offsets for use when emitting native code.
+ /// </summary>
+ static ThreadLocalMap()
+ {
+ ThreadLocalMap<T> instance = new ThreadLocalMap<T>();
+
+ ThreadIdsOffset = OffsetOf(ref instance, ref instance.ThreadIds);
+ StructsOffset = OffsetOf(ref instance, ref instance.Structs);
+ }
+
+ /// <summary>
+ /// Gets the index of a given thread ID in the map, or reserves one.
+ /// When reserving a struct, its value is set to the given initial value.
+ /// Returns -1 when there is no space to reserve a new entry.
+ /// </summary>
+ /// <param name="threadId">Thread ID to use as a key</param>
+ /// <param name="initial">Initial value of the associated struct.</param>
+ /// <returns>The index of the entry, or -1 if none</returns>
+ public int GetOrReserve(int threadId, T initial)
+ {
+ // Try get a match first.
+
+ for (int i = 0; i < MapSize; i++)
+ {
+ int compare = Interlocked.CompareExchange(ref ThreadIds[i], threadId, threadId);
+
+ if (compare == threadId)
+ {
+ return i;
+ }
+ }
+
+ // Try get a free entry. Since the id is assumed to be unique to this thread, we know it doesn't exist yet.
+
+ for (int i = 0; i < MapSize; i++)
+ {
+ int compare = Interlocked.CompareExchange(ref ThreadIds[i], threadId, 0);
+
+ if (compare == 0)
+ {
+ Structs[i] = initial;
+ return i;
+ }
+ }
+
+ return -1;
+ }
+
+ /// <summary>
+ /// Gets the struct value for a given map entry.
+ /// </summary>
+ /// <param name="index">Index of the entry</param>
+ /// <returns>A reference to the struct value</returns>
+ public ref T GetValue(int index)
+ {
+ return ref Structs[index];
+ }
+
+ /// <summary>
+ /// Releases an entry from the map.
+ /// </summary>
+ /// <param name="index">Index of the entry to release</param>
+ public void Release(int index)
+ {
+ Interlocked.Exchange(ref ThreadIds[index], 0);
+ }
+ }
+}