Preferring exit code from ExitThread now over thread function return.
authorArmin Novak <armin.novak@thincast.com>
Tue, 15 Jul 2014 10:42:53 +0000 (12:42 +0200)
committerArmin Novak <armin.novak@thincast.com>
Mon, 27 Oct 2014 10:23:16 +0000 (11:23 +0100)
winpr/libwinpr/thread/thread.c
winpr/libwinpr/thread/thread.h

index 1a09af1..79fca51 100644 (file)
@@ -248,7 +248,9 @@ static void *thread_launcher(void *arg)
 
 exit:
        set_event(thread);
-       thread->dwExitCode = (DWORD)(size_t)rc;
+
+       if (!thread->exited)
+               thread->dwExitCode = (DWORD)(size_t)rc;
 
        if (thread->detached || !thread->started)
                cleanup_handle(thread);
@@ -378,6 +380,7 @@ BOOL ThreadCloseHandle(HANDLE handle)
                }
                else
                        cleanup_handle(thread);
+
                ListDictionary_Unlock(thread_list);
 
                if (ListDictionary_Count(thread_list) < 1)
@@ -399,7 +402,6 @@ HANDLE CreateRemoteThread(HANDLE hProcess, LPSECURITY_ATTRIBUTES lpThreadAttribu
 
 VOID ExitThread(DWORD dwExitCode)
 {
-#if defined(WITH_DEBUG_THREADS) && defined(HAVE_EXECINFO_H)
        pthread_t tid = pthread_self();
 
        if (NULL == thread_list)
@@ -422,12 +424,13 @@ VOID ExitThread(DWORD dwExitCode)
                ListDictionary_Lock(thread_list);
                thread = ListDictionary_GetItemValue(thread_list, &tid);
                assert(thread);
+               thread->exited = TRUE;
+               thread->dwExitCode = dwExitCode;
+#if defined(WITH_DEBUG_THREADS) && defined(HAVE_EXECINFO_H)
                backtrace(thread->exit_stack, 20);
+#endif
                ListDictionary_Unlock(thread_list);
        }
-
-#endif
-       fprintf(stderr, "[%s] terminated...\n", __FUNCTION__);
 }
 
 BOOL GetExitCodeThread(HANDLE hThread, LPDWORD lpExitCode)
@@ -573,6 +576,7 @@ VOID DumpThreadHandles(void)
 
                if (keys)
                        free(keys);
+
                ListDictionary_Unlock(thread_list);
        }
 
index ea7c533..a6f0c3c 100644 (file)
@@ -28,7 +28,7 @@
 
 #include "../handle/handle.h"
 
-typedef void *(*pthread_start_routine)(void*);
+typedef void *(*pthread_start_routine)(void *);
 
 struct winpr_thread
 {
@@ -38,6 +38,7 @@ struct winpr_thread
        int pipe_fd[2];
        BOOL mainProcess;
        BOOL detached;
+       BOOL exited;
        DWORD dwExitCode;
        pthread_t thread;
        SIZE_T dwStackSize;