Refactor thread initialization to attach to process

This commit is contained in:
2026-06-17 12:15:32 +02:00
parent ffcb2dbeda
commit 6560ca9b96
2 changed files with 72 additions and 22 deletions

View File

@@ -21,6 +21,7 @@ namespace KE
STATIC ETHREAD InitialThread; STATIC ETHREAD InitialThread;
public: public:
STATIC XTAPI VOID AttachThread(IN PKTHREAD Thread);
STATIC XTAPI PETHREAD GetInitialThread(VOID); STATIC XTAPI PETHREAD GetInitialThread(VOID);
STATIC XTAPI XTSTATUS InitializeIdleThread(IN PKPROCESS IdleProcess, STATIC XTAPI XTSTATUS InitializeIdleThread(IN PKPROCESS IdleProcess,
IN OUT PKTHREAD IdleThread, IN OUT PKTHREAD IdleThread,
@@ -34,8 +35,7 @@ namespace KE
IN PCONTEXT Context, IN PCONTEXT Context,
IN PVOID EnvironmentBlock, IN PVOID EnvironmentBlock,
IN PVOID Stack, IN PVOID Stack,
IN BOOLEAN StartThread); IN BOOLEAN AttachToProcess);
STATIC XTAPI VOID StartThread(IN PKTHREAD Thread);
private: private:
STATIC XTAPI VOID HandleSystemThreadExit(VOID); STATIC XTAPI VOID HandleSystemThreadExit(VOID);

View File

@@ -9,6 +9,72 @@
#include <xtos.hh> #include <xtos.hh>
/**
* Finalizes thread initialization by inheriting parent process scheduling properties.
*
* @param Thread
* Supplies a pointer to the thread.
*
* @return This routine does not return any value.
*
* @since XT 1.0
*/
XTAPI
VOID
KE::KThread::AttachThread(IN OUT PKTHREAD Thread)
{
ULONG IdealProcessor;
PKPROCESS Process;
/* Extract the parent process from the thread's initial APC state */
Process = Thread->ApcState.Process;
/* Inherit scheduling properties from the parent process */
Thread->DisableBoost = Process->DisableBoost;
Thread->Iopl = Process->Iopl;
Thread->Quantum = Process->Quantum;
Thread->SystemAffinityActive = FALSE;
/* Acquire the process lock */
KE::SpinLockGuard ProcessGuard(&Process->ProcessLock);
/* Inherit priority from the parent process */
Thread->BasePriority = Process->BasePriority;
Thread->Priority = Process->BasePriority;
/* Inherit affinity state from the parent process */
KE::Affinity::CopyAffinity(Thread->Affinity, Process->Affinity);
KE::Affinity::CopyAffinity(Thread->UserAffinity, Process->Affinity);
/* Calculate the ideal processor based on the process thread seed and the affinity map */
IdealProcessor = KE::Affinity::FindNextRightSetProcessor(Process->ThreadSeed, Thread->Affinity);
/* Advance the thread seed for the next thread created in this process */
Process->ThreadSeed++;
/* Assign the selected ideal processor */
Thread->IdealProcessor = (UCHAR)IdealProcessor;
Thread->UserIdealProcessor = (UCHAR)IdealProcessor;
/* Acquire the dispatcher database lock */
KE::QueuedSpinLockGuard DispatcherGuard(DispatcherLock);
/* Insert the thread into the process's active thread list */
RTL::LinkedList::InsertTailList(&Process->ThreadListHead, &Thread->ThreadListEntry);
/* Handle edge cases where the stack count is uninitialized or explicitly maxed out */
if(Process->StackCount == MAXULONG_PTR)
{
/* Initialize the stack count for the first thread */
Process->StackCount = 1;
}
else
{
/* Increment the process stack count */
Process->StackCount++;
}
}
/** /**
* Retrieves a pointer to the system's initial executive thread object. * Retrieves a pointer to the system's initial executive thread object.
* *
@@ -149,7 +215,7 @@ KE::KThread::InitializeThread(IN PKPROCESS Process,
IN PCONTEXT Context, IN PCONTEXT Context,
IN PVOID EnvironmentBlock, IN PVOID EnvironmentBlock,
IN PVOID Stack, IN PVOID Stack,
IN BOOLEAN RunThread) IN BOOLEAN AttachToProcess)
{ {
PKWAIT_BLOCK TimerWaitBlock; PKWAIT_BLOCK TimerWaitBlock;
BOOLEAN Allocation; BOOLEAN Allocation;
@@ -270,33 +336,16 @@ KE::KThread::InitializeThread(IN PKPROCESS Process,
Thread->State = Initialized; Thread->State = Initialized;
/* Check if thread should be started */ /* Check if thread should be started */
if(RunThread) if(AttachToProcess)
{ {
/* Start thread */ /* Start thread */
StartThread(Thread); AttachThread(Thread);
} }
/* Return success */ /* Return success */
return STATUS_SUCCESS; return STATUS_SUCCESS;
} }
/**
* Starts the thread.
*
* @param Thread
* Supplies a pointer to the thread.
*
* @return This routine does not return any value.
*
* @since XT 1.0
*/
XTAPI
VOID
KE::KThread::StartThread(IN OUT PKTHREAD Thread)
{
UNIMPLEMENTED;
}
/** /**
* Suspend APC-built thread NOP routine. It takes no actions. * Suspend APC-built thread NOP routine. It takes no actions.
* *
@@ -383,4 +432,5 @@ XTAPI
VOID VOID
KE::KThread::SwitchToUserMode(VOID) KE::KThread::SwitchToUserMode(VOID)
{ {
UNIMPLEMENTED;
} }