From 520dc85f313e2a30759fd73206c30bff27062785 Mon Sep 17 00:00:00 2001 From: Eli Zaretskii Date: Wed, 18 Nov 2015 22:09:08 +0200 Subject: [PATCH] Fix MS-Windows build --with-modules * src/module.c: Reformat copyright commentary. (module_vec_get): Use explicit cast to size_t to avoid compiler warning in 32-bit builds. (check_main_thread) [WINDOWSNT]: Fix letter-case in Windows APIs. Compare thread IDs directly, as GetThreadId is not available before Windows Vista. (check_main_thread) [WINDOWSNT]: Duplicate the thread handle without using APIs and constants not available on XP and older systems. Obtain and store the thread ID as well. --- src/module.c | 55 ++++++++++++++++++++++++++++++++-------------------- 1 file changed, 34 insertions(+), 21 deletions(-) diff --git a/src/module.c b/src/module.c index 125fd7fed26..9360744c4ec 100644 --- a/src/module.c +++ b/src/module.c @@ -1,22 +1,21 @@ -/* - module.c - Module loading and runtime implementation - Copyright (C) 2015 Free Software Foundation, Inc. +/* module.c - Module loading and runtime implementation + +Copyright (C) 2015 Free Software Foundation, Inc. - This file is part of GNU Emacs. +This file is part of GNU Emacs. - GNU Emacs is free software: you can redistribute it and/or modify - it under the terms of the GNU General Public License as published by - the Free Software Foundation, either version 3 of the License, or - (at your option) any later version. +GNU Emacs is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. - GNU Emacs is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. +GNU Emacs is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. - You should have received a copy of the GNU General Public License - along with GNU Emacs. If not, see . -*/ +You should have received a copy of the GNU General Public License +along with GNU Emacs. If not, see . */ #include #include @@ -53,9 +52,11 @@ static thrd_t main_thread; static pthread_t main_thread; #elif defined(WINDOWSNT) #include -/* On Windows, we store a handle to the main thread instead of the - thread ID because the latter can be reused when a thread terminates. */ +/* On Windows, we 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; #endif @@ -757,7 +758,8 @@ static emacs_value module_vec_get (emacs_env *env, eassert (size >= 0); if (i >= size) { - if (i > MOST_POSITIVE_FIXNUM) i = MOST_POSITIVE_FIXNUM; + if (i > MOST_POSITIVE_FIXNUM) + i = (size_t) MOST_POSITIVE_FIXNUM; module_args_out_of_range (env, lvec, make_number (i)); return NULL; } @@ -899,8 +901,8 @@ static void check_main_thread (void) /* 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 () == GetThreadID (main_thread) && - WaitForSingleObject (main_thread, 0) == WAIT_TIMEOUT); + eassert (GetCurrentThreadId () == main_thread_id + && WaitForSingleObject (main_thread, 0) == WAIT_TIMEOUT); #endif } @@ -1175,11 +1177,22 @@ void 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 0 /* GetCurrentProcess returns a pseudohandle, which we have to duplicate. */ if (! DuplicateHandle (GetCurrentProcess(), GetCurrentThread(), GetCurrentProcess(), &main_thread, - SYNCHRONIZE | THREAD_QUERY_LIMITED_INFORMATION, + SYNCHRONIZE | THREAD_QUERY_INFORMATION, FALSE, 0)) emacs_abort (); +#else + /* GetCurrentThread returns a pseudohandle, which we have to duplicate. */ + HANDLE th = GetCurrentThread (); + if (!DuplicateHandle (GetCurrentProcess (), th, + GetCurrentProcess (), &main_thread, 0, FALSE, + DUPLICATE_SAME_ACCESS)) + emacs_abort (); + main_thread_id = GetCurrentThreadId (); +#endif #endif } -- 2.39.5