Syncpoint.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Threading;
  4. namespace Ryujinx.Graphics.Gpu.Synchronization
  5. {
  6. /// <summary>
  7. /// Represents GPU hardware syncpoint.
  8. /// </summary>
  9. class Syncpoint
  10. {
  11. private int _storedValue;
  12. public readonly uint Id;
  13. /// <summary>
  14. /// The value of the syncpoint.
  15. /// </summary>
  16. public uint Value => (uint)_storedValue;
  17. // TODO: switch to something handling concurrency?
  18. private readonly List<SyncpointWaiterHandle> _waiters;
  19. public Syncpoint(uint id)
  20. {
  21. Id = id;
  22. _waiters = new List<SyncpointWaiterHandle>();
  23. }
  24. /// <summary>
  25. /// Register a new callback for a target threshold.
  26. /// The callback will be called once the threshold is reached and will automatically be unregistered.
  27. /// </summary>
  28. /// <param name="threshold">The target threshold</param>
  29. /// <param name="callback">The callback to call when the threshold is reached</param>
  30. /// <returns>The created SyncpointWaiterHandle object or null if already past threshold</returns>
  31. public SyncpointWaiterHandle RegisterCallback(uint threshold, Action<SyncpointWaiterHandle> callback)
  32. {
  33. lock (_waiters)
  34. {
  35. if (Value >= threshold)
  36. {
  37. callback(null);
  38. return null;
  39. }
  40. else
  41. {
  42. SyncpointWaiterHandle waiterInformation = new SyncpointWaiterHandle
  43. {
  44. Threshold = threshold,
  45. Callback = callback
  46. };
  47. _waiters.Add(waiterInformation);
  48. return waiterInformation;
  49. }
  50. }
  51. }
  52. public void UnregisterCallback(SyncpointWaiterHandle waiterInformation)
  53. {
  54. lock (_waiters)
  55. {
  56. _waiters.Remove(waiterInformation);
  57. }
  58. }
  59. /// <summary>
  60. /// Increment the syncpoint
  61. /// </summary>
  62. /// <returns>The incremented value of the syncpoint</returns>
  63. public uint Increment()
  64. {
  65. uint currentValue = (uint)Interlocked.Increment(ref _storedValue);
  66. SyncpointWaiterHandle expired = null;
  67. List<SyncpointWaiterHandle> expiredList = null;
  68. lock (_waiters)
  69. {
  70. _waiters.RemoveAll(item =>
  71. {
  72. bool isPastThreshold = currentValue >= item.Threshold;
  73. if (isPastThreshold)
  74. {
  75. if (expired == null)
  76. {
  77. expired = item;
  78. }
  79. else
  80. {
  81. if (expiredList == null)
  82. {
  83. expiredList = new List<SyncpointWaiterHandle>();
  84. }
  85. expiredList.Add(item);
  86. }
  87. }
  88. return isPastThreshold;
  89. });
  90. }
  91. // Call the callbacks as a separate step.
  92. // As we don't know what the callback will be doing,
  93. // and it could block execution for a indefinite amount of time,
  94. // we can't call it inside the lock.
  95. if (expired != null)
  96. {
  97. expired.Callback(expired);
  98. if (expiredList != null)
  99. {
  100. for (int i = 0; i < expiredList.Count; i++)
  101. {
  102. expiredList[i].Callback(expiredList[i]);
  103. }
  104. }
  105. }
  106. return currentValue;
  107. }
  108. }
  109. }