ConditionVariable.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. using Ryujinx.Core.OsHle.Handles;
  2. using System.Collections.Generic;
  3. using System.Threading;
  4. namespace Ryujinx.Core.OsHle.Kernel
  5. {
  6. class ConditionVariable
  7. {
  8. private Process Process;
  9. private long CondVarAddress;
  10. private bool OwnsCondVarValue;
  11. private List<(KThread Thread, AutoResetEvent WaitEvent)> WaitingThreads;
  12. public ConditionVariable(Process Process, long CondVarAddress)
  13. {
  14. this.Process = Process;
  15. this.CondVarAddress = CondVarAddress;
  16. WaitingThreads = new List<(KThread, AutoResetEvent)>();
  17. }
  18. public bool WaitForSignal(KThread Thread, ulong Timeout)
  19. {
  20. bool Result = true;
  21. int Count = Process.Memory.ReadInt32(CondVarAddress);
  22. if (Count <= 0)
  23. {
  24. using (AutoResetEvent WaitEvent = new AutoResetEvent(false))
  25. {
  26. lock (WaitingThreads)
  27. {
  28. WaitingThreads.Add((Thread, WaitEvent));
  29. }
  30. if (Timeout == ulong.MaxValue)
  31. {
  32. Result = WaitEvent.WaitOne();
  33. }
  34. else
  35. {
  36. Result = WaitEvent.WaitOne(NsTimeConverter.GetTimeMs(Timeout));
  37. lock (WaitingThreads)
  38. {
  39. WaitingThreads.Remove((Thread, WaitEvent));
  40. }
  41. }
  42. }
  43. }
  44. AcquireCondVarValue();
  45. Count = Process.Memory.ReadInt32(CondVarAddress);
  46. if (Result && Count > 0)
  47. {
  48. Process.Memory.WriteInt32(CondVarAddress, Count - 1);
  49. }
  50. ReleaseCondVarValue();
  51. return Result;
  52. }
  53. public void SetSignal(KThread Thread, int Count)
  54. {
  55. lock (WaitingThreads)
  56. {
  57. if (Count < 0)
  58. {
  59. foreach ((_, AutoResetEvent WaitEvent) in WaitingThreads)
  60. {
  61. IncrementCondVarValue();
  62. WaitEvent.Set();
  63. }
  64. WaitingThreads.Clear();
  65. }
  66. else
  67. {
  68. while (WaitingThreads.Count > 0 && Count-- > 0)
  69. {
  70. int HighestPriority = WaitingThreads[0].Thread.Priority;
  71. int HighestPrioIndex = 0;
  72. for (int Index = 1; Index < WaitingThreads.Count; Index++)
  73. {
  74. if (HighestPriority > WaitingThreads[Index].Thread.Priority)
  75. {
  76. HighestPriority = WaitingThreads[Index].Thread.Priority;
  77. HighestPrioIndex = Index;
  78. }
  79. }
  80. IncrementCondVarValue();
  81. WaitingThreads[HighestPrioIndex].WaitEvent.Set();
  82. WaitingThreads.RemoveAt(HighestPrioIndex);
  83. }
  84. }
  85. }
  86. Process.Scheduler.Yield(Thread);
  87. }
  88. private void IncrementCondVarValue()
  89. {
  90. AcquireCondVarValue();
  91. int Count = Process.Memory.ReadInt32(CondVarAddress);
  92. Process.Memory.WriteInt32(CondVarAddress, Count + 1);
  93. ReleaseCondVarValue();
  94. }
  95. private void AcquireCondVarValue()
  96. {
  97. if (!OwnsCondVarValue)
  98. {
  99. while (!Process.Memory.AcquireAddress(CondVarAddress))
  100. {
  101. Thread.Yield();
  102. }
  103. OwnsCondVarValue = true;
  104. }
  105. }
  106. private void ReleaseCondVarValue()
  107. {
  108. if (OwnsCondVarValue)
  109. {
  110. OwnsCondVarValue = false;
  111. Process.Memory.ReleaseAddress(CondVarAddress);
  112. }
  113. }
  114. }
  115. }