[PATCH] [NtDll-Kernel32]: thread/process exit/terminate

Eric Pouech eric.pouech at wanadoo.fr
Wed Jun 14 14:19:33 CDT 2006


- rewrote the lot of thread/process exit/terminate in
  kernel32 to only rely on ntdll stuff

A+
---

 dlls/kernel/process.c |    3 ++-
 dlls/kernel/thread.c  |   23 ++++++++-------------
 dlls/ntdll/thread.c   |   54 +++++++++++++++++++++++++++++++------------------
 3 files changed, 45 insertions(+), 35 deletions(-)

diff --git a/dlls/kernel/process.c b/dlls/kernel/process.c
index 4a9d01e..966a9f1 100644
--- a/dlls/kernel/process.c
+++ b/dlls/kernel/process.c
@@ -1962,7 +1962,8 @@ void WINAPI ExitProcess( DWORD status )
 {
     LdrShutdownProcess();
     NtTerminateProcess(GetCurrentProcess(), status);
-    exit(status);
+    /* shouldn't return */
+    assert(0);
 }
 
 
diff --git a/dlls/kernel/thread.c b/dlls/kernel/thread.c
index bf29aac..2796bb1 100644
--- a/dlls/kernel/thread.c
+++ b/dlls/kernel/thread.c
@@ -39,7 +39,6 @@ #include "thread.h"
 #include "wine/winbase16.h"
 #include "wine/exception.h"
 #include "wine/library.h"
-#include "wine/server.h"
 #include "wine/debug.h"
 
 #include "kernel_private.h"
@@ -200,23 +199,19 @@ HANDLE WINAPI OpenThread( DWORD dwDesire
  */
 void WINAPI ExitThread( DWORD code ) /* [in] Exit code for this thread */
 {
-    BOOL last;
-    SERVER_START_REQ( terminate_thread )
-    {
-        /* send the exit code to the server */
-        req->handle    = GetCurrentThread();
-        req->exit_code = code;
-        wine_server_call( req );
-        last = reply->last;
-    }
-    SERVER_END_REQ;
+    BOOLEAN     last;
+    NTSTATUS    status;
 
-    if (last)
+    status = NtQueryInformationThread( GetCurrentThread(), ThreadAmILastThread,
+                                       &last, sizeof(last), NULL );
+    if (status == STATUS_SUCCESS && last)
     {
-        LdrShutdownProcess();
-        exit( code );
+        ExitProcess( code );
+        /* shouldn't return */
     }
     else RtlExitUserThread( code );
+    /* shouldn't return either */
+    assert(0);
 }
 
 
diff --git a/dlls/ntdll/thread.c b/dlls/ntdll/thread.c
index 7f82a88..7187969 100644
--- a/dlls/ntdll/thread.c
+++ b/dlls/ntdll/thread.c
@@ -29,6 +29,7 @@ #ifdef HAVE_SYS_TIMES_H
 #include <sys/times.h>
 #endif
 
+#include <assert.h>
 #define NONAMELESSUNION
 #include "ntstatus.h"
 #define WIN32_NO_STATUS
@@ -358,6 +359,35 @@ static void start_thread( struct wine_pt
 }
 
 
+/******************************************************************
+ *		terminate_thread
+ *
+ */
+static NTSTATUS terminate_thread( HANDLE handle, DWORD exit_code, void (*term_func)(int) )
+{
+    NTSTATUS ret;
+    BOOL self, last;
+
+    SERVER_START_REQ( terminate_thread )
+    {
+        req->handle    = handle;
+        req->exit_code = exit_code;
+        ret = wine_server_call( req );
+        self = !ret && reply->self;
+        last = reply->last;
+    }
+    SERVER_END_REQ;
+
+    if (self)
+    {
+        if (last) exit( exit_code );
+        else term_func( exit_code );
+        /* should never be here */
+        assert(0);
+    }
+    return ret;
+}
+
 /***********************************************************************
  *              RtlCreateUserThread   (NTDLL.@)
  */
@@ -483,7 +513,9 @@ error:
 void WINAPI RtlExitUserThread( ULONG status )
 {
     LdrShutdownThread();
-    server_exit_thread( status );
+    terminate_thread( GetCurrentThread(), status, server_exit_thread );
+    /* should never be here, but this prevents GCC from barking */
+    exit(status);
 }
 
 
@@ -573,25 +605,7 @@ NTSTATUS WINAPI NtAlertThread( HANDLE ha
  */
 NTSTATUS WINAPI NtTerminateThread( HANDLE handle, LONG exit_code )
 {
-    NTSTATUS ret;
-    BOOL self, last;
-
-    SERVER_START_REQ( terminate_thread )
-    {
-        req->handle    = handle;
-        req->exit_code = exit_code;
-        ret = wine_server_call( req );
-        self = !ret && reply->self;
-        last = reply->last;
-    }
-    SERVER_END_REQ;
-
-    if (self)
-    {
-        if (last) exit( exit_code );
-        else server_abort_thread( exit_code );
-    }
-    return ret;
+    return terminate_thread( handle, exit_code, server_abort_thread );
 }
 
 



More information about the wine-patches mailing list