2018-02-20 21:09:23 +01:00
|
|
|
using ChocolArm64.Memory;
|
|
|
|
using ChocolArm64.State;
|
|
|
|
using System;
|
|
|
|
using System.Threading;
|
|
|
|
|
|
|
|
namespace ChocolArm64
|
|
|
|
{
|
|
|
|
public class AThread
|
|
|
|
{
|
|
|
|
public AThreadState ThreadState { get; private set; }
|
|
|
|
public AMemory Memory { get; private set; }
|
|
|
|
|
2018-03-10 03:12:57 +01:00
|
|
|
private long EntryPoint;
|
2018-02-20 21:09:23 +01:00
|
|
|
|
|
|
|
private ATranslator Translator;
|
|
|
|
|
|
|
|
private Thread Work;
|
|
|
|
|
|
|
|
public event EventHandler WorkFinished;
|
|
|
|
|
|
|
|
public int ThreadId => ThreadState.ThreadId;
|
|
|
|
|
2018-03-12 05:04:52 +01:00
|
|
|
private int IsExecuting;
|
2018-02-20 21:09:23 +01:00
|
|
|
|
2018-03-12 05:04:52 +01:00
|
|
|
public AThread(ATranslator Translator, AMemory Memory, long EntryPoint)
|
2018-02-20 21:09:23 +01:00
|
|
|
{
|
2018-02-26 02:14:58 +01:00
|
|
|
this.Translator = Translator;
|
2018-02-20 21:09:23 +01:00
|
|
|
this.Memory = Memory;
|
|
|
|
this.EntryPoint = EntryPoint;
|
|
|
|
|
|
|
|
ThreadState = new AThreadState();
|
|
|
|
|
2018-03-12 05:04:52 +01:00
|
|
|
ThreadState.Running = true;
|
|
|
|
}
|
2018-02-20 21:09:23 +01:00
|
|
|
|
|
|
|
public bool Execute()
|
|
|
|
{
|
2018-03-12 05:04:52 +01:00
|
|
|
if (Interlocked.Exchange(ref IsExecuting, 1) == 1)
|
2018-02-20 21:09:23 +01:00
|
|
|
{
|
2018-03-12 05:04:52 +01:00
|
|
|
return false;
|
2018-02-20 21:09:23 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
Work = new Thread(delegate()
|
|
|
|
{
|
2018-02-26 02:14:58 +01:00
|
|
|
Translator.ExecuteSubroutine(this, EntryPoint);
|
2018-02-20 21:09:23 +01:00
|
|
|
|
|
|
|
Memory.RemoveMonitor(ThreadId);
|
|
|
|
|
|
|
|
WorkFinished?.Invoke(this, EventArgs.Empty);
|
|
|
|
});
|
|
|
|
|
|
|
|
Work.Start();
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
2018-03-12 05:04:52 +01:00
|
|
|
|
|
|
|
public void StopExecution() => ThreadState.Running = false;
|
2018-02-20 21:09:23 +01:00
|
|
|
}
|
|
|
|
}
|