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