2018-02-04 16:08:20 -07:00
|
|
|
using ChocolArm64.Memory;
|
|
|
|
using ChocolArm64.State;
|
|
|
|
using System;
|
|
|
|
using System.Threading;
|
|
|
|
|
|
|
|
namespace ChocolArm64
|
|
|
|
{
|
2018-02-15 17:04:38 -07:00
|
|
|
public class AThread
|
2018-02-04 16:08:20 -07:00
|
|
|
{
|
2018-02-19 12:37:13 -07:00
|
|
|
public AThreadState ThreadState { get; private set; }
|
|
|
|
public AMemory Memory { get; private set; }
|
2018-02-04 16:08:20 -07:00
|
|
|
|
|
|
|
private ATranslator Translator;
|
2018-02-13 19:43:08 -07:00
|
|
|
|
2018-09-18 17:36:43 -06:00
|
|
|
public Thread Work;
|
2018-02-04 16:08:20 -07:00
|
|
|
|
|
|
|
public event EventHandler WorkFinished;
|
|
|
|
|
2018-03-11 22:04:52 -06:00
|
|
|
private int IsExecuting;
|
2018-02-13 19:43:08 -07:00
|
|
|
|
2018-03-11 22:04:52 -06:00
|
|
|
public AThread(ATranslator Translator, AMemory Memory, long EntryPoint)
|
2018-02-04 16:08:20 -07:00
|
|
|
{
|
2018-02-25 18:14:58 -07:00
|
|
|
this.Translator = Translator;
|
2018-02-04 16:08:20 -07:00
|
|
|
this.Memory = Memory;
|
|
|
|
|
2018-02-18 12:28:07 -07:00
|
|
|
ThreadState = new AThreadState();
|
2018-02-04 16:08:20 -07:00
|
|
|
|
2018-05-26 14:49:21 -06:00
|
|
|
ThreadState.ExecutionMode = AExecutionMode.AArch64;
|
|
|
|
|
2018-03-11 22:04:52 -06:00
|
|
|
ThreadState.Running = true;
|
2018-09-18 17:36:43 -06:00
|
|
|
|
|
|
|
Work = new Thread(delegate()
|
|
|
|
{
|
|
|
|
Translator.ExecuteSubroutine(this, EntryPoint);
|
|
|
|
|
|
|
|
Memory.RemoveMonitor(ThreadState.Core);
|
|
|
|
|
|
|
|
WorkFinished?.Invoke(this, EventArgs.Empty);
|
|
|
|
});
|
2018-03-11 22:04:52 -06:00
|
|
|
}
|
2018-02-04 16:08:20 -07:00
|
|
|
|
2018-02-13 19:43:08 -07:00
|
|
|
public bool Execute()
|
2018-02-04 16:08:20 -07:00
|
|
|
{
|
2018-03-11 22:04:52 -06:00
|
|
|
if (Interlocked.Exchange(ref IsExecuting, 1) == 1)
|
2018-02-13 19:43:08 -07:00
|
|
|
{
|
2018-03-11 22:04:52 -06:00
|
|
|
return false;
|
2018-02-13 19:43:08 -07:00
|
|
|
}
|
|
|
|
|
2018-02-04 16:08:20 -07:00
|
|
|
Work.Start();
|
2018-02-13 19:43:08 -07:00
|
|
|
|
|
|
|
return true;
|
2018-02-04 16:08:20 -07:00
|
|
|
}
|
2018-03-11 22:04:52 -06:00
|
|
|
|
2018-04-19 13:18:30 -06:00
|
|
|
public void StopExecution()
|
|
|
|
{
|
|
|
|
ThreadState.Running = false;
|
|
|
|
}
|
|
|
|
|
2018-09-18 17:36:43 -06:00
|
|
|
public void RequestInterrupt()
|
|
|
|
{
|
|
|
|
ThreadState.RequestInterrupt();
|
|
|
|
}
|
|
|
|
|
2018-04-19 13:18:30 -06:00
|
|
|
public bool IsCurrentThread()
|
|
|
|
{
|
|
|
|
return Thread.CurrentThread == Work;
|
|
|
|
}
|
2018-02-04 16:08:20 -07:00
|
|
|
}
|
|
|
|
}
|