2 * COPYRIGHT: See COPYING in the top level directory
3 * PROJECT: ReactOS kernel
4 * FILE: ntoskrnl/ps/kill.c
5 * PURPOSE: Terminating a thread
6 * PROGRAMMER: David Welch (welch@cwcom.net)
11 /* INCLUDES *****************************************************************/
13 #include <ddk/ntddk.h>
14 #include <internal/ps.h>
15 #include <internal/ke.h>
16 #include <internal/mm.h>
17 #include <internal/ob.h>
20 #include <internal/debug.h>
22 /* GLOBALS *******************************************************************/
24 extern ULONG PiNrThreads
;
26 /* FUNCTIONS *****************************************************************/
28 VOID
PsTerminateCurrentThread(NTSTATUS ExitStatus
)
30 * FUNCTION: Terminates the current thread
34 PETHREAD CurrentThread
;
38 CurrentThread
= PsGetCurrentThread();
40 CurrentThread
->ExitStatus
= ExitStatus
;
42 DPRINT("terminating %x\n",CurrentThread
);
43 ObDereferenceObject(CurrentThread
->ThreadsProcess
);
44 CurrentThread
->ThreadsProcess
= NULL
;
45 KeRaiseIrql(DISPATCH_LEVEL
,&oldlvl
);
46 CurrentThread
->Tcb
.State
= THREAD_STATE_TERMINATED
;
47 CurrentThread
->Tcb
.DispatcherHeader
.SignalState
= TRUE
;
48 KeDispatcherObjectWake(&CurrentThread
->Tcb
.DispatcherHeader
);
53 VOID
PsTerminateOtherThread(PETHREAD Thread
, NTSTATUS ExitStatus
)
55 * FUNCTION: Terminate a thread when calling from that thread's context
61 KeRaiseIrql(DISPATCH_LEVEL
, &oldlvl
);
62 Thread
->Tcb
.State
= THREAD_STATE_TERMINATED
;
63 Thread
->Tcb
.DispatcherHeader
.SignalState
= TRUE
;
64 KeDispatcherObjectWake(&Thread
->Tcb
.DispatcherHeader
);
65 ObDereferenceObject(Thread
->ThreadsProcess
);
66 Thread
->ThreadsProcess
= NULL
;
74 IN HANDLE ProcessHandle
,
75 IN NTSTATUS ExitStatus
82 DPRINT("NtTerminateProcess(ProcessHandle %x, ExitStatus %x)\n",
83 ProcessHandle
, ExitStatus
);
85 Status
= ObReferenceObjectByHandle(ProcessHandle
,
91 if (Status
!= STATUS_SUCCESS
)
96 PiTerminateProcessThreads(Process
, ExitStatus
);
97 KeRaiseIrql(DISPATCH_LEVEL
, &oldlvl
);
98 Process
->Pcb
.ProcessState
= PROCESS_STATE_TERMINATED
;
99 Process
->Pcb
.DispatcherHeader
.SignalState
= TRUE
;
100 KeDispatcherObjectWake(&Process
->Pcb
.DispatcherHeader
);
101 if (PsGetCurrentThread()->ThreadsProcess
== Process
)
104 ObDereferenceObject(Process
);
105 PsTerminateCurrentThread(ExitStatus
);
108 ObDereferenceObject(Process
);
109 return(STATUS_SUCCESS
);
116 IN HANDLE ThreadHandle
,
117 IN NTSTATUS ExitStatus
123 Status
= ObReferenceObjectByHandle(ThreadHandle
,
129 if (Status
!= STATUS_SUCCESS
)
134 if (Thread
== PsGetCurrentThread())
136 PsTerminateCurrentThread(ExitStatus
);
140 PsTerminateOtherThread(Thread
, ExitStatus
);
142 return(STATUS_SUCCESS
);
145 VOID
PsReleaseThread(PETHREAD Thread
)
147 * FUNCTION: Called internally to release a thread's resources from another
150 * Thread = Thread to release
153 DPRINT("PsReleaseThread(Thread %x)\n",Thread
);
155 RemoveEntryList(&Thread
->Tcb
.Entry
);
156 HalReleaseTask(Thread
);
157 ObDereferenceObject(Thread
);
161 NTSTATUS
PsTerminateSystemThread(NTSTATUS ExitStatus
)
163 * FUNCTION: Terminates the current thread
165 * ExitStatus = Status to pass to the creater
169 PsTerminateCurrentThread(ExitStatus
);
170 return(STATUS_SUCCESS
);
176 NtRegisterThreadTerminatePort (
177 HANDLE TerminationPort