Thread scheduler rewrite (#393)

* Started to rewrite the thread scheduler

* Add a single core-like scheduling mode, enabled by default

* Clear exclusive monitor on context switch

* Add SetThreadActivity, misc fixes

* Implement WaitForAddress and SignalToAddress svcs, misc fixes

* Misc fixes (on SetActivity and Arbiter), other tweaks

* Rebased

* Add missing null check

* Rename multicore key on config, fix UpdatePriorityInheritance

* Make scheduling data MLQs private

* nit: Ordering
This commit is contained in:
gdkchan 2018-09-18 20:36:43 -03:00 committed by GitHub
parent 193bf223ec
commit f07e1fa8af
4 changed files with 54 additions and 26 deletions

View file

@ -10,11 +10,9 @@ namespace ChocolArm64
public AThreadState ThreadState { get; private set; } public AThreadState ThreadState { get; private set; }
public AMemory Memory { get; private set; } public AMemory Memory { get; private set; }
private long EntryPoint;
private ATranslator Translator; private ATranslator Translator;
private Thread Work; public Thread Work;
public event EventHandler WorkFinished; public event EventHandler WorkFinished;
@ -24,13 +22,21 @@ namespace ChocolArm64
{ {
this.Translator = Translator; this.Translator = Translator;
this.Memory = Memory; this.Memory = Memory;
this.EntryPoint = EntryPoint;
ThreadState = new AThreadState(); ThreadState = new AThreadState();
ThreadState.ExecutionMode = AExecutionMode.AArch64; ThreadState.ExecutionMode = AExecutionMode.AArch64;
ThreadState.Running = true; ThreadState.Running = true;
Work = new Thread(delegate()
{
Translator.ExecuteSubroutine(this, EntryPoint);
Memory.RemoveMonitor(ThreadState.Core);
WorkFinished?.Invoke(this, EventArgs.Empty);
});
} }
public bool Execute() public bool Execute()
@ -40,14 +46,7 @@ namespace ChocolArm64
return false; return false;
} }
Work = new Thread(delegate() Work.Name = "cpu_thread_" + Work.ManagedThreadId;
{
Translator.ExecuteSubroutine(this, EntryPoint);
Memory.RemoveMonitor(ThreadState);
WorkFinished?.Invoke(this, EventArgs.Empty);
});
Work.Start(); Work.Start();
@ -59,6 +58,11 @@ namespace ChocolArm64
ThreadState.Running = false; ThreadState.Running = false;
} }
public void RequestInterrupt()
{
ThreadState.RequestInterrupt();
}
public bool IsCurrentThread() public bool IsCurrentThread()
{ {
return Thread.CurrentThread == Work; return Thread.CurrentThread == Work;

View file

@ -1,5 +1,6 @@
using ChocolArm64.Decoder; using ChocolArm64.Decoder;
using ChocolArm64.Memory; using ChocolArm64.Memory;
using ChocolArm64.State;
using ChocolArm64.Translation; using ChocolArm64.Translation;
using System; using System;
using System.Reflection.Emit; using System.Reflection.Emit;
@ -170,6 +171,8 @@ namespace ChocolArm64.Instruction
Context.EmitLdarg(ATranslatedSub.MemoryArgIdx); Context.EmitLdarg(ATranslatedSub.MemoryArgIdx);
Context.EmitLdarg(ATranslatedSub.StateArgIdx); Context.EmitLdarg(ATranslatedSub.StateArgIdx);
Context.EmitCallPropGet(typeof(AThreadState), nameof(AThreadState.Core));
if (Rn != -1) if (Rn != -1)
{ {
Context.EmitLdint(Rn); Context.EmitLdint(Rn);

View file

@ -41,7 +41,7 @@ namespace ChocolArm64.Memory
} }
} }
private Dictionary<AThreadState, ArmMonitor> Monitors; private Dictionary<int, ArmMonitor> Monitors;
private ConcurrentDictionary<long, IntPtr> ObservedPages; private ConcurrentDictionary<long, IntPtr> ObservedPages;
@ -53,7 +53,7 @@ namespace ChocolArm64.Memory
public AMemory(IntPtr Ram) public AMemory(IntPtr Ram)
{ {
Monitors = new Dictionary<AThreadState, ArmMonitor>(); Monitors = new Dictionary<int, ArmMonitor>();
ObservedPages = new ConcurrentDictionary<long, IntPtr>(); ObservedPages = new ConcurrentDictionary<long, IntPtr>();
@ -69,17 +69,17 @@ namespace ChocolArm64.Memory
} }
} }
public void RemoveMonitor(AThreadState State) public void RemoveMonitor(int Core)
{ {
lock (Monitors) lock (Monitors)
{ {
ClearExclusive(State); ClearExclusive(Core);
Monitors.Remove(State); Monitors.Remove(Core);
} }
} }
public void SetExclusive(AThreadState ThreadState, long Position) public void SetExclusive(int Core, long Position)
{ {
Position &= ~ErgMask; Position &= ~ErgMask;
@ -93,11 +93,11 @@ namespace ChocolArm64.Memory
} }
} }
if (!Monitors.TryGetValue(ThreadState, out ArmMonitor ThreadMon)) if (!Monitors.TryGetValue(Core, out ArmMonitor ThreadMon))
{ {
ThreadMon = new ArmMonitor(); ThreadMon = new ArmMonitor();
Monitors.Add(ThreadState, ThreadMon); Monitors.Add(Core, ThreadMon);
} }
ThreadMon.Position = Position; ThreadMon.Position = Position;
@ -105,7 +105,7 @@ namespace ChocolArm64.Memory
} }
} }
public bool TestExclusive(AThreadState ThreadState, long Position) public bool TestExclusive(int Core, long Position)
{ {
//Note: Any call to this method also should be followed by a //Note: Any call to this method also should be followed by a
//call to ClearExclusiveForStore if this method returns true. //call to ClearExclusiveForStore if this method returns true.
@ -113,7 +113,7 @@ namespace ChocolArm64.Memory
Monitor.Enter(Monitors); Monitor.Enter(Monitors);
if (!Monitors.TryGetValue(ThreadState, out ArmMonitor ThreadMon)) if (!Monitors.TryGetValue(Core, out ArmMonitor ThreadMon))
{ {
return false; return false;
} }
@ -128,9 +128,9 @@ namespace ChocolArm64.Memory
return ExState; return ExState;
} }
public void ClearExclusiveForStore(AThreadState ThreadState) public void ClearExclusiveForStore(int Core)
{ {
if (Monitors.TryGetValue(ThreadState, out ArmMonitor ThreadMon)) if (Monitors.TryGetValue(Core, out ArmMonitor ThreadMon))
{ {
ThreadMon.ExState = false; ThreadMon.ExState = false;
} }
@ -138,11 +138,11 @@ namespace ChocolArm64.Memory
Monitor.Exit(Monitors); Monitor.Exit(Monitors);
} }
public void ClearExclusive(AThreadState ThreadState) public void ClearExclusive(int Core)
{ {
lock (Monitors) lock (Monitors)
{ {
if (Monitors.TryGetValue(ThreadState, out ArmMonitor ThreadMon)) if (Monitors.TryGetValue(Core, out ArmMonitor ThreadMon))
{ {
ThreadMon.ExState = false; ThreadMon.ExState = false;
} }

View file

@ -41,6 +41,9 @@ namespace ChocolArm64.State
public bool Negative; public bool Negative;
public bool Running { get; set; } public bool Running { get; set; }
public int Core { get; set; }
private bool Interrupted;
public long TpidrEl0 { get; set; } public long TpidrEl0 { get; set; }
public long Tpidr { get; set; } public long Tpidr { get; set; }
@ -73,6 +76,7 @@ namespace ChocolArm64.State
} }
} }
public event EventHandler<EventArgs> Interrupt;
public event EventHandler<AInstExceptionEventArgs> Break; public event EventHandler<AInstExceptionEventArgs> Break;
public event EventHandler<AInstExceptionEventArgs> SvcCall; public event EventHandler<AInstExceptionEventArgs> SvcCall;
public event EventHandler<AInstUndefinedEventArgs> Undefined; public event EventHandler<AInstUndefinedEventArgs> Undefined;
@ -99,9 +103,26 @@ namespace ChocolArm64.State
internal bool Synchronize() internal bool Synchronize()
{ {
if (Interrupted)
{
Interrupted = false;
OnInterrupt();
}
return Running; return Running;
} }
internal void RequestInterrupt()
{
Interrupted = true;
}
private void OnInterrupt()
{
Interrupt?.Invoke(this, EventArgs.Empty);
}
internal void OnBreak(long Position, int Imm) internal void OnBreak(long Position, int Imm)
{ {
Break?.Invoke(this, new AInstExceptionEventArgs(Position, Imm)); Break?.Invoke(this, new AInstExceptionEventArgs(Position, Imm));