From bfaef2778234ee2f53e2b2c786349cce1c3bd000 Mon Sep 17 00:00:00 2001 From: Phillip Susi Date: Sat, 18 Dec 1999 07:33:53 +0000 Subject: [PATCH] Added tracking of running threads in a process, and termination of process when all threads terminate svn path=/trunk/; revision=888 --- reactos/ntoskrnl/ps/create.c | 4 ++-- reactos/ntoskrnl/ps/process.c | 2 ++ reactos/ntoskrnl/ps/thread.c | 18 ++++++++++++++++-- 3 files changed, 20 insertions(+), 4 deletions(-) diff --git a/reactos/ntoskrnl/ps/create.c b/reactos/ntoskrnl/ps/create.c index d0211501792..519dc5d9bdd 100644 --- a/reactos/ntoskrnl/ps/create.c +++ b/reactos/ntoskrnl/ps/create.c @@ -1,4 +1,4 @@ -/* $Id: create.c,v 1.2 1999/12/14 18:35:19 phreak Exp $ +/* $Id: create.c,v 1.3 1999/12/18 07:33:53 phreak Exp $ * * COPYRIGHT: See COPYING in the top level directory * PROJECT: ReactOS kernel @@ -150,7 +150,7 @@ NTSTATUS PsInitializeThread(HANDLE ProcessHandle, Thread->ThreadsProcess = Process; KeInitializeDpc( &Thread->Tcb.TimerDpc, PiTimeoutThread, Thread ); Thread->Tcb.WaitBlockList = NULL; - + InsertTailList( &Thread->ThreadsProcess->Pcb.ThreadListHead, &Thread->Tcb.ProcessThreadListEntry ); KeInitializeDispatcherHeader(&Thread->Tcb.DispatcherHeader, InternalThreadType, sizeof(ETHREAD), diff --git a/reactos/ntoskrnl/ps/process.c b/reactos/ntoskrnl/ps/process.c index 6623d3678a2..3068c5c461a 100644 --- a/reactos/ntoskrnl/ps/process.c +++ b/reactos/ntoskrnl/ps/process.c @@ -117,6 +117,7 @@ VOID PsInitProcessManagment(VOID) KeAcquireSpinLock(&PsProcessListLock, &oldIrql); InsertHeadList(&PsProcessListHead, &KProcess->ProcessListEntry); + InitializeListHead( &KProcess->ThreadListHead ); KeReleaseSpinLock(&PsProcessListLock, oldIrql); ObCreateHandle(SystemProcess, @@ -278,6 +279,7 @@ NtCreateProcess ( KeAcquireSpinLock(&PsProcessListLock, &oldIrql); InsertHeadList(&PsProcessListHead, &KProcess->ProcessListEntry); + InitializeListHead( &KProcess->ThreadListHead ); KeReleaseSpinLock(&PsProcessListLock, oldIrql); Status = PsCreatePeb (*ProcessHandle); diff --git a/reactos/ntoskrnl/ps/thread.c b/reactos/ntoskrnl/ps/thread.c index 9580d2e4a12..265721a82c9 100644 --- a/reactos/ntoskrnl/ps/thread.c +++ b/reactos/ntoskrnl/ps/thread.c @@ -1,4 +1,4 @@ -/* $Id: thread.c,v 1.42 1999/12/16 22:59:03 phreak Exp $ +/* $Id: thread.c,v 1.43 1999/12/18 07:33:53 phreak Exp $ * * COPYRIGHT: See COPYING in the top level directory * PROJECT: ReactOS kernel @@ -161,11 +161,25 @@ VOID PsReapThreads(VOID) if (current->Tcb.State == THREAD_STATE_TERMINATED_1) { + PEPROCESS Process = current->ThreadsProcess; + NTSTATUS Status = current->ExitStatus; + + ObReferenceObjectByPointer( Process, 0, PsProcessType, KernelMode ); DPRINT("Reaping thread %x\n", current); current->Tcb.State = THREAD_STATE_TERMINATED_2; - KeReleaseSpinLock(&PiThreadListLock, oldIrql); + RemoveEntryList( ¤t->Tcb.ProcessThreadListEntry ); + KeReleaseSpinLock(&PiThreadListLock, oldIrql); ObDereferenceObject(current); KeAcquireSpinLock(&PiThreadListLock, &oldIrql); + if( IsListEmpty( &Process->Pcb.ThreadListHead ) ) + { + //TODO: Optimize this so it doesnt jerk the IRQL around so much :) + DPRINT( "Last thread terminated, terminating process\n" ); + KeReleaseSpinLock(&PiThreadListLock, oldIrql); + PiTerminateProcess( Process, Status ); + KeAcquireSpinLock(&PiThreadListLock, &oldIrql); + } + ObDereferenceObject( Process ); current_entry = PiThreadListHead.Flink; } }