Commit d696d62f authored by Eli Zaretskii's avatar Eli Zaretskii

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.
parent e6b1818f
......@@ -50,12 +50,9 @@ static thrd_t main_thread;
# include <pthread.h>
static pthread_t main_thread;
#elif defined WINDOWSNT
# include <windows.h>
/* 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 <windows.h>
#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
}
......@@ -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
......
......@@ -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"));
}
......
......@@ -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
......
......@@ -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;
......
......@@ -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 */
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment