emacs-diffs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Emacs-diffs] emacs-25 d696d62: Simplify recording of main thread's ID o


From: Eli Zaretskii
Subject: [Emacs-diffs] emacs-25 d696d62: Simplify recording of main thread's ID on MS-Windows
Date: Sat, 21 Nov 2015 17:45:52 +0000

branch: emacs-25
commit d696d62fea48096680d6d511a71c4df56d00a51f
Author: Eli Zaretskii <address@hidden>
Commit: Eli Zaretskii <address@hidden>

    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 e730ca3..011cc7b 100644
--- a/src/emacs-module.c
+++ b/src/emacs-module.c
@@ -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
 }
diff --git a/src/emacs.c b/src/emacs.c
index ba71ceb..c411da6 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 ec54f83..7fffabf 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 f3391cb..208c980 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 f764e25..f48e725 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 467da10..3377b53 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 */



reply via email to

[Prev in Thread] Current Thread [Next in Thread]