From 8de2868281a9159c7ff48ab9a43c9e893324181e Mon Sep 17 00:00:00 2001 From: Armin Novak Date: Tue, 15 Jul 2014 12:42:53 +0200 Subject: [PATCH] Preferring exit code from ExitThread now over thread function return. --- winpr/libwinpr/thread/thread.c | 14 +++++++++----- winpr/libwinpr/thread/thread.h | 3 ++- 2 files changed, 11 insertions(+), 6 deletions(-) diff --git a/winpr/libwinpr/thread/thread.c b/winpr/libwinpr/thread/thread.c index 1a09af1..79fca51 100644 --- a/winpr/libwinpr/thread/thread.c +++ b/winpr/libwinpr/thread/thread.c @@ -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); } diff --git a/winpr/libwinpr/thread/thread.h b/winpr/libwinpr/thread/thread.h index ea7c533..a6f0c3c 100644 --- a/winpr/libwinpr/thread/thread.h +++ b/winpr/libwinpr/thread/thread.h @@ -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; -- 2.7.4