KThread.cs 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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 int ThreadId => Thread.ThreadId;
  22. public KThread(
  23. AThread Thread,
  24. Process Process,
  25. int ProcessorId,
  26. int Priority)
  27. {
  28. this.Thread = Thread;
  29. this.Process = Process;
  30. this.ProcessorId = ProcessorId;
  31. this.IdealCore = ProcessorId;
  32. MutexWaiters = new List<KThread>();
  33. CoreMask = 1 << ProcessorId;
  34. ActualPriority = WantedPriority = Priority;
  35. }
  36. public void SetPriority(int Priority)
  37. {
  38. WantedPriority = Priority;
  39. UpdatePriority();
  40. }
  41. public void UpdatePriority()
  42. {
  43. bool PriorityChanged;
  44. lock (Process.ThreadSyncLock)
  45. {
  46. int OldPriority = ActualPriority;
  47. int CurrPriority = WantedPriority;
  48. foreach (KThread Thread in MutexWaiters)
  49. {
  50. int WantedPriority = Thread.WantedPriority;
  51. if (CurrPriority > WantedPriority)
  52. {
  53. CurrPriority = WantedPriority;
  54. }
  55. }
  56. PriorityChanged = CurrPriority != OldPriority;
  57. ActualPriority = CurrPriority;
  58. }
  59. if (PriorityChanged)
  60. {
  61. Process.Scheduler.Resort(this);
  62. MutexOwner?.UpdatePriority();
  63. }
  64. }
  65. }
  66. }