Syncpoint.cs 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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. // TODO: get rid of this lock
  14. private object _listLock = new object();
  15. /// <summary>
  16. /// The value of the syncpoint.
  17. /// </summary>
  18. public uint Value => (uint)_storedValue;
  19. // TODO: switch to something handling concurrency?
  20. private List<SyncpointWaiterHandle> _waiters;
  21. public Syncpoint(uint id)
  22. {
  23. Id = id;
  24. _waiters = new List<SyncpointWaiterHandle>();
  25. }
  26. /// <summary>
  27. /// Register a new callback for a target threshold.
  28. /// The callback will be called once the threshold is reached and will automatically be unregistered.
  29. /// </summary>
  30. /// <param name="threshold">The target threshold</param>
  31. /// <param name="callback">The callback to call when the threshold is reached</param>
  32. /// <returns>The created SyncpointWaiterHandle object or null if already past threshold</returns>
  33. public SyncpointWaiterHandle RegisterCallback(uint threshold, Action callback)
  34. {
  35. lock (_listLock)
  36. {
  37. if (Value >= threshold)
  38. {
  39. callback();
  40. return null;
  41. }
  42. else
  43. {
  44. SyncpointWaiterHandle waiterInformation = new SyncpointWaiterHandle
  45. {
  46. Threshold = threshold,
  47. Callback = callback
  48. };
  49. _waiters.Add(waiterInformation);
  50. return waiterInformation;
  51. }
  52. }
  53. }
  54. public void UnregisterCallback(SyncpointWaiterHandle waiterInformation)
  55. {
  56. lock (_listLock)
  57. {
  58. _waiters.Remove(waiterInformation);
  59. }
  60. }
  61. /// <summary>
  62. /// Increment the syncpoint
  63. /// </summary>
  64. /// <returns>The incremented value of the syncpoint</returns>
  65. public uint Increment()
  66. {
  67. uint currentValue = (uint)Interlocked.Increment(ref _storedValue);
  68. lock (_listLock)
  69. {
  70. _waiters.RemoveAll(item =>
  71. {
  72. bool isPastThreshold = currentValue >= item.Threshold;
  73. if (isPastThreshold)
  74. {
  75. item.Callback();
  76. }
  77. return isPastThreshold;
  78. });
  79. }
  80. return currentValue;
  81. }
  82. }
  83. }