aboutsummaryrefslogtreecommitdiff
path: root/ChocolArm64
diff options
context:
space:
mode:
authorgdkchan <gab.dark.100@gmail.com>2018-03-12 01:04:52 -0300
committergdkchan <gab.dark.100@gmail.com>2018-03-12 01:14:12 -0300
commit7a27990faa557c5c93f52e5cb082d551ad119ed0 (patch)
treea0800fded014a4a6afe738e5a65a17bc78cf0c19 /ChocolArm64
parent3aaa4717b6f7400bac862e589a1f345e70e78d56 (diff)
Allow more than one process, free resources on process dispose, implement SvcExitThread
Diffstat (limited to 'ChocolArm64')
-rw-r--r--ChocolArm64/AThread.cs29
-rw-r--r--ChocolArm64/ATranslator.cs8
-rw-r--r--ChocolArm64/Instruction/AInstEmitException.cs16
-rw-r--r--ChocolArm64/State/AThreadState.cs2
4 files changed, 27 insertions, 28 deletions
diff --git a/ChocolArm64/AThread.cs b/ChocolArm64/AThread.cs
index cec26817..62f9d2d3 100644
--- a/ChocolArm64/AThread.cs
+++ b/ChocolArm64/AThread.cs
@@ -14,43 +14,30 @@ namespace ChocolArm64
private ATranslator Translator;
- private ThreadPriority Priority;
-
private Thread Work;
public event EventHandler WorkFinished;
public int ThreadId => ThreadState.ThreadId;
- public bool IsAlive => Work.IsAlive;
-
- private bool IsExecuting;
+ private int IsExecuting;
- private object ExecuteLock;
-
- public AThread(ATranslator Translator, AMemory Memory, ThreadPriority Priority, long EntryPoint)
+ public AThread(ATranslator Translator, AMemory Memory, long EntryPoint)
{
this.Translator = Translator;
this.Memory = Memory;
- this.Priority = Priority;
this.EntryPoint = EntryPoint;
ThreadState = new AThreadState();
- ExecuteLock = new object();
- }
- public void StopExecution() => Translator.StopExecution();
+ ThreadState.Running = true;
+ }
public bool Execute()
{
- lock (ExecuteLock)
+ if (Interlocked.Exchange(ref IsExecuting, 1) == 1)
{
- if (IsExecuting)
- {
- return false;
- }
-
- IsExecuting = true;
+ return false;
}
Work = new Thread(delegate()
@@ -62,11 +49,11 @@ namespace ChocolArm64
WorkFinished?.Invoke(this, EventArgs.Empty);
});
- Work.Priority = Priority;
-
Work.Start();
return true;
}
+
+ public void StopExecution() => ThreadState.Running = false;
}
} \ No newline at end of file
diff --git a/ChocolArm64/ATranslator.cs b/ChocolArm64/ATranslator.cs
index 1f4a922a..02c18efd 100644
--- a/ChocolArm64/ATranslator.cs
+++ b/ChocolArm64/ATranslator.cs
@@ -22,8 +22,6 @@ namespace ChocolArm64
public bool EnableCpuTrace { get; set; }
- private bool KeepRunning;
-
public ATranslator(IReadOnlyDictionary<long, string> SymbolTable = null)
{
SubBlocks = new HashSet<long>();
@@ -38,12 +36,8 @@ namespace ChocolArm64
{
this.SymbolTable = new ConcurrentDictionary<long, string>();
}
-
- KeepRunning = true;
}
- internal void StopExecution() => KeepRunning = false;
-
internal void ExecuteSubroutine(AThread Thread, long Position)
{
do
@@ -70,7 +64,7 @@ namespace ChocolArm64
Position = Sub.Execute(Thread.ThreadState, Thread.Memory);
}
- while (Position != 0 && KeepRunning);
+ while (Position != 0 && Thread.ThreadState.Running);
}
internal bool TryGetCachedSub(AOpCode OpCode, out ATranslatedSub Sub)
diff --git a/ChocolArm64/Instruction/AInstEmitException.cs b/ChocolArm64/Instruction/AInstEmitException.cs
index fe348edd..3964c949 100644
--- a/ChocolArm64/Instruction/AInstEmitException.cs
+++ b/ChocolArm64/Instruction/AInstEmitException.cs
@@ -34,6 +34,22 @@ namespace ChocolArm64.Instruction
Context.EmitCall(MthdInfo);
+ //Check if the thread should still be running, if it isn't then we return 0
+ //to force a return to the dispatcher and then exit the thread.
+ Context.EmitLdarg(ATranslatedSub.StateArgIdx);
+
+ Context.EmitCallPropGet(typeof(AThreadState), nameof(AThreadState.Running));
+
+ AILLabel LblEnd = new AILLabel();
+
+ Context.Emit(OpCodes.Brtrue_S, LblEnd);
+
+ Context.EmitLdc_I8(0);
+
+ Context.Emit(OpCodes.Ret);
+
+ Context.MarkLabel(LblEnd);
+
if (Context.CurrBlock.Next != null)
{
Context.EmitLoadState(Context.CurrBlock.Next);
diff --git a/ChocolArm64/State/AThreadState.cs b/ChocolArm64/State/AThreadState.cs
index d86f5bf9..ec8621b8 100644
--- a/ChocolArm64/State/AThreadState.cs
+++ b/ChocolArm64/State/AThreadState.cs
@@ -29,6 +29,8 @@ namespace ChocolArm64.State
public int ProcessId;
public int ThreadId;
+ public bool Running { get; set; }
+
public long TpidrEl0 { get; set; }
public long Tpidr { get; set; }