KThread.cs 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. using ChocolArm64;
  2. using System.Collections.Generic;
  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 List<KThread> MutexWaiters { get; private set; }
  13. public KThread MutexOwner { get; set; }
  14. public int ActualPriority { get; private set; }
  15. public int WantedPriority { get; private set; }
  16. public int ActualCore { get; set; }
  17. public int IdealCore { get; set; }
  18. public int WaitHandle { get; set; }
  19. public int ThreadId => Thread.ThreadId;
  20. public KThread(
  21. AThread Thread,
  22. Process Process,
  23. int IdealCore,
  24. int Priority)
  25. {
  26. this.Thread = Thread;
  27. this.Process = Process;
  28. this.IdealCore = IdealCore;
  29. MutexWaiters = new List<KThread>();
  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. lock (Process.ThreadSyncLock)
  43. {
  44. foreach (KThread Thread in MutexWaiters)
  45. {
  46. if (CurrPriority > Thread.WantedPriority)
  47. {
  48. CurrPriority = Thread.WantedPriority;
  49. }
  50. }
  51. }
  52. if (CurrPriority != OldPriority)
  53. {
  54. ActualPriority = CurrPriority;
  55. Process.Scheduler.Resort(this);
  56. MutexOwner?.UpdatePriority();
  57. }
  58. }
  59. }
  60. }