AThread.cs 1.6 KB

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