2018-02-04 23:08:20 +00:00
|
|
|
using ChocolArm64.Memory;
|
|
|
|
using ChocolArm64.State;
|
|
|
|
using System;
|
|
|
|
using System.Threading;
|
|
|
|
|
|
|
|
namespace ChocolArm64
|
|
|
|
{
|
2018-02-16 00:04:38 +00:00
|
|
|
public class AThread
|
2018-02-04 23:08:20 +00:00
|
|
|
{
|
2018-02-19 19:37:13 +00:00
|
|
|
public AThreadState ThreadState { get; private set; }
|
|
|
|
public AMemory Memory { get; private set; }
|
2018-02-04 23:08:20 +00:00
|
|
|
|
2018-03-10 02:12:57 +00:00
|
|
|
private long EntryPoint;
|
2018-02-14 02:43:08 +00:00
|
|
|
|
2018-02-04 23:08:20 +00:00
|
|
|
private ATranslator Translator;
|
2018-02-14 02:43:08 +00:00
|
|
|
|
|
|
|
private ThreadPriority Priority;
|
|
|
|
|
|
|
|
private Thread Work;
|
2018-02-04 23:08:20 +00:00
|
|
|
|
|
|
|
public event EventHandler WorkFinished;
|
|
|
|
|
2018-02-18 19:28:07 +00:00
|
|
|
public int ThreadId => ThreadState.ThreadId;
|
2018-02-04 23:08:20 +00:00
|
|
|
|
|
|
|
public bool IsAlive => Work.IsAlive;
|
|
|
|
|
2018-02-14 02:43:08 +00:00
|
|
|
private bool IsExecuting;
|
|
|
|
|
|
|
|
private object ExecuteLock;
|
2018-02-04 23:08:20 +00:00
|
|
|
|
2018-02-26 01:14:58 +00:00
|
|
|
public AThread(ATranslator Translator, AMemory Memory, ThreadPriority Priority, long EntryPoint)
|
2018-02-04 23:08:20 +00:00
|
|
|
{
|
2018-02-26 01:14:58 +00:00
|
|
|
this.Translator = Translator;
|
2018-02-04 23:08:20 +00:00
|
|
|
this.Memory = Memory;
|
|
|
|
this.Priority = Priority;
|
2018-02-14 02:43:08 +00:00
|
|
|
this.EntryPoint = EntryPoint;
|
2018-02-04 23:08:20 +00:00
|
|
|
|
2018-02-18 19:28:07 +00:00
|
|
|
ThreadState = new AThreadState();
|
2018-02-14 02:43:08 +00:00
|
|
|
ExecuteLock = new object();
|
2018-02-04 23:08:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public void StopExecution() => Translator.StopExecution();
|
|
|
|
|
2018-02-14 02:43:08 +00:00
|
|
|
public bool Execute()
|
2018-02-04 23:08:20 +00:00
|
|
|
{
|
2018-02-14 02:43:08 +00:00
|
|
|
lock (ExecuteLock)
|
|
|
|
{
|
|
|
|
if (IsExecuting)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
IsExecuting = true;
|
|
|
|
}
|
|
|
|
|
2018-02-04 23:08:20 +00:00
|
|
|
Work = new Thread(delegate()
|
|
|
|
{
|
2018-02-26 01:14:58 +00:00
|
|
|
Translator.ExecuteSubroutine(this, EntryPoint);
|
2018-02-04 23:08:20 +00:00
|
|
|
|
|
|
|
Memory.RemoveMonitor(ThreadId);
|
|
|
|
|
|
|
|
WorkFinished?.Invoke(this, EventArgs.Empty);
|
|
|
|
});
|
|
|
|
|
2018-02-14 02:43:08 +00:00
|
|
|
Work.Priority = Priority;
|
2018-02-04 23:08:20 +00:00
|
|
|
|
|
|
|
Work.Start();
|
2018-02-14 02:43:08 +00:00
|
|
|
|
|
|
|
return true;
|
2018-02-04 23:08:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|