- PspExitThread(ExitStatus);
-
- /* we should never reach this point! */
- KEBUGCHECK(0);
- }
- else
- {
- /* unlock and dereference the process so the threads can kill themselves */
- PsUnlockProcess(Process);
- ObDereferenceObject(Process);
+ /* Only master thread remains... kill it off */
+ if (CurrentThread->ThreadsProcess == Process) {
+
+ /* mark our thread as terminating so attempts to terminate it, when
+ unlocking the process, fail */
+ CurrentThread->HasTerminated = TRUE;
+
+ PsUnlockProcess(Process);
+
+ /* we can safely dereference the process because the current thread
+ holds a reference to it until it gets reaped */
+ ObDereferenceObject(Process);
+
+ /* now the other threads get a chance to terminate, we don't wait but
+ just kill ourselves right now. The process will be run down when the
+ last thread terminates */
+
+ PspExitThread(ExitStatus);
+
+ /* we should never reach this point! */
+ KEBUGCHECK(0);
+ }