From 1fca2400a4bdb7a40663ad3369146520fcb903c7 Mon Sep 17 00:00:00 2001 From: Aiken Harris Date: Wed, 17 Jun 2026 16:30:28 +0200 Subject: [PATCH] Add functions to transition threads to deferred ready or transition states --- xtoskrnl/includes/ke/kthread.hh | 2 ++ xtoskrnl/ke/kthread.cc | 60 +++++++++++++++++++++++++++++++++ 2 files changed, 62 insertions(+) diff --git a/xtoskrnl/includes/ke/kthread.hh b/xtoskrnl/includes/ke/kthread.hh index 4f724a4..241d384 100644 --- a/xtoskrnl/includes/ke/kthread.hh +++ b/xtoskrnl/includes/ke/kthread.hh @@ -22,6 +22,7 @@ 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, @@ -36,6 +37,7 @@ 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/ke/kthread.cc b/xtoskrnl/ke/kthread.cc index 69565f4..7cc6182 100644 --- a/xtoskrnl/ke/kthread.cc +++ b/xtoskrnl/ke/kthread.cc @@ -75,6 +75,44 @@ 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. * @@ -346,6 +384,28 @@ 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. *