ThreadLocalMap.cs 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. using System.Runtime.InteropServices;
  2. using System.Threading;
  3. using static Ryujinx.Common.Memory.PartialUnmaps.PartialUnmapHelpers;
  4. namespace Ryujinx.Common.Memory.PartialUnmaps
  5. {
  6. /// <summary>
  7. /// A simple fixed size thread safe map that can be used from native code.
  8. /// Integer thread IDs map to corresponding structs.
  9. /// </summary>
  10. /// <typeparam name="T">The value type for the map</typeparam>
  11. [StructLayout(LayoutKind.Sequential, Pack = 1)]
  12. public struct ThreadLocalMap<T> where T : unmanaged
  13. {
  14. public const int MapSize = 20;
  15. public Array20<int> ThreadIds;
  16. public Array20<T> Structs;
  17. public static int ThreadIdsOffset;
  18. public static int StructsOffset;
  19. /// <summary>
  20. /// Populates the field offsets for use when emitting native code.
  21. /// </summary>
  22. static ThreadLocalMap()
  23. {
  24. ThreadLocalMap<T> instance = new ThreadLocalMap<T>();
  25. ThreadIdsOffset = OffsetOf(ref instance, ref instance.ThreadIds);
  26. StructsOffset = OffsetOf(ref instance, ref instance.Structs);
  27. }
  28. /// <summary>
  29. /// Gets the index of a given thread ID in the map, or reserves one.
  30. /// When reserving a struct, its value is set to the given initial value.
  31. /// Returns -1 when there is no space to reserve a new entry.
  32. /// </summary>
  33. /// <param name="threadId">Thread ID to use as a key</param>
  34. /// <param name="initial">Initial value of the associated struct.</param>
  35. /// <returns>The index of the entry, or -1 if none</returns>
  36. public int GetOrReserve(int threadId, T initial)
  37. {
  38. // Try get a match first.
  39. for (int i = 0; i < MapSize; i++)
  40. {
  41. int compare = Interlocked.CompareExchange(ref ThreadIds[i], threadId, threadId);
  42. if (compare == threadId)
  43. {
  44. return i;
  45. }
  46. }
  47. // Try get a free entry. Since the id is assumed to be unique to this thread, we know it doesn't exist yet.
  48. for (int i = 0; i < MapSize; i++)
  49. {
  50. int compare = Interlocked.CompareExchange(ref ThreadIds[i], threadId, 0);
  51. if (compare == 0)
  52. {
  53. Structs[i] = initial;
  54. return i;
  55. }
  56. }
  57. return -1;
  58. }
  59. /// <summary>
  60. /// Gets the struct value for a given map entry.
  61. /// </summary>
  62. /// <param name="index">Index of the entry</param>
  63. /// <returns>A reference to the struct value</returns>
  64. public ref T GetValue(int index)
  65. {
  66. return ref Structs[index];
  67. }
  68. /// <summary>
  69. /// Releases an entry from the map.
  70. /// </summary>
  71. /// <param name="index">Index of the entry to release</param>
  72. public void Release(int index)
  73. {
  74. Interlocked.Exchange(ref ThreadIds[index], 0);
  75. }
  76. }
  77. }