AThread.cs 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. using ChocolArm64.Memory;
  2. using ChocolArm64.State;
  3. using System;
  4. using System.Threading;
  5. namespace ChocolArm64
  6. {
  7. public class AThread
  8. {
  9. public AThreadState ThreadState { get; private set; }
  10. public AMemory Memory { get; private set; }
  11. private ATranslator Translator;
  12. public Thread Work;
  13. public event EventHandler WorkFinished;
  14. private int IsExecuting;
  15. public AThread(ATranslator Translator, AMemory Memory, long EntryPoint)
  16. {
  17. this.Translator = Translator;
  18. this.Memory = Memory;
  19. ThreadState = new AThreadState();
  20. ThreadState.ExecutionMode = AExecutionMode.AArch64;
  21. ThreadState.Running = true;
  22. Work = new Thread(delegate()
  23. {
  24. Translator.ExecuteSubroutine(this, EntryPoint);
  25. Memory.RemoveMonitor(ThreadState.Core);
  26. WorkFinished?.Invoke(this, EventArgs.Empty);
  27. });
  28. }
  29. public bool Execute()
  30. {
  31. if (Interlocked.Exchange(ref IsExecuting, 1) == 1)
  32. {
  33. return false;
  34. }
  35. Work.Start();
  36. return true;
  37. }
  38. public void StopExecution()
  39. {
  40. ThreadState.Running = false;
  41. }
  42. public void RequestInterrupt()
  43. {
  44. ThreadState.RequestInterrupt();
  45. }
  46. public bool IsCurrentThread()
  47. {
  48. return Thread.CurrentThread == Work;
  49. }
  50. }
  51. }