From d696d62fea48096680d6d511a71c4df56d00a51f Mon Sep 17 00:00:00 2001 From: Eli Zaretskii Date: Sat, 21 Nov 2015 19:44:02 +0200 Subject: [PATCH] Simplify recording of main thread's ID on MS-Windows * src/w32term.c (w32_initialize): * src/w32console.c (initialize_w32_display): * src/w32fns.c (globals_of_w32fns): Don't record the main thread ID independently for each type of session (GUI, TTY, batch). * src/w32term.c (w32_init_main_thread): New function, records the main thread's thread ID. * src/w32term.h: Add prototype for w32_init_main_thread. * src/emacs.c (main) [WINDOWSNT]: Call w32_init_main_thread. * src/emacs-module.c [WINDOWSNT]: Rename main_thread_id to main_thread, for consistency with other threading libraries. All users changed. Include w32term.h. (check_main_thread) [WINDOWSNT]: Simplify the test: no need to make sure the main thread is alive, as we hold a handle on it opened by w32_init_main_thread. (module_init) [WINDOWSNT]: Reuse the thread ID recorded by w32_init_main_thread, instead of calling the requisite APIs once more. --- src/emacs-module.c | 35 +++++++---------------------------- src/emacs.c | 3 +++ src/w32console.c | 5 ----- src/w32fns.c | 4 ---- src/w32term.c | 13 +++++++++---- src/w32term.h | 2 ++ 6 files changed, 21 insertions(+), 41 deletions(-) diff --git a/src/emacs-module.c b/src/emacs-module.c index e730ca35ae5..011cc7be914 100644 --- a/src/emacs-module.c +++ b/src/emacs-module.c @@ -50,12 +50,9 @@ static thrd_t main_thread; # include static pthread_t main_thread; #elif defined WINDOWSNT -# include -/* On Windows, store both a handle to the main thread and the - thread ID because the latter can be reused when a thread - terminates. */ -static HANDLE main_thread; -static DWORD main_thread_id; +#include +#include "w32term.h" +static DWORD main_thread; #endif @@ -789,11 +786,7 @@ check_main_thread (void) #elif defined HAVE_PTHREAD eassert (pthread_equal (pthread_self (), main_thread)); #elif defined WINDOWSNT - /* CompareObjectHandles would be perfect, but is only available in - Windows 10. Also check whether the thread is still running to - protect against thread identifier reuse. */ - eassert (GetCurrentThreadId () == main_thread_id - && WaitForSingleObject (main_thread, 0) == WAIT_TIMEOUT); + eassert (GetCurrentThreadId () == main_thread); #endif } @@ -1123,22 +1116,8 @@ module_init (void) #elif defined HAVE_PTHREAD main_thread = pthread_self (); #elif defined WINDOWSNT - /* This calls APIs that are only available on Vista and later. */ -# if false - /* GetCurrentProcess returns a pseudohandle, which must be duplicated. */ - if (! DuplicateHandle (GetCurrentProcess (), GetCurrentThread (), - GetCurrentProcess (), &main_thread, - SYNCHRONIZE | THREAD_QUERY_INFORMATION, - FALSE, 0)) - emacs_abort (); -# else - /* GetCurrentThread returns a pseudohandle, which must be duplicated. */ - HANDLE th = GetCurrentThread (); - if (!DuplicateHandle (GetCurrentProcess (), th, - GetCurrentProcess (), &main_thread, 0, FALSE, - DUPLICATE_SAME_ACCESS)) - emacs_abort (); - main_thread_id = GetCurrentThreadId (); -# endif + /* The 'main' function already recorded the main thread's thread ID, + so we need just to use it . */ + main_thread = dwMainThreadId; #endif } diff --git a/src/emacs.c b/src/emacs.c index ba71ceb84ce..c411da6a5da 100644 --- a/src/emacs.c +++ b/src/emacs.c @@ -760,6 +760,9 @@ main (int argc, char **argv) to have non-stub implementations of APIs we need to convert file names between UTF-8 and the system's ANSI codepage. */ maybe_load_unicows_dll (); + /* This has to be done before module_init is called below, so that + the latter could use the thread ID of the main thread. */ + w32_init_main_thread (); #endif #endif diff --git a/src/w32console.c b/src/w32console.c index ec54f83129f..7fffabf3853 100644 --- a/src/w32console.c +++ b/src/w32console.c @@ -757,13 +757,8 @@ initialize_w32_display (struct terminal *term, int *width, int *height) else w32_console_unicode_input = 0; - /* This is needed by w32notify.c:send_notifications. */ - dwMainThreadId = GetCurrentThreadId (); - /* Setup w32_display_info structure for this frame. */ - w32_initialize_display_info (build_string ("Console")); - } diff --git a/src/w32fns.c b/src/w32fns.c index f3391cb98f0..208c9805e91 100644 --- a/src/w32fns.c +++ b/src/w32fns.c @@ -9925,10 +9925,6 @@ globals_of_w32fns (void) InitCommonControls (); syms_of_w32uniscribe (); - - /* Needed for recovery from C stack overflows in batch mode. */ - if (noninteractive) - dwMainThreadId = GetCurrentThreadId (); } #ifdef NTGUI_UNICODE diff --git a/src/w32term.c b/src/w32term.c index f764e250aa8..f48e72553a5 100644 --- a/src/w32term.c +++ b/src/w32term.c @@ -6925,6 +6925,15 @@ x_delete_display (struct w32_display_info *dpyinfo) /* Set up use of W32. */ +void +w32_init_main_thread (void) +{ + dwMainThreadId = GetCurrentThreadId (); + DuplicateHandle (GetCurrentProcess (), GetCurrentThread (), + GetCurrentProcess (), &hMainThread, 0, TRUE, + DUPLICATE_SAME_ACCESS); +} + DWORD WINAPI w32_msg_worker (void * arg); static void @@ -6985,10 +6994,6 @@ w32_initialize (void) terminates */ init_crit (); - dwMainThreadId = GetCurrentThreadId (); - DuplicateHandle (GetCurrentProcess (), GetCurrentThread (), - GetCurrentProcess (), &hMainThread, 0, TRUE, DUPLICATE_SAME_ACCESS); - /* Wait for thread to start */ { MSG msg; diff --git a/src/w32term.h b/src/w32term.h index 467da10c3b7..3377b53608e 100644 --- a/src/w32term.h +++ b/src/w32term.h @@ -855,6 +855,8 @@ extern void globals_of_w32menu (void); extern void globals_of_w32fns (void); extern void globals_of_w32notify (void); +extern void w32_init_main_thread (void); + #ifdef CYGWIN extern int w32_message_fd; #endif /* CYGWIN */ -- 2.39.5