crt: test: Check event counter in t_tls_exe_dtor test for main process callback Verifies that callback registered via __tlregdtor for the main process thread (which is called after the main function returns) is not called more than once. Signed-off-by: LIU Hao <lh_mouse@126.com>
diff --git a/mingw-w64-crt/testcases/t_tls_exe_dtor.c b/mingw-w64-crt/testcases/t_tls_exe_dtor.c index 5c3ca76..5ced214 100644 --- a/mingw-w64-crt/testcases/t_tls_exe_dtor.c +++ b/mingw-w64-crt/testcases/t_tls_exe_dtor.c
@@ -11,11 +11,24 @@ /* mingw-w64 headers */ #include "libtest.h" -static int thread_detach = 0; +extern int __cdecl __tlregdtor(void (__cdecl*func)(void)); -static void __cdecl dtor_tls_callback(void) +static int thread_dtor_counter = 0; +static int process_dtor_counter = 0; + +static void __cdecl thread_dtor_callback(void) { - thread_detach++; + thread_dtor_counter++; +} + +static void __cdecl process_dtor_callback(void) +{ + if (process_dtor_counter != 0) { + printf("Error after main: dtor TLS callback on main process thread was called more times\n"); + /* exit, _exit, or ExitProcess calls TLS callbacks, so use TerminateProcess() which is not calling them */ + TerminateProcess(GetCurrentProcess(), 1); + } + process_dtor_counter++; } #if defined(__i386__) @@ -24,10 +37,11 @@ #endif static DWORD WINAPI thread_main(LPVOID user_data __attribute__((unused))) { - /* Register dtor_tls_callback as oneshot dtor TLS callback */ - extern int __cdecl __tlregdtor(void (__cdecl*func)(void)); - if (__tlregdtor(&dtor_tls_callback) != 0) - printf("Error: __tlregdtor() failed\n"); + /* Register thread_dtor_callback as oneshot dtor TLS callback */ + if (__tlregdtor(&thread_dtor_callback) != 0) { + printf("Error: __tlregdtor() on second thread failed\n"); + return 1; + } return 0; } @@ -62,6 +76,12 @@ return 77; } + /* Register process_dtor_callback as oneshot dtor TLS callback */ + if (__tlregdtor(&process_dtor_callback) != 0) { + printf("Error: __tlregdtor() on main process thread failed\n"); + ret = 1; + } + printf("Creating new thread\n"); HANDLE thread = CreateThread(NULL, 0, thread_main, NULL, 0, &(DWORD){0} /*out: ThreadId*/); if (!thread) { @@ -81,14 +101,19 @@ ret = 1; } - if (thread_detach != 1) { - printf("Error: dtor TLS callback for DLL_THREAD_DETACH was not called\n"); + if (thread_dtor_counter != 1) { + printf("Error: dtor TLS callback on second thread was not called\n"); ret = 1; } else { - printf("dtor TLS callback for DLL_THREAD_DETACH was called\n"); + printf("dtor TLS callback on second thread was called\n"); } } + if (process_dtor_counter != 0) { + printf("Error: dtor TLS callback on main process thread was called before exiting process\n"); + ret = 1; + } + if (ret) printf("FAILED\n"); else printf("PASSED\n"); return ret; }