KThread.cs 2.4 KB

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