KThread.cs 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. using ChocolArm64;
  2. using System;
  3. namespace Ryujinx.Core.OsHle.Handles
  4. {
  5. class KThread : KSynchronizationObject
  6. {
  7. public AThread Thread { get; private set; }
  8. public int CoreMask { get; set; }
  9. public long MutexAddress { get; set; }
  10. public long CondVarAddress { get; set; }
  11. private Process Process;
  12. public KThread NextMutexThread { get; set; }
  13. public KThread NextCondVarThread { get; set; }
  14. public KThread MutexOwner { get; set; }
  15. public int ActualPriority { get; private set; }
  16. public int WantedPriority { get; private set; }
  17. public int IdealCore { get; private set; }
  18. public int ActualCore { get; set; }
  19. public int WaitHandle { get; set; }
  20. public int ThreadId => Thread.ThreadId;
  21. public KThread(
  22. AThread Thread,
  23. Process Process,
  24. int IdealCore,
  25. int Priority)
  26. {
  27. this.Thread = Thread;
  28. this.Process = Process;
  29. this.IdealCore = IdealCore;
  30. CoreMask = 1 << IdealCore;
  31. ActualPriority = WantedPriority = Priority;
  32. }
  33. public void SetPriority(int Priority)
  34. {
  35. WantedPriority = Priority;
  36. UpdatePriority();
  37. }
  38. public void UpdatePriority()
  39. {
  40. int OldPriority = ActualPriority;
  41. int CurrPriority = WantedPriority;
  42. if (NextMutexThread != null && CurrPriority > NextMutexThread.WantedPriority)
  43. {
  44. CurrPriority = NextMutexThread.WantedPriority;
  45. }
  46. if (CurrPriority != OldPriority)
  47. {
  48. ActualPriority = CurrPriority;
  49. UpdateWaitLists();
  50. MutexOwner?.UpdatePriority();
  51. }
  52. }
  53. private void UpdateWaitLists()
  54. {
  55. UpdateMutexList();
  56. UpdateCondVarList();
  57. Process.Scheduler.Resort(this);
  58. }
  59. private void UpdateMutexList()
  60. {
  61. KThread OwnerThread = MutexOwner;
  62. if (OwnerThread == null)
  63. {
  64. return;
  65. }
  66. //The MutexOwner field should only be non-null when the thread is
  67. //waiting for the lock, and the lock belongs to another thread.
  68. if (OwnerThread == this)
  69. {
  70. throw new InvalidOperationException();
  71. }
  72. lock (OwnerThread)
  73. {
  74. //Remove itself from the list.
  75. KThread CurrThread = OwnerThread;
  76. while (CurrThread.NextMutexThread != null)
  77. {
  78. if (CurrThread.NextMutexThread == this)
  79. {
  80. CurrThread.NextMutexThread = NextMutexThread;
  81. break;
  82. }
  83. CurrThread = CurrThread.NextMutexThread;
  84. }
  85. //Re-add taking new priority into account.
  86. CurrThread = OwnerThread;
  87. while (CurrThread.NextMutexThread != null)
  88. {
  89. if (CurrThread.NextMutexThread.ActualPriority > ActualPriority)
  90. {
  91. break;
  92. }
  93. CurrThread = CurrThread.NextMutexThread;
  94. }
  95. NextMutexThread = CurrThread.NextMutexThread;
  96. CurrThread.NextMutexThread = this;
  97. }
  98. }
  99. private void UpdateCondVarList()
  100. {
  101. lock (Process.ThreadArbiterListLock)
  102. {
  103. if (Process.ThreadArbiterListHead == null)
  104. {
  105. return;
  106. }
  107. //Remove itself from the list.
  108. bool Found;
  109. KThread CurrThread = Process.ThreadArbiterListHead;
  110. if (Found = (Process.ThreadArbiterListHead == this))
  111. {
  112. Process.ThreadArbiterListHead = Process.ThreadArbiterListHead.NextCondVarThread;
  113. }
  114. else
  115. {
  116. while (CurrThread.NextCondVarThread != null)
  117. {
  118. if (CurrThread.NextCondVarThread == this)
  119. {
  120. CurrThread.NextCondVarThread = NextCondVarThread;
  121. Found = true;
  122. break;
  123. }
  124. CurrThread = CurrThread.NextCondVarThread;
  125. }
  126. }
  127. if (!Found)
  128. {
  129. return;
  130. }
  131. //Re-add taking new priority into account.
  132. if (Process.ThreadArbiterListHead == null ||
  133. Process.ThreadArbiterListHead.ActualPriority > ActualPriority)
  134. {
  135. NextCondVarThread = Process.ThreadArbiterListHead;
  136. Process.ThreadArbiterListHead = this;
  137. return;
  138. }
  139. CurrThread = Process.ThreadArbiterListHead;
  140. while (CurrThread.NextCondVarThread != null)
  141. {
  142. if (CurrThread.NextCondVarThread.ActualPriority > ActualPriority)
  143. {
  144. break;
  145. }
  146. CurrThread = CurrThread.NextCondVarThread;
  147. }
  148. NextCondVarThread = CurrThread.NextCondVarThread;
  149. CurrThread.NextCondVarThread = this;
  150. }
  151. }
  152. }
  153. }