AThread.cs 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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.Name = "cpu_thread_" + Work.ManagedThreadId;
  36. Work.Start();
  37. return true;
  38. }
  39. public void StopExecution()
  40. {
  41. ThreadState.Running = false;
  42. }
  43. public void RequestInterrupt()
  44. {
  45. ThreadState.RequestInterrupt();
  46. }
  47. public bool IsCurrentThread()
  48. {
  49. return Thread.CurrentThread == Work;
  50. }
  51. }
  52. }