KThread.cs 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. using ChocolArm64;
  2. using System.Collections.Generic;
  3. namespace Ryujinx.HLE.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. public bool CondVarSignaled { get; set; }
  12. private Process Process;
  13. public List<KThread> MutexWaiters { get; private set; }
  14. public KThread MutexOwner { get; set; }
  15. public int ActualPriority { get; private set; }
  16. public int WantedPriority { get; private set; }
  17. public int ActualCore { get; set; }
  18. public int ProcessorId { get; set; }
  19. public int IdealCore { get; set; }
  20. public int WaitHandle { get; set; }
  21. public long LastPc { get; set; }
  22. public int ThreadId => Thread.ThreadId;
  23. public KThread(
  24. AThread Thread,
  25. Process Process,
  26. int ProcessorId,
  27. int Priority)
  28. {
  29. this.Thread = Thread;
  30. this.Process = Process;
  31. this.ProcessorId = ProcessorId;
  32. this.IdealCore = ProcessorId;
  33. MutexWaiters = new List<KThread>();
  34. CoreMask = 1 << ProcessorId;
  35. ActualPriority = WantedPriority = Priority;
  36. }
  37. public void SetPriority(int Priority)
  38. {
  39. WantedPriority = Priority;
  40. UpdatePriority();
  41. }
  42. public void UpdatePriority()
  43. {
  44. bool PriorityChanged;
  45. lock (Process.ThreadSyncLock)
  46. {
  47. int OldPriority = ActualPriority;
  48. int CurrPriority = WantedPriority;
  49. foreach (KThread Thread in MutexWaiters)
  50. {
  51. int WantedPriority = Thread.WantedPriority;
  52. if (CurrPriority > WantedPriority)
  53. {
  54. CurrPriority = WantedPriority;
  55. }
  56. }
  57. PriorityChanged = CurrPriority != OldPriority;
  58. ActualPriority = CurrPriority;
  59. }
  60. if (PriorityChanged)
  61. {
  62. Process.Scheduler.Resort(this);
  63. MutexOwner?.UpdatePriority();
  64. }
  65. }
  66. }
  67. }