PartialUnmapState.cs 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. using System;
  2. using System.Runtime.CompilerServices;
  3. using System.Runtime.InteropServices;
  4. using System.Runtime.Versioning;
  5. using System.Threading;
  6. using static Ryujinx.Common.Memory.PartialUnmaps.PartialUnmapHelpers;
  7. namespace Ryujinx.Common.Memory.PartialUnmaps
  8. {
  9. /// <summary>
  10. /// State for partial unmaps. Intended to be used on Windows.
  11. /// </summary>
  12. [StructLayout(LayoutKind.Sequential, Pack = 1)]
  13. public partial struct PartialUnmapState
  14. {
  15. public NativeReaderWriterLock PartialUnmapLock;
  16. public int PartialUnmapsCount;
  17. public ThreadLocalMap<int> LocalCounts;
  18. public readonly static int PartialUnmapLockOffset;
  19. public readonly static int PartialUnmapsCountOffset;
  20. public readonly static int LocalCountsOffset;
  21. public readonly static IntPtr GlobalState;
  22. [SupportedOSPlatform("windows")]
  23. [LibraryImport("kernel32.dll")]
  24. private static partial int GetCurrentThreadId();
  25. [SupportedOSPlatform("windows")]
  26. [LibraryImport("kernel32.dll", SetLastError = true)]
  27. private static partial IntPtr OpenThread(int dwDesiredAccess, [MarshalAs(UnmanagedType.Bool)] bool bInheritHandle, uint dwThreadId);
  28. [SupportedOSPlatform("windows")]
  29. [LibraryImport("kernel32.dll", SetLastError = true)]
  30. [return: MarshalAs(UnmanagedType.Bool)]
  31. private static partial bool CloseHandle(IntPtr hObject);
  32. [SupportedOSPlatform("windows")]
  33. [LibraryImport("kernel32.dll", SetLastError = true)]
  34. [return: MarshalAs(UnmanagedType.Bool)]
  35. private static partial bool GetExitCodeThread(IntPtr hThread, out uint lpExitCode);
  36. /// <summary>
  37. /// Creates a global static PartialUnmapState and populates the field offsets.
  38. /// </summary>
  39. static unsafe PartialUnmapState()
  40. {
  41. PartialUnmapState instance = new();
  42. PartialUnmapLockOffset = OffsetOf(ref instance, ref instance.PartialUnmapLock);
  43. PartialUnmapsCountOffset = OffsetOf(ref instance, ref instance.PartialUnmapsCount);
  44. LocalCountsOffset = OffsetOf(ref instance, ref instance.LocalCounts);
  45. int size = Unsafe.SizeOf<PartialUnmapState>();
  46. GlobalState = Marshal.AllocHGlobal(size);
  47. Unsafe.InitBlockUnaligned((void*)GlobalState, 0, (uint)size);
  48. }
  49. /// <summary>
  50. /// Resets the global state.
  51. /// </summary>
  52. public static unsafe void Reset()
  53. {
  54. int size = Unsafe.SizeOf<PartialUnmapState>();
  55. Unsafe.InitBlockUnaligned((void*)GlobalState, 0, (uint)size);
  56. }
  57. /// <summary>
  58. /// Gets a reference to the global state.
  59. /// </summary>
  60. /// <returns>A reference to the global state</returns>
  61. public static unsafe ref PartialUnmapState GetRef()
  62. {
  63. return ref Unsafe.AsRef<PartialUnmapState>((void*)GlobalState);
  64. }
  65. /// <summary>
  66. /// Checks if an access violation handler should retry execution due to a fault caused by partial unmap.
  67. /// </summary>
  68. /// <remarks>
  69. /// Due to Windows limitations, <see cref="UnmapView"/> might need to unmap more memory than requested.
  70. /// The additional memory that was unmapped is later remapped, however this leaves a time gap where the
  71. /// memory might be accessed but is unmapped. Users of the API must compensate for that by catching the
  72. /// access violation and retrying if it happened between the unmap and remap operation.
  73. /// This method can be used to decide if retrying in such cases is necessary or not.
  74. ///
  75. /// This version of the function is not used, but serves as a reference for the native
  76. /// implementation in ARMeilleure.
  77. /// </remarks>
  78. /// <returns>True if execution should be retried, false otherwise</returns>
  79. [SupportedOSPlatform("windows")]
  80. public bool RetryFromAccessViolation()
  81. {
  82. PartialUnmapLock.AcquireReaderLock();
  83. int threadID = GetCurrentThreadId();
  84. int threadIndex = LocalCounts.GetOrReserve(threadID, 0);
  85. if (threadIndex == -1)
  86. {
  87. // Out of thread local space... try again later.
  88. PartialUnmapLock.ReleaseReaderLock();
  89. return true;
  90. }
  91. ref int threadLocalPartialUnmapsCount = ref LocalCounts.GetValue(threadIndex);
  92. bool retry = threadLocalPartialUnmapsCount != PartialUnmapsCount;
  93. if (retry)
  94. {
  95. threadLocalPartialUnmapsCount = PartialUnmapsCount;
  96. }
  97. PartialUnmapLock.ReleaseReaderLock();
  98. return retry;
  99. }
  100. /// <summary>
  101. /// Iterates and trims threads in the thread -> count map that
  102. /// are no longer active.
  103. /// </summary>
  104. [SupportedOSPlatform("windows")]
  105. public void TrimThreads()
  106. {
  107. const uint ExitCodeStillActive = 259;
  108. const int ThreadQueryInformation = 0x40;
  109. Span<int> ids = LocalCounts.ThreadIds.AsSpan();
  110. for (int i = 0; i < ids.Length; i++)
  111. {
  112. int id = ids[i];
  113. if (id != 0)
  114. {
  115. IntPtr handle = OpenThread(ThreadQueryInformation, false, (uint)id);
  116. if (handle == IntPtr.Zero)
  117. {
  118. Interlocked.CompareExchange(ref ids[i], 0, id);
  119. }
  120. else
  121. {
  122. GetExitCodeThread(handle, out uint exitCode);
  123. if (exitCode != ExitCodeStillActive)
  124. {
  125. Interlocked.CompareExchange(ref ids[i], 0, id);
  126. }
  127. CloseHandle(handle);
  128. }
  129. }
  130. }
  131. }
  132. }
  133. }