aboutsummaryrefslogtreecommitdiffstats
path: root/src/emacs-module.c
diff options
context:
space:
mode:
authorEli Zaretskii2015-11-21 19:44:02 +0200
committerEli Zaretskii2015-11-21 19:44:02 +0200
commitd696d62fea48096680d6d511a71c4df56d00a51f (patch)
treeec9d8707142e396e0de00fffbc5dd9df28800d80 /src/emacs-module.c
parente6b1818f87f559fdd854bdace2801637caffe6ae (diff)
downloademacs-d696d62fea48096680d6d511a71c4df56d00a51f.tar.gz
emacs-d696d62fea48096680d6d511a71c4df56d00a51f.zip
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.
Diffstat (limited to 'src/emacs-module.c')
-rw-r--r--src/emacs-module.c35
1 files changed, 7 insertions, 28 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;
50# include <pthread.h> 50# include <pthread.h>
51static pthread_t main_thread; 51static pthread_t main_thread;
52#elif defined WINDOWSNT 52#elif defined WINDOWSNT
53# include <windows.h> 53#include <windows.h>
54/* On Windows, store both a handle to the main thread and the 54#include "w32term.h"
55 thread ID because the latter can be reused when a thread 55static DWORD main_thread;
56 terminates. */
57static HANDLE main_thread;
58static DWORD main_thread_id;
59#endif 56#endif
60 57
61 58
@@ -789,11 +786,7 @@ check_main_thread (void)
789#elif defined HAVE_PTHREAD 786#elif defined HAVE_PTHREAD
790 eassert (pthread_equal (pthread_self (), main_thread)); 787 eassert (pthread_equal (pthread_self (), main_thread));
791#elif defined WINDOWSNT 788#elif defined WINDOWSNT
792 /* CompareObjectHandles would be perfect, but is only available in 789 eassert (GetCurrentThreadId () == main_thread);
793 Windows 10. Also check whether the thread is still running to
794 protect against thread identifier reuse. */
795 eassert (GetCurrentThreadId () == main_thread_id
796 && WaitForSingleObject (main_thread, 0) == WAIT_TIMEOUT);
797#endif 790#endif
798} 791}
799 792
@@ -1123,22 +1116,8 @@ module_init (void)
1123#elif defined HAVE_PTHREAD 1116#elif defined HAVE_PTHREAD
1124 main_thread = pthread_self (); 1117 main_thread = pthread_self ();
1125#elif defined WINDOWSNT 1118#elif defined WINDOWSNT
1126 /* This calls APIs that are only available on Vista and later. */ 1119 /* The 'main' function already recorded the main thread's thread ID,
1127# if false 1120 so we need just to use it . */
1128 /* GetCurrentProcess returns a pseudohandle, which must be duplicated. */ 1121 main_thread = dwMainThreadId;
1129 if (! DuplicateHandle (GetCurrentProcess (), GetCurrentThread (),
1130 GetCurrentProcess (), &main_thread,
1131 SYNCHRONIZE | THREAD_QUERY_INFORMATION,
1132 FALSE, 0))
1133 emacs_abort ();
1134# else
1135 /* GetCurrentThread returns a pseudohandle, which must be duplicated. */
1136 HANDLE th = GetCurrentThread ();
1137 if (!DuplicateHandle (GetCurrentProcess (), th,
1138 GetCurrentProcess (), &main_thread, 0, FALSE,
1139 DUPLICATE_SAME_ACCESS))
1140 emacs_abort ();
1141 main_thread_id = GetCurrentThreadId ();
1142# endif
1143#endif 1122#endif
1144} 1123}