From 156ae3efab116baea17c42536ee832dbc41223c8 Mon Sep 17 00:00:00 2001 From: Aiken Harris Date: Wed, 17 Jun 2026 20:56:05 +0200 Subject: [PATCH] Refactor thread ready state transitions into Scheduler class --- xtoskrnl/includes/ke/kthread.hh | 2 -- xtoskrnl/includes/ke/schedule.hh | 2 ++ xtoskrnl/ke/kthread.cc | 60 -------------------------------- xtoskrnl/ke/schedule.cc | 60 ++++++++++++++++++++++++++++++++ 4 files changed, 62 insertions(+), 62 deletions(-) diff --git a/xtoskrnl/includes/ke/kthread.hh b/xtoskrnl/includes/ke/kthread.hh index 241d384..4f724a4 100644 --- a/xtoskrnl/includes/ke/kthread.hh +++ b/xtoskrnl/includes/ke/kthread.hh @@ -22,7 +22,6 @@ namespace KE public: STATIC XTAPI VOID AttachThread(IN PKTHREAD Thread); - STATIC XTAPI VOID DispatchReadyThread(IN PKTHREAD Thread); STATIC XTAPI PETHREAD GetInitialThread(VOID); STATIC XTAPI XTSTATUS InitializeIdleThread(IN PKPROCESS IdleProcess, IN OUT PKTHREAD IdleThread, @@ -37,7 +36,6 @@ namespace KE IN PVOID EnvironmentBlock, IN PVOID Stack, IN BOOLEAN AttachToProcess); - STATIC XTAPI VOID ReadyThread(IN PKTHREAD Thread); private: STATIC XTAPI VOID HandleSystemThreadExit(VOID); diff --git a/xtoskrnl/includes/ke/schedule.hh b/xtoskrnl/includes/ke/schedule.hh index de05c1e..f71f63f 100644 --- a/xtoskrnl/includes/ke/schedule.hh +++ b/xtoskrnl/includes/ke/schedule.hh @@ -19,6 +19,8 @@ namespace KE { public: STATIC XTFASTCALL VOID InsertDeferredReadyList(IN PKTHREAD Thread); + STATIC XTAPI VOID ProcessReadyThread(IN PKTHREAD Thread); + STATIC XTAPI VOID ReadyThread(IN PKTHREAD Thread); }; } diff --git a/xtoskrnl/ke/kthread.cc b/xtoskrnl/ke/kthread.cc index 79640f9..984484c 100644 --- a/xtoskrnl/ke/kthread.cc +++ b/xtoskrnl/ke/kthread.cc @@ -75,44 +75,6 @@ KE::KThread::AttachThread(IN OUT PKTHREAD Thread) } } -/** - * Transitions a thread to the ready state and queues it for execution within the dispatcher database. - * - * @param Thread - * Supplies a pointer to the thread to be dispatched. - * - * @return This routine does not return any value. - * - * @since XT 1.0 - */ -XTAPI -VOID -KE::KThread::DispatchReadyThread(IN PKTHREAD Thread) -{ - PKPROCESS Process; - - /* Extract the process from the thread's APC state */ - Process = Thread->ApcState.Process; - - /* Verify if the process and thread are resident in physical memory */ - if(Process->State != ProcessInMemory) - { - /* Process is swapped out, place the thread in a transition state */ - Thread->State = Transition; - } - else if(!Thread->KernelStackResident) - { - /* Increment the active stack count and transition the thread */ - Process->StackCount++; - Thread->State = Transition; - } - else - { - /* Both process and thread's stack are fully resident, queue the thread for execution */ - KE::Scheduler::InsertDeferredReadyList(Thread); - } -} - /** * Retrieves a pointer to the system's initial executive thread object. * @@ -385,28 +347,6 @@ KE::KThread::InitializeThread(IN PKPROCESS Process, return STATUS_SUCCESS; } -/** - * Prepares a thread for execution by safely acquiring the dispatcher database lock. - * - * @param Thread - * Supplies a pointer to the thread to be readied. - * - * @return This routine does not return any value. - * - * @since XT 1.0 - */ -XTAPI -VOID -KE::KThread::ReadyThread(IN PKTHREAD Thread) -{ - /* Raise runlevel to SYNC level and acquire the dispatcher database lock */ - KE::RaiseRunLevel RunLevel(SYNC_LEVEL); - KE::QueuedSpinLockGuard DispatcherGuard(DispatcherLock); - - /* Evaluate residency and queue the thread */ - DispatchReadyThread(Thread); -} - /** * Suspend APC-built thread NOP routine. It takes no actions. * diff --git a/xtoskrnl/ke/schedule.cc b/xtoskrnl/ke/schedule.cc index 64deb6b..c9b5ae1 100644 --- a/xtoskrnl/ke/schedule.cc +++ b/xtoskrnl/ke/schedule.cc @@ -35,3 +35,63 @@ KE::Scheduler::InsertDeferredReadyList(IN PKTHREAD Thread) /* Insert the thread into the deferred ready list */ RTL::LifoQueue::PushEntryList(&Prcb->DeferredReadyListHead, &Thread->SwapListEntry); } + +/** + * Transitions a thread to the ready state and queues it for execution within the dispatcher database. + * + * @param Thread + * Supplies a pointer to the thread to be dispatched. + * + * @return This routine does not return any value. + * + * @since XT 1.0 + */ +XTAPI +VOID +KE::Scheduler::ProcessReadyThread(IN PKTHREAD Thread) +{ + PKPROCESS Process; + + /* Extract the process from the thread's APC state */ + Process = Thread->ApcState.Process; + + /* Verify if the process and thread are resident in physical memory */ + if(Process->State != ProcessInMemory) + { + /* Process is swapped out, place the thread in a transition state */ + Thread->State = Transition; + } + else if(!Thread->KernelStackResident) + { + /* Increment the active stack count and transition the thread */ + Process->StackCount++; + Thread->State = Transition; + } + else + { + /* Both process and thread's stack are fully resident, queue the thread for execution */ + InsertDeferredReadyList(Thread); + } +} + +/** + * Prepares a thread for execution by safely acquiring the dispatcher database lock. + * + * @param Thread + * Supplies a pointer to the thread to be readied. + * + * @return This routine does not return any value. + * + * @since XT 1.0 + */ +XTAPI +VOID +KE::Scheduler::ReadyThread(IN PKTHREAD Thread) +{ + /* Raise runlevel to SYNC level and acquire the dispatcher database lock */ + KE::RaiseRunLevel RunLevel(SYNC_LEVEL); + KE::QueuedSpinLockGuard DispatcherGuard(DispatcherLock); + + /* Evaluate residency and queue the thread */ + ProcessReadyThread(Thread); +}