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 long EntryPoint;
  12. private ATranslator Translator;
  13. private Thread Work;
  14. public event EventHandler WorkFinished;
  15. public int ThreadId => ThreadState.ThreadId;
  16. private int IsExecuting;
  17. public AThread(ATranslator Translator, AMemory Memory, long EntryPoint)
  18. {
  19. this.Translator = Translator;
  20. this.Memory = Memory;
  21. this.EntryPoint = EntryPoint;
  22. ThreadState = new AThreadState();
  23. ThreadState.ExecutionMode = AExecutionMode.AArch64;
  24. ThreadState.Running = true;
  25. }
  26. public bool Execute()
  27. {
  28. if (Interlocked.Exchange(ref IsExecuting, 1) == 1)
  29. {
  30. return false;
  31. }
  32. Work = new Thread(delegate()
  33. {
  34. Translator.ExecuteSubroutine(this, EntryPoint);
  35. Memory.RemoveMonitor(ThreadState);
  36. WorkFinished?.Invoke(this, EventArgs.Empty);
  37. });
  38. Work.Start();
  39. return true;
  40. }
  41. public void StopExecution()
  42. {
  43. ThreadState.Running = false;
  44. }
  45. public bool IsCurrentThread()
  46. {
  47. return Thread.CurrentThread == Work;
  48. }
  49. }
  50. }