AThread.cs 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. using ChocolArm64.Memory;
  2. using ChocolArm64.State;
  3. using System;
  4. using System.Threading;
  5. namespace ChocolArm64
  6. {
  7. class AThread
  8. {
  9. public ARegisters Registers { get; private set; }
  10. public AMemory Memory { get; private set; }
  11. private ATranslator Translator;
  12. private Thread Work;
  13. public event EventHandler WorkFinished;
  14. public int ThreadId => Registers.ThreadId;
  15. public bool IsAlive => Work.IsAlive;
  16. public long EntryPoint { get; private set; }
  17. public int Priority { get; private set; }
  18. public AThread(AMemory Memory, long EntryPoint = 0, int Priority = 0)
  19. {
  20. this.Memory = Memory;
  21. this.EntryPoint = EntryPoint;
  22. this.Priority = Priority;
  23. Registers = new ARegisters();
  24. Translator = new ATranslator(this);
  25. }
  26. public void StopExecution() => Translator.StopExecution();
  27. public void Execute() => Execute(EntryPoint);
  28. public void Execute(long EntryPoint)
  29. {
  30. Work = new Thread(delegate()
  31. {
  32. Translator.ExecuteSubroutine(EntryPoint);
  33. Memory.RemoveMonitor(ThreadId);
  34. WorkFinished?.Invoke(this, EventArgs.Empty);
  35. });
  36. if (Priority < 12)
  37. {
  38. Work.Priority = ThreadPriority.Highest;
  39. }
  40. else if (Priority < 24)
  41. {
  42. Work.Priority = ThreadPriority.AboveNormal;
  43. }
  44. else if (Priority < 36)
  45. {
  46. Work.Priority = ThreadPriority.Normal;
  47. }
  48. else if (Priority < 48)
  49. {
  50. Work.Priority = ThreadPriority.BelowNormal;
  51. }
  52. else
  53. {
  54. Work.Priority = ThreadPriority.Lowest;
  55. }
  56. Work.Start();
  57. }
  58. }
  59. }