ConditionVariable.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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, long 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. Process.Scheduler.Suspend(Thread.ProcessorId);
  31. if (Timeout < 0)
  32. {
  33. Result = WaitEvent.WaitOne();
  34. }
  35. else
  36. {
  37. Result = WaitEvent.WaitOne((int)(Timeout / 1000000));
  38. lock (WaitingThreads)
  39. {
  40. WaitingThreads.Remove((Thread, WaitEvent));
  41. }
  42. }
  43. Process.Scheduler.Resume(Thread);
  44. }
  45. }
  46. AcquireCondVarValue();
  47. Count = Process.Memory.ReadInt32(CondVarAddress);
  48. if (Count > 0)
  49. {
  50. Process.Memory.WriteInt32(CondVarAddress, Count - 1);
  51. }
  52. ReleaseCondVarValue();
  53. return Result;
  54. }
  55. public void SetSignal(KThread Thread, int Count)
  56. {
  57. lock (WaitingThreads)
  58. {
  59. if (Count < 0)
  60. {
  61. Process.Memory.WriteInt32(CondVarAddress, WaitingThreads.Count);
  62. foreach ((_, AutoResetEvent WaitEvent) in WaitingThreads)
  63. {
  64. WaitEvent.Set();
  65. }
  66. WaitingThreads.Clear();
  67. }
  68. else
  69. {
  70. Process.Memory.WriteInt32(CondVarAddress, Count);
  71. while (WaitingThreads.Count > 0 && Count-- > 0)
  72. {
  73. int HighestPriority = WaitingThreads[0].Thread.Priority;
  74. int HighestPrioIndex = 0;
  75. for (int Index = 1; Index < WaitingThreads.Count; Index++)
  76. {
  77. if (HighestPriority > WaitingThreads[Index].Thread.Priority)
  78. {
  79. HighestPriority = WaitingThreads[Index].Thread.Priority;
  80. HighestPrioIndex = Index;
  81. }
  82. }
  83. WaitingThreads[HighestPrioIndex].WaitEvent.Set();
  84. WaitingThreads.RemoveAt(HighestPrioIndex);
  85. }
  86. }
  87. }
  88. }
  89. private void AcquireCondVarValue()
  90. {
  91. if (!OwnsCondVarValue)
  92. {
  93. while (!Process.Memory.AcquireAddress(CondVarAddress))
  94. {
  95. Thread.Yield();
  96. }
  97. OwnsCondVarValue = true;
  98. }
  99. }
  100. private void ReleaseCondVarValue()
  101. {
  102. if (OwnsCondVarValue)
  103. {
  104. OwnsCondVarValue = false;
  105. Process.Memory.ReleaseAddress(CondVarAddress);
  106. }
  107. }
  108. }
  109. }