]> code.delx.au - gnu-emacs/commitdiff
Simplify recording of main thread's ID on MS-Windows
authorEli Zaretskii <eliz@gnu.org>
Sat, 21 Nov 2015 17:44:02 +0000 (19:44 +0200)
committerEli Zaretskii <eliz@gnu.org>
Sat, 21 Nov 2015 17:44:02 +0000 (19:44 +0200)
* 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
src/emacs.c
src/w32console.c
src/w32fns.c
src/w32term.c
src/w32term.h

index e730ca35ae520effee5b0dd3eb664a39c55e06b6..011cc7be91440d98c1761c196bc8ece5e811013b 100644 (file)
@@ -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
 
 \f
@@ -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
 }
index ba71ceb84ce09f608fecfaaf3a42f02c2cd37b59..c411da6a5da87212b44e0dfe98138519d559c410 100644 (file)
@@ -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
 
index ec54f83129f4cd4c5b81333ed2de8ab0a7498eed..7fffabf38535aba61ab6865dc097246b5edceeea 100644 (file)
@@ -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"));
-
 }
 
 
index f3391cb98f01620a34d5d1b9cebf52b7e7cb6303..208c9805e91c640ccb33983a906291ddb458a0b6 100644 (file)
@@ -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
index f764e250aa812fc643758f3f3d579def9edcad6e..f48e72553a51cb18cb7b57839b1abf76bb5c53d9 100644 (file)
@@ -6925,6 +6925,15 @@ x_delete_display (struct w32_display_info *dpyinfo)
 \f
 /* 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;
index 467da10c3b7e8aceea55e527bead441317af5654..3377b53608ea3c74b0ee33c4aa69f45d93c8de11 100644 (file)
@@ -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 */