public unsafe void SuspendThread(HleThread Thread) { Thread.SetStatus(HleThread.Status.Suspend); if (Thread == this.Current) { this.Yield(); } }
public HleThread Create() { var HlePspThread = new HleThread(PspEmulatorContext, new CpuThreadState(Processor)); HlePspThread.Id = LastId++; HlePspThread.Name = "Thread-" + HlePspThread.Id; HlePspThread.CurrentStatus = HleThread.Status.Stopped; Threads.Add(HlePspThread); return HlePspThread; }
public HleThread Create() { var HlePspThread = new HleThread(new CpuThreadState(Processor)); HlePspThread.Id = LastId++; HlePspThread.Name = "Thread-" + HlePspThread.Id; HlePspThread.CurrentStatus = HleThread.Status.Stopped; Threads.Add(HlePspThread); return(HlePspThread); }
private HleThread GetThreadById(int ThreadId) { HleThread HleThread = ThreadManager.GetThreadById(ThreadId); if (HleThread == null) { throw (new SceKernelException(SceKernelErrors.ERROR_KERNEL_NOT_FOUND_THREAD)); } return(HleThread); }
public int WaitThread(HleThread HleThread, WakeUpCallbackDelegate WakeUpCallback, int ExpectedMinimumCount) { WaitingSemaphoreThreadList.Add( new WaitingSemaphoreThread() { HleThread = HleThread, ExpectedMinimumCount = ExpectedMinimumCount, WakeUpCallback = WakeUpCallback, } ); return UpdatedCurrentCount(); }
public int WaitThread(HleThread HleThread, WakeUpCallbackDelegate WakeUpCallback, int ExpectedMinimumCount) { WaitingSemaphoreThreadList.Add( new WaitingSemaphoreThread() { HleThread = HleThread, ExpectedMinimumCount = ExpectedMinimumCount, WakeUpCallback = WakeUpCallback, } ); return(UpdatedCurrentCount()); }
public unsafe void DeleteThread(HleThread HleThread) { #if DEBUG_THREADS ConsoleUtils.SaveRestoreConsoleColor(ConsoleColor.Red, () => { Console.Error.WriteLine("DeleteThread: {0}", HleThread); Console.Error.WriteLine("{0}", Environment.StackTrace); }); #endif HleThread.Stack.DeallocateFromParent(); Threads.Remove(HleThread); ThreadsById.Remove(HleThread.Id); PreemptiveScheduler.Remove(HleThread); }
public HleThread Create() { var HlePspThread = new HleThread(InjectContext, new CpuThreadState(Processor)); HlePspThread.Id = LastId++; HlePspThread.Name = "Thread-" + HlePspThread.Id; HlePspThread.SetStatus(HleThread.Status.Stopped); //Console.Error.WriteLine("Created: {0}", HlePspThread); PreemptiveScheduler.Update(HlePspThread); Threads.Add(HlePspThread); ThreadsById[HlePspThread.Id] = HlePspThread; return(HlePspThread); }
/// <summary> /// Execute current thread steps until it can execute other thread. /// </summary> /// <param name="Current"></param> private void ExecuteCurrent(HleThread Current) { do { ExecuteQueuedCallbacks(); ExecuteQueuedInterrupts(); if (Current.HasAllStatus(HleThread.Status.Suspend)) { return; } Current.Step(); } while (DispatchingThreads == SCE_KERNEL_DISPATCHTHREAD_STATE.DISABLED); Current = null; }
public void ExitThread(HleThread HleThread, int ExitStatus) { #if DEBUG_THREADS ConsoleUtils.SaveRestoreConsoleColor(ConsoleColor.Red, () => { Console.Error.WriteLine("TerminateThread: {0}", HleThread); }); #endif HleThread.Info.ExitStatus = ExitStatus; HleThread.SetStatus(HleThread.Status.Killed); HleThread.Terminate(); if (HleThread == Current) { HleThread.CpuThreadState.Yield(); } }
private HleThread CalculateNext() { HleThread MinThread = null; //Console.Write("{0},", Threads.Count); foreach (var Thread in Threads) { if ((Thread.CurrentStatus == HleThread.Status.Ready) || Thread.IsWaitingAndHandlingCallbacks) { if (MinThread == null || Thread.PriorityValue < MinThread.PriorityValue) { MinThread = Thread; } } } return(MinThread); }
public void CpuThreadStateTest() { var HlePspThread = new HleThread(new CpuThreadState(Processor)); MipsAssembler.Assemble(@" .code 0x08000000 li r31, 0x08000000 jal end nop end: addi r1, r1, 1 jr r31 nop " ); HlePspThread.CpuThreadState.PC = 0x08000000; HlePspThread.Step(); }
public HleThread GetThreadById(int Id, bool AllowSelf = true) { //Debug.WriteLine(Threads.Count); if (Id == 0) { if (!AllowSelf) { throw new SceKernelException(SceKernelErrors.ERROR_KERNEL_ILLEGAL_THREAD); } return(Current); } HleThread HleThread = null; ThreadsById.TryGetValue(Id, out HleThread); if (HleThread == null) { throw new SceKernelException(SceKernelErrors.ERROR_KERNEL_NOT_FOUND_THREAD); } return(HleThread); }
public void CpuThreadStateBugTest() { var HlePspThread = new HleThread(new CpuThreadState(Processor)); MipsAssembler.Assemble(@" .code 0x08000000 li r31, 0x08000000 jal end nop end: jr r31 nop " ); HlePspThread.CpuThreadState.PC = 0x08000000; Assert.Inconclusive(); Console.WriteLine("1"); HlePspThread.Step(); Console.WriteLine("2"); }
/// <summary> /// Execute current thread steps until it can execute other thread. /// </summary> /// <param name="Current"></param> private void ExecuteCurrent(HleThread Current) { do { ExecuteQueuedCallbacks(); ExecuteQueuedInterrupts(); if (Current.HasAllStatus(HleThread.Status.Suspend)) return; Current.Step(); } while (DispatchingThreads == SCE_KERNEL_DISPATCHTHREAD_STATE.DISABLED); Current = null; }
public unsafe void DeleteThread(HleThread Thread) { ExitThread(Thread); }
public HleThread Create() { var HlePspThread = new HleThread(InjectContext, new CpuThreadState(Processor)); HlePspThread.Id = LastId++; HlePspThread.Name = "Thread-" + HlePspThread.Id; HlePspThread.SetStatus(HleThread.Status.Stopped); //Console.Error.WriteLine("Created: {0}", HlePspThread); PreemptiveScheduler.Update(HlePspThread); Threads.Add(HlePspThread); ThreadsById[HlePspThread.Id] = HlePspThread; return HlePspThread; }
public void UpdatedThread(HleThread HleThread) { PreemptiveScheduler.Update(HleThread); }
public void StepNext(Action DoBeforeSelectingNext) { //HleInterruptManager.EnableDisable(() => { //}; // Select the thread with the lowest PriorityValue var NextThread = CalculateNext(); #if DEBUG_THREADS if (NextThread != null) { Console.Error.WriteLine("+++++++++++++++++++++++++++++++++"); foreach (var Thread in Threads) { Console.Error.WriteLine(Thread); } Console.Error.WriteLine("NextThread: {0} : {1}", NextThread.Id, NextThread.PriorityValue); } #endif //Console.WriteLine("{0} -> {1}", String.Join(",", PreemptiveScheduler.GetThreadsInQueue().Select(Item => Item.Name)), (NextThread != null) ? NextThread.Name : "-"); ExecuteQueuedInterrupts(); ExecuteQueuedCallbacks(); lock (ChangeStatusActions) { if (ChangeStatusActions.Count > 0) { while (ChangeStatusActions.Count > 0) { var Action = ChangeStatusActions.Dequeue(); Action?.Invoke(); } NextThread = CalculateNext(); //if (NextThread == null) //{ // StepNext(DoBeforeSelectingNext); // return; //} } } // No thread found. if (NextThread == null) { if (DisplayConfig.VerticalSynchronization) { Thread.Sleep(1); } if (Threads.Count == 0) { Thread.Sleep(5); } return; } // Run that thread this.Current = NextThread; var CurrentCurrent = Current; { // Ready -> Running CurrentCurrent.SetStatus(HleThread.Status.Running); try { if (HleConfig.DebugThreadSwitching) { ConsoleUtils.SaveRestoreConsoleState(() => { Console.ForegroundColor = ConsoleColor.Yellow; Console.WriteLine("Execute: {0} : PC: 0x{1:X}", CurrentCurrent, CurrentCurrent.CpuThreadState.Pc); }); } DoBeforeSelectingNext?.Invoke(); ExecuteCurrent(CurrentCurrent); } finally { // Running -> Ready if (CurrentCurrent.HasAllStatus(HleThread.Status.Running)) { CurrentCurrent.SetStatus(HleThread.Status.Ready); } } } this.Current = null; }
public void ScheduleNext(HleThread ThreadToSchedule) { ThreadToSchedule.PriorityValue = Threads.Min(Thread => Thread.PriorityValue) - 1; Reschedule(); //Console.WriteLine("!ScheduleNext: "); }
public void ExitThread(HleThread HlePspThread) { Threads.Remove(HlePspThread); }
public unsafe void DeleteThread(HleThread Thread) { Thread.Stack.DeallocateFromParent(); ExitThread(Thread); }
public unsafe void ResumeThread(HleThread Thread) { Thread.SetStatus(HleThread.Status.Ready); }
public void StepNext() { MustReschedule = false; //HleInterruptManager.EnableDisable(() => { //}); if (Threads.Count > 0) { HleInterruptManager.ExecuteQueued(Threads.First().CpuThreadState); } // Select the thread with the lowest PriorityValue var NextThread = CalculateNext(); //Console.WriteLine("NextThread: {0} : {1}", NextThread.Id, NextThread.PriorityValue); // No thread found. if (NextThread == null) { if (Processor.PspConfig.VerticalSynchronization) { Thread.Sleep(1); } return; } // Run that thread Current = NextThread; { // Waiting, but listeing to callbacks. if (Current.IsWaitingAndHandlingCallbacks) { HleCallbackManager.ExecuteQueued(Current.CpuThreadState, MustReschedule); } // Executing normally. else { Current.CurrentStatus = HleThread.Status.Running; try { if (Processor.PspConfig.DebugThreadSwitching) { ConsoleUtils.SaveRestoreConsoleState(() => { Console.ForegroundColor = ConsoleColor.Yellow; Console.WriteLine("Execute: {0} : PC: 0x{1:X}", Current, Current.CpuThreadState.PC); }); } Current.Step(); } finally { if (Current.CurrentStatus == HleThread.Status.Running) { Current.CurrentStatus = HleThread.Status.Ready; } } } } Current = null; // Decrement all threads by that PriorityValue. int DecrementValue = NextThread.PriorityValue; foreach (var Thread in Threads) { //Console.WriteLine(Thread.PriorityValue); Thread.PriorityValue -= DecrementValue; } // Increment. NextThread.PriorityValue += NextThread.Info.PriorityCurrent + 1; }
public void TerminateThread(HleThread HleThread) { ExitThread(HleThread, -1); }
public void StepNext() { MustReschedule = false; //HleInterruptManager.EnableDisable(() => { //}; #if !DISABLE_CALLBACKS if (Threads.Count > 0) { HleInterruptManager.ExecuteQueued(Threads.First().CpuThreadState); } #endif // Select the thread with the lowest PriorityValue var NextThread = CalculateNext(); //Console.WriteLine("NextThread: {0} : {1}", NextThread.Id, NextThread.PriorityValue); // No thread found. if (NextThread == null) { if (Processor.PspConfig.VerticalSynchronization) { Thread.Sleep(1); } if (Threads.Count == 0) { Thread.Sleep(5); } return; } // Run that thread Current = NextThread; { // Waiting, but listeing to callbacks. if (Current.IsWaitingAndHandlingCallbacks) { /* if (Processor.PspConfig.VerticalSynchronization) { Thread.Sleep(1); } */ #if !DISABLE_CALLBACKS HleCallbackManager.ExecuteQueued(Current.CpuThreadState, MustReschedule); HleInterop.ExecuteAllQueuedFunctionsNow(); #endif } // Executing normally. else { //throw (new Exception("aaaaaaaaaaaa")); Current.CurrentStatus = HleThread.Status.Running; try { if (Processor.PspConfig.DebugThreadSwitching) { ConsoleUtils.SaveRestoreConsoleState(() => { Console.ForegroundColor = ConsoleColor.Yellow; Console.WriteLine("Execute: {0} : PC: 0x{1:X}", Current, Current.CpuThreadState.PC); }); } Current.Step(); } finally { if (Current.CurrentStatus == HleThread.Status.Running) { Current.CurrentStatus = HleThread.Status.Ready; } } } } Current = null; // Decrement all threads by that PriorityValue. int DecrementValue = NextThread.PriorityValue; foreach (var Thread in Threads) { //Console.WriteLine(Thread.PriorityValue); Thread.PriorityValue -= DecrementValue; } // Increment. NextThread.PriorityValue += NextThread.Info.PriorityCurrent + 1; }
public void StepNext(Action DoBeforeSelectingNext) { //HleInterruptManager.EnableDisable(() => { //}; // Select the thread with the lowest PriorityValue var NextThread = CalculateNext(); #if DEBUG_THREADS if (NextThread != null) { Console.Error.WriteLine("+++++++++++++++++++++++++++++++++"); foreach (var Thread in Threads) { Console.Error.WriteLine(Thread); } Console.Error.WriteLine("NextThread: {0} : {1}", NextThread.Id, NextThread.PriorityValue); } #endif //Console.WriteLine("{0} -> {1}", String.Join(",", PreemptiveScheduler.GetThreadsInQueue().Select(Item => Item.Name)), (NextThread != null) ? NextThread.Name : "-"); ExecuteQueuedInterrupts(); ExecuteQueuedCallbacks(); lock (ChangeStatusActions) { if (ChangeStatusActions.Count > 0) { while (ChangeStatusActions.Count > 0) { var Action = ChangeStatusActions.Dequeue(); if (Action != null) Action(); } NextThread = CalculateNext(); //if (NextThread == null) //{ // StepNext(DoBeforeSelectingNext); // return; //} } } // No thread found. if (NextThread == null) { if (DisplayConfig.VerticalSynchronization) { Thread.Sleep(1); } if (Threads.Count == 0) { Thread.Sleep(5); } return; } // Run that thread this.Current = NextThread; var CurrentCurrent = Current; { // Ready -> Running CurrentCurrent.SetStatus(HleThread.Status.Running); try { if (HleConfig.DebugThreadSwitching) { ConsoleUtils.SaveRestoreConsoleState(() => { Console.ForegroundColor = ConsoleColor.Yellow; Console.WriteLine("Execute: {0} : PC: 0x{1:X}", CurrentCurrent, CurrentCurrent.CpuThreadState.PC); }); } if (DoBeforeSelectingNext != null) DoBeforeSelectingNext(); ExecuteCurrent(CurrentCurrent); } finally { // Running -> Ready if (CurrentCurrent.HasAllStatus(HleThread.Status.Running)) { CurrentCurrent.SetStatus(HleThread.Status.Ready); } } } this.Current = null; }