KThread.cs 2.5 KB

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