KThread.cs 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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. 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. int OldPriority = ActualPriority;
  44. int CurrPriority = WantedPriority;
  45. lock (Process.ThreadSyncLock)
  46. {
  47. foreach (KThread Thread in MutexWaiters)
  48. {
  49. if (CurrPriority > Thread.WantedPriority)
  50. {
  51. CurrPriority = Thread.WantedPriority;
  52. }
  53. }
  54. }
  55. if (CurrPriority != OldPriority)
  56. {
  57. ActualPriority = CurrPriority;
  58. Process.Scheduler.Resort(this);
  59. MutexOwner?.UpdatePriority();
  60. }
  61. }
  62. }
  63. }