Exemplo n.º 1
0
 public unsafe void SuspendThread(HleThread Thread)
 {
     Thread.SetStatus(HleThread.Status.Suspend);
     if (Thread == this.Current)
     {
         this.Yield();
     }
 }
Exemplo n.º 2
0
 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;
 }
Exemplo n.º 3
0
        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);
        }
Exemplo n.º 4
0
        private HleThread GetThreadById(int ThreadId)
        {
            HleThread HleThread = ThreadManager.GetThreadById(ThreadId);

            if (HleThread == null)
            {
                throw (new SceKernelException(SceKernelErrors.ERROR_KERNEL_NOT_FOUND_THREAD));
            }
            return(HleThread);
        }
Exemplo n.º 5
0
        public int WaitThread(HleThread HleThread, WakeUpCallbackDelegate WakeUpCallback, int ExpectedMinimumCount)
        {
            WaitingSemaphoreThreadList.Add(
                new WaitingSemaphoreThread()
                {
                    HleThread = HleThread,
                    ExpectedMinimumCount = ExpectedMinimumCount,
                    WakeUpCallback = WakeUpCallback,
                }
            );

            return UpdatedCurrentCount();
        }
Exemplo n.º 6
0
        public int WaitThread(HleThread HleThread, WakeUpCallbackDelegate WakeUpCallback, int ExpectedMinimumCount)
        {
            WaitingSemaphoreThreadList.Add(
                new WaitingSemaphoreThread()
            {
                HleThread            = HleThread,
                ExpectedMinimumCount = ExpectedMinimumCount,
                WakeUpCallback       = WakeUpCallback,
            }
                );

            return(UpdatedCurrentCount());
        }
Exemplo n.º 7
0
        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);
        }
Exemplo n.º 8
0
        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);
        }
Exemplo n.º 9
0
        /// <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;
        }
Exemplo n.º 10
0
        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();
            }
        }
Exemplo n.º 11
0
        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);
        }
Exemplo n.º 12
0
        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();
        }
Exemplo n.º 13
0
        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);
        }
Exemplo n.º 14
0
        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");
        }
Exemplo n.º 15
0
        /// <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;
        }
Exemplo n.º 16
0
 public unsafe void DeleteThread(HleThread Thread)
 {
     ExitThread(Thread);
 }
Exemplo n.º 17
0
        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;
        }
Exemplo n.º 18
0
 public void UpdatedThread(HleThread HleThread)
 {
     PreemptiveScheduler.Update(HleThread);
 }
Exemplo n.º 19
0
        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;
        }
Exemplo n.º 20
0
 public void ScheduleNext(HleThread ThreadToSchedule)
 {
     ThreadToSchedule.PriorityValue = Threads.Min(Thread => Thread.PriorityValue) - 1;
     Reschedule();
     //Console.WriteLine("!ScheduleNext: ");
 }
Exemplo n.º 21
0
 public void ExitThread(HleThread HlePspThread)
 {
     Threads.Remove(HlePspThread);
 }
Exemplo n.º 22
0
 public void UpdatedThread(HleThread HleThread)
 {
     PreemptiveScheduler.Update(HleThread);
 }
Exemplo n.º 23
0
 public unsafe void DeleteThread(HleThread Thread)
 {
     Thread.Stack.DeallocateFromParent();
     ExitThread(Thread);
 }
Exemplo n.º 24
0
 public unsafe void ResumeThread(HleThread Thread)
 {
     Thread.SetStatus(HleThread.Status.Ready);
 }
Exemplo n.º 25
0
 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();
     }
 }
Exemplo n.º 26
0
 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);
 }
Exemplo n.º 27
0
 public unsafe void DeleteThread(HleThread Thread)
 {
     ExitThread(Thread);
 }
Exemplo n.º 28
0
        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;
        }
Exemplo n.º 29
0
 public void TerminateThread(HleThread HleThread)
 {
     ExitThread(HleThread, -1);
 }
Exemplo n.º 30
0
 public void ExitThread(HleThread HlePspThread)
 {
     Threads.Remove(HlePspThread);
 }
Exemplo n.º 31
0
 public void ScheduleNext(HleThread ThreadToSchedule)
 {
     ThreadToSchedule.PriorityValue = Threads.Min(Thread => Thread.PriorityValue) - 1;
     Reschedule();
     //Console.WriteLine("!ScheduleNext: ");
 }
Exemplo n.º 32
0
 public unsafe void SuspendThread(HleThread Thread)
 {
     Thread.SetStatus(HleThread.Status.Suspend);
     if (Thread == this.Current)
     {
         this.Yield();
     }
 }
Exemplo n.º 33
0
 public unsafe void ResumeThread(HleThread Thread)
 {
     Thread.SetStatus(HleThread.Status.Ready);
 }
Exemplo n.º 34
0
        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;
        }
Exemplo n.º 35
0
        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;
        }
Exemplo n.º 36
0
 public void TerminateThread(HleThread HleThread)
 {
     ExitThread(HleThread, -1);
 }