Пример #1
0
        public void StartThread(KThread Thread)
        {
            lock (SchedLock)
            {
                SchedulerThread SchedThread = new SchedulerThread(Thread);

                if (!AllThreads.TryAdd(Thread, SchedThread))
                {
                    return;
                }

                if (ActiveProcessors.Add(Thread.ProcessorId))
                {
                    Thread.Thread.Execute();

                    PrintDbgThreadInfo(Thread, "running.");
                }
                else
                {
                    WaitingToRun[Thread.ProcessorId].Push(SchedThread);

                    PrintDbgThreadInfo(Thread, "waiting to run.");
                }
            }
        }
Пример #2
0
 public bool HasThread(SchedulerThread SchedThread)
 {
     lock (Threads)
     {
         return(Threads.Contains(SchedThread));
     }
 }
Пример #3
0
        private void TryResumingExecution(SchedulerThread SchedThread)
        {
            KThread Thread = SchedThread.Thread;

            PrintDbgThreadInfo(Thread, "trying to resume...");

            SchedThread.WaitActivity.WaitOne();

            lock (SchedLock)
            {
                if (ActiveProcessors.Add(Thread.ProcessorId))
                {
                    PrintDbgThreadInfo(Thread, "resuming execution...");

                    return;
                }

                WaitingToRun[Thread.ProcessorId].Push(SchedThread);

                PrintDbgThreadInfo(Thread, "entering wait state...");
            }

            SchedThread.WaitSched.WaitOne();

            PrintDbgThreadInfo(Thread, "resuming execution...");
        }
Пример #4
0
        public void RemoveThread(KThread Thread)
        {
            PrintDbgThreadInfo(Thread, "exited.");

            lock (SchedLock)
            {
                if (AllThreads.TryRemove(Thread, out SchedulerThread SchedThread))
                {
                    WaitingToRun.Remove(SchedThread);

                    SchedThread.Dispose();
                }

                int ActualCore = Thread.ActualCore;

                SchedulerThread NewThread = WaitingToRun.Pop(ActualCore);

                if (NewThread == null)
                {
                    Log.PrintDebug(LogClass.KernelScheduler, $"Nothing to run on core {ActualCore}!");

                    CoreThreads[ActualCore] = null;

                    return;
                }

                NewThread.Thread.ActualCore = ActualCore;

                RunThread(NewThread);
            }
        }
Пример #5
0
 public bool Remove(SchedulerThread SchedThread)
 {
     lock (Threads)
     {
         return(Threads.Remove(SchedThread));
     }
 }
Пример #6
0
        public void Suspend(KThread Thread)
        {
            lock (SchedLock)
            {
                PrintDbgThreadInfo(Thread, "suspended.");

                int ActualCore = Thread.ActualCore;

                CoreReschedule[ActualCore] = false;

                SchedulerThread SchedThread = WaitingToRun.Pop(ActualCore);

                if (SchedThread != null)
                {
                    SchedThread.Thread.ActualCore = ActualCore;

                    CoreThreads[ActualCore] = SchedThread.Thread;

                    RunThread(SchedThread);
                }
                else
                {
                    Log.PrintDebug(LogClass.KernelScheduler, $"Nothing to run on core {Thread.ActualCore}!");

                    CoreThreads[ActualCore] = null;
                }
            }
        }
Пример #7
0
        public void StartThread(KThread Thread)
        {
            lock (SchedLock)
            {
                SchedulerThread SchedThread = new SchedulerThread(Thread);

                if (!AllThreads.TryAdd(Thread, SchedThread))
                {
                    return;
                }

                if (!ActiveProcessors.Contains(Thread.ProcessorId))
                {
                    ActiveProcessors.Add(Thread.ProcessorId);

                    Thread.Thread.Execute();

                    Logging.Debug($"{GetDbgThreadInfo(Thread)} running.");
                }
                else
                {
                    WaitingToRun[Thread.ProcessorId].Push(SchedThread);

                    Logging.Debug($"{GetDbgThreadInfo(SchedThread.Thread)} waiting to run.");
                }
            }
        }
Пример #8
0
        private void TryResumingExecution(SchedulerThread SchedThread)
        {
            KThread Thread = SchedThread.Thread;

            if (SchedThread.Active)
            {
                lock (SchedLock)
                {
                    if (ActiveProcessors.Add(Thread.ProcessorId))
                    {
                        PrintDbgThreadInfo(Thread, "resuming execution...");

                        return;
                    }

                    WaitingToRun[Thread.ProcessorId].Push(SchedThread);

                    PrintDbgThreadInfo(Thread, "entering wait state...");
                }
            }
            else
            {
                PrintDbgThreadInfo(Thread, "entering inactive wait state...");
            }

            SchedThread.WaitEvent.WaitOne();

            PrintDbgThreadInfo(Thread, "resuming execution...");
        }
Пример #9
0
 public void Push(SchedulerThread Thread)
 {
     lock (Threads)
     {
         Threads.Add(Thread);
     }
 }
Пример #10
0
        public void Yield(KThread Thread)
        {
            PrintDbgThreadInfo(Thread, "yielded execution.");

            if (IsActive(Thread))
            {
                lock (SchedLock)
                {
                    SchedulerThread SchedThread = WaitingToRun[Thread.ProcessorId].Pop(Thread.ActualPriority);

                    if (SchedThread == null)
                    {
                        PrintDbgThreadInfo(Thread, "resumed because theres nothing better to run.");

                        return;
                    }

                    if (SchedThread != null)
                    {
                        RunThread(SchedThread);
                    }
                }
            }
            else
            {
                //Just stop running the thread if it's not active,
                //and run whatever is waiting to run with the higuest priority.
                Suspend(Thread.ProcessorId);
            }

            Resume(Thread);
        }
Пример #11
0
        public void RemoveThread(KThread Thread)
        {
            PrintDbgThreadInfo(Thread, "exited.");

            lock (SchedLock)
            {
                if (AllThreads.TryRemove(Thread, out SchedulerThread SchedThread))
                {
                    WaitingToRun[Thread.ProcessorId].Remove(SchedThread);

                    SchedThread.Dispose();
                }

                SchedulerThread NewThread = WaitingToRun[Thread.ProcessorId].Pop();

                if (NewThread == null)
                {
                    Log.PrintDebug(LogClass.KernelScheduler, $"Nothing to run on core {Thread.ProcessorId}!");

                    ActiveProcessors.Remove(Thread.ProcessorId);

                    return;
                }

                RunThread(NewThread);
            }
        }
Пример #12
0
        public void EnterWait(KThread Thread, int TimeoutMs = Timeout.Infinite)
        {
            SchedulerThread SchedThread = AllThreads[Thread];

            Suspend(Thread);

            SchedThread.WaitSync.WaitOne(TimeoutMs);

            TryResumingExecution(SchedThread);
        }
Пример #13
0
 private void UpdateSyncWaitEvent(SchedulerThread SchedThread)
 {
     if (SchedThread.Active && SchedThread.SyncTimeout == 0)
     {
         SchedThread.SyncWaitEvent.Set();
     }
     else
     {
         SchedThread.SyncWaitEvent.Reset();
     }
 }
Пример #14
0
 private void RunThread(SchedulerThread SchedThread)
 {
     if (!SchedThread.Thread.Thread.Execute())
     {
         SchedThread.WaitEvent.Set();
     }
     else
     {
         Logging.Debug($"{GetDbgThreadInfo(SchedThread.Thread)} running.");
     }
 }
Пример #15
0
 private void RunThread(SchedulerThread SchedThread)
 {
     if (!SchedThread.Thread.Thread.Execute())
     {
         SchedThread.WaitSched.Set();
     }
     else
     {
         PrintDbgThreadInfo(SchedThread.Thread, "running.");
     }
 }
Пример #16
0
        private bool WaitIfNeeded(SchedulerThread SchedThread)
        {
            KThread Thread = SchedThread.Thread;

            if (!IsActive(SchedThread) && Thread.Thread.IsCurrentThread())
            {
                Suspend(Thread.ProcessorId);

                return(Resume(Thread));
            }
            else
            {
                return(false);
            }
        }
Пример #17
0
        public void Suspend(int ProcessorId)
        {
            lock (SchedLock)
            {
                SchedulerThread SchedThread = WaitingToRun[ProcessorId].Pop();

                if (SchedThread != null)
                {
                    RunThread(SchedThread);
                }
                else
                {
                    ActiveProcessors.Remove(ProcessorId);
                }
            }
        }
Пример #18
0
        public void Suspend(int ProcessorId)
        {
            lock (SchedLock)
            {
                SchedulerThread SchedThread = WaitingToRun[ProcessorId].Pop();

                if (SchedThread != null)
                {
                    RunThread(SchedThread);
                }
                else
                {
                    Log.PrintDebug(LogClass.KernelScheduler, $"Nothing to run on core {ProcessorId}!");

                    ActiveProcessors.Remove(ProcessorId);
                }
            }
        }
Пример #19
0
        public void Yield(KThread Thread)
        {
            PrintDbgThreadInfo(Thread, "yielded execution.");

            lock (SchedLock)
            {
                SchedulerThread SchedThread = WaitingToRun[Thread.ProcessorId].Pop(Thread.Priority);

                if (SchedThread == null)
                {
                    PrintDbgThreadInfo(Thread, "resumed because theres nothing better to run.");

                    return;
                }

                RunThread(SchedThread);
            }

            Resume(Thread);
        }
Пример #20
0
        private void TryResumingExecution(SchedulerThread SchedThread)
        {
            KThread Thread = SchedThread.Thread;

            lock (SchedLock)
            {
                if (ActiveProcessors.Add(Thread.ProcessorId))
                {
                    Logging.Debug($"{GetDbgThreadInfo(Thread)} resuming execution...");

                    return;
                }

                WaitingToRun[Thread.ProcessorId].Push(SchedThread);
            }

            SchedThread.WaitEvent.WaitOne();

            Logging.Debug($"{GetDbgThreadInfo(Thread)} resuming execution...");
        }
Пример #21
0
        public void Reschedule(KThread Thread)
        {
            bool NeedsReschedule;

            lock (SchedLock)
            {
                int ActualCore = Thread.ActualCore;

                NeedsReschedule = CoreReschedule[ActualCore];

                CoreReschedule[ActualCore] = false;
            }

            if (NeedsReschedule)
            {
                PrintDbgThreadInfo(Thread, "yielded execution.");

                lock (SchedLock)
                {
                    int ActualCore = Thread.ActualCore;

                    SchedulerThread NewThread = WaitingToRun.Pop(ActualCore, Thread.ActualPriority);

                    if (NewThread == null)
                    {
                        PrintDbgThreadInfo(Thread, "resumed because theres nothing better to run.");

                        return;
                    }

                    NewThread.Thread.ActualCore = ActualCore;

                    CoreThreads[ActualCore] = NewThread.Thread;

                    RunThread(NewThread);
                }

                Resume(Thread);
            }
        }
Пример #22
0
        private bool TryResumingExecution(SchedulerThread SchedThread)
        {
            KThread Thread = SchedThread.Thread;

            if (!SchedThread.Active || SchedThread.SyncTimeout != 0)
            {
                PrintDbgThreadInfo(Thread, "entering inactive wait state...");
            }

            bool Result = false;

            if (SchedThread.SyncTimeout != 0)
            {
                Result = SchedThread.SyncWaitEvent.WaitOne(SchedThread.SyncTimeout);

                SchedThread.SyncTimeout = 0;
            }

            lock (SchedLock)
            {
                if (ActiveProcessors.Add(Thread.ProcessorId))
                {
                    PrintDbgThreadInfo(Thread, "resuming execution...");

                    return(Result);
                }

                WaitingToRun[Thread.ProcessorId].Push(SchedThread);

                PrintDbgThreadInfo(Thread, "entering wait state...");
            }

            SchedThread.SchedWaitEvent.WaitOne();

            PrintDbgThreadInfo(Thread, "resuming execution...");

            return(Result);
        }
Пример #23
0
 private bool IsActive(SchedulerThread SchedThread)
 {
     return(SchedThread.Active && SchedThread.SyncTimeout == 0);
 }