CpuThread.cs 1.5 KB

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