diff options
| author | Eli Zaretskii | 2015-11-21 19:44:02 +0200 |
|---|---|---|
| committer | Eli Zaretskii | 2015-11-21 19:44:02 +0200 |
| commit | d696d62fea48096680d6d511a71c4df56d00a51f (patch) | |
| tree | ec9d8707142e396e0de00fffbc5dd9df28800d80 | |
| parent | e6b1818f87f559fdd854bdace2801637caffe6ae (diff) | |
| download | emacs-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.
| -rw-r--r-- | src/emacs-module.c | 35 | ||||
| -rw-r--r-- | src/emacs.c | 3 | ||||
| -rw-r--r-- | src/w32console.c | 5 | ||||
| -rw-r--r-- | src/w32fns.c | 4 | ||||
| -rw-r--r-- | src/w32term.c | 13 | ||||
| -rw-r--r-- | 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; | |||
| 50 | # include <pthread.h> | 50 | # include <pthread.h> |
| 51 | static pthread_t main_thread; | 51 | static 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 | 55 | static DWORD main_thread; |
| 56 | terminates. */ | ||
| 57 | static HANDLE main_thread; | ||
| 58 | static 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 | } |
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) | |||
| 760 | to have non-stub implementations of APIs we need to convert file | 760 | to have non-stub implementations of APIs we need to convert file |
| 761 | names between UTF-8 and the system's ANSI codepage. */ | 761 | names between UTF-8 and the system's ANSI codepage. */ |
| 762 | maybe_load_unicows_dll (); | 762 | maybe_load_unicows_dll (); |
| 763 | /* This has to be done before module_init is called below, so that | ||
| 764 | the latter could use the thread ID of the main thread. */ | ||
| 765 | w32_init_main_thread (); | ||
| 763 | #endif | 766 | #endif |
| 764 | #endif | 767 | #endif |
| 765 | 768 | ||
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) | |||
| 757 | else | 757 | else |
| 758 | w32_console_unicode_input = 0; | 758 | w32_console_unicode_input = 0; |
| 759 | 759 | ||
| 760 | /* This is needed by w32notify.c:send_notifications. */ | ||
| 761 | dwMainThreadId = GetCurrentThreadId (); | ||
| 762 | |||
| 763 | /* Setup w32_display_info structure for this frame. */ | 760 | /* Setup w32_display_info structure for this frame. */ |
| 764 | |||
| 765 | w32_initialize_display_info (build_string ("Console")); | 761 | w32_initialize_display_info (build_string ("Console")); |
| 766 | |||
| 767 | } | 762 | } |
| 768 | 763 | ||
| 769 | 764 | ||
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) | |||
| 9925 | InitCommonControls (); | 9925 | InitCommonControls (); |
| 9926 | 9926 | ||
| 9927 | syms_of_w32uniscribe (); | 9927 | syms_of_w32uniscribe (); |
| 9928 | |||
| 9929 | /* Needed for recovery from C stack overflows in batch mode. */ | ||
| 9930 | if (noninteractive) | ||
| 9931 | dwMainThreadId = GetCurrentThreadId (); | ||
| 9932 | } | 9928 | } |
| 9933 | 9929 | ||
| 9934 | #ifdef NTGUI_UNICODE | 9930 | #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) | |||
| 6925 | 6925 | ||
| 6926 | /* Set up use of W32. */ | 6926 | /* Set up use of W32. */ |
| 6927 | 6927 | ||
| 6928 | void | ||
| 6929 | w32_init_main_thread (void) | ||
| 6930 | { | ||
| 6931 | dwMainThreadId = GetCurrentThreadId (); | ||
| 6932 | DuplicateHandle (GetCurrentProcess (), GetCurrentThread (), | ||
| 6933 | GetCurrentProcess (), &hMainThread, 0, TRUE, | ||
| 6934 | DUPLICATE_SAME_ACCESS); | ||
| 6935 | } | ||
| 6936 | |||
| 6928 | DWORD WINAPI w32_msg_worker (void * arg); | 6937 | DWORD WINAPI w32_msg_worker (void * arg); |
| 6929 | 6938 | ||
| 6930 | static void | 6939 | static void |
| @@ -6985,10 +6994,6 @@ w32_initialize (void) | |||
| 6985 | terminates */ | 6994 | terminates */ |
| 6986 | init_crit (); | 6995 | init_crit (); |
| 6987 | 6996 | ||
| 6988 | dwMainThreadId = GetCurrentThreadId (); | ||
| 6989 | DuplicateHandle (GetCurrentProcess (), GetCurrentThread (), | ||
| 6990 | GetCurrentProcess (), &hMainThread, 0, TRUE, DUPLICATE_SAME_ACCESS); | ||
| 6991 | |||
| 6992 | /* Wait for thread to start */ | 6997 | /* Wait for thread to start */ |
| 6993 | { | 6998 | { |
| 6994 | MSG msg; | 6999 | 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); | |||
| 855 | extern void globals_of_w32fns (void); | 855 | extern void globals_of_w32fns (void); |
| 856 | extern void globals_of_w32notify (void); | 856 | extern void globals_of_w32notify (void); |
| 857 | 857 | ||
| 858 | extern void w32_init_main_thread (void); | ||
| 859 | |||
| 858 | #ifdef CYGWIN | 860 | #ifdef CYGWIN |
| 859 | extern int w32_message_fd; | 861 | extern int w32_message_fd; |
| 860 | #endif /* CYGWIN */ | 862 | #endif /* CYGWIN */ |