From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!.POSTED!not-for-mail From: Paul Eggert Newsgroups: gmane.emacs.devel Subject: Re: Delivering SIGIO when several threads are active? Date: Fri, 30 Dec 2016 13:05:44 -0800 Organization: UCLA Computer Science Department Message-ID: <90c023d2-7e44-d159-9249-0e749c05a584@cs.ucla.edu> References: <8360m1u311.fsf@gnu.org> <8e01d8c5-66fa-fcb8-ef0e-74ca10a72317@cs.ucla.edu> <83wpehs1pm.fsf@gnu.org> NNTP-Posting-Host: blaine.gmane.org Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="------------6D06BBF9B27EE5704F7BF973" X-Trace: blaine.gmane.org 1483132005 23017 195.159.176.226 (30 Dec 2016 21:06:45 GMT) X-Complaints-To: usenet@blaine.gmane.org NNTP-Posting-Date: Fri, 30 Dec 2016 21:06:45 +0000 (UTC) User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:45.0) Gecko/20100101 Thunderbird/45.5.1 Cc: emacs-devel@gnu.org To: Eli Zaretskii Original-X-From: emacs-devel-bounces+ged-emacs-devel=m.gmane.org@gnu.org Fri Dec 30 22:06:40 2016 Return-path: Envelope-to: ged-emacs-devel@m.gmane.org Original-Received: from lists.gnu.org ([208.118.235.17]) by blaine.gmane.org with esmtp (Exim 4.84_2) (envelope-from ) id 1cN4Nm-0004dM-RS for ged-emacs-devel@m.gmane.org; Fri, 30 Dec 2016 22:06:35 +0100 Original-Received: from localhost ([::1]:41488 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1cN4Nr-00027L-Qr for ged-emacs-devel@m.gmane.org; Fri, 30 Dec 2016 16:06:39 -0500 Original-Received: from eggs.gnu.org ([2001:4830:134:3::10]:43094) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1cN4N9-00024w-62 for emacs-devel@gnu.org; Fri, 30 Dec 2016 16:05:56 -0500 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1cN4N7-0003mu-Ma for emacs-devel@gnu.org; Fri, 30 Dec 2016 16:05:55 -0500 Original-Received: from zimbra.cs.ucla.edu ([131.179.128.68]:56958) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1cN4N2-0003le-Oq; Fri, 30 Dec 2016 16:05:49 -0500 Original-Received: from localhost (localhost [127.0.0.1]) by zimbra.cs.ucla.edu (Postfix) with ESMTP id D67A6160156; Fri, 30 Dec 2016 13:05:46 -0800 (PST) Original-Received: from zimbra.cs.ucla.edu ([127.0.0.1]) by localhost (zimbra.cs.ucla.edu [127.0.0.1]) (amavisd-new, port 10032) with ESMTP id bVoAuNx0rtuv; Fri, 30 Dec 2016 13:05:45 -0800 (PST) Original-Received: from localhost (localhost [127.0.0.1]) by zimbra.cs.ucla.edu (Postfix) with ESMTP id 269F1160161; Fri, 30 Dec 2016 13:05:45 -0800 (PST) X-Virus-Scanned: amavisd-new at zimbra.cs.ucla.edu Original-Received: from zimbra.cs.ucla.edu ([127.0.0.1]) by localhost (zimbra.cs.ucla.edu [127.0.0.1]) (amavisd-new, port 10026) with ESMTP id bsnGqQH3QCVM; Fri, 30 Dec 2016 13:05:45 -0800 (PST) Original-Received: from [192.168.1.9] (unknown [47.153.178.162]) by zimbra.cs.ucla.edu (Postfix) with ESMTPSA id 04255160156; Fri, 30 Dec 2016 13:05:45 -0800 (PST) In-Reply-To: <83wpehs1pm.fsf@gnu.org> X-detected-operating-system: by eggs.gnu.org: GNU/Linux 3.x [fuzzy] X-Received-From: 131.179.128.68 X-BeenThere: emacs-devel@gnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: "Emacs development discussions." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: emacs-devel-bounces+ged-emacs-devel=m.gmane.org@gnu.org Original-Sender: "Emacs-devel" Xref: news.gmane.org gmane.emacs.devel:211005 Archived-At: This is a multi-part message in MIME format. --------------6D06BBF9B27EE5704F7BF973 Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: quoted-printable Eli Zaretskii wrote: > what if another thread > is waiting on the keyboard descriptor and wants to handle keyboard > input, or wants to receive X events? That depends on what is meant by "waiting on the keyboard descriptor" and= "wants=20 to receive X events". At the low level, if the other thread was not invol= ved in=20 delivering the signal and is calling pselect on the keyboard descriptor w= hile=20 keyboard input occurs, pselect will return successfully without being=20 interrupted. Similarly for X events, I assume. >> On GNU/Linux, process signals get sent to the main thread unless the m= ain thread >> is blocking them (or if a few less-common situations arise, e.g., the = main >> thread is exiting). > And what happens in those exceptional cases? Typically the signal gets sent to some other thread. (There are exception= s to=20 this, too; it's complicated.) >> Hmm, I see that the recent changes use mixed terminology. Until now, E= macs has >> called the initial thread the "main thread", but the recent code somet= imes calls >> it the "primary thread". We shouldn't have two names for the same thin= g. I'll >> look into fixing that. > "Primary thread" is the name of the main thread. How so? Threads don't have names in Emacs. In POSIX it's called the "initial thread". Emacs has long used "main thre= ad"=20 internally to describe the same thing; this is a common alternative name,= and=20 it's shorter so it's OK as long as we're consistent about it. Using a *th= ird*=20 phrase "primary thread" for the same thing in some places adds needless c= onfusion. Evidently the longstanding variable named main_thread (which is a thread = ID) got=20 in the way of the names that thread.c should have used, and thread.c inst= ead=20 used names like "primary_thread". I'll try to lessen the confusion by cha= nging=20 the longstanding variable name to "main_thread_id" (which is more-accurat= e=20 anyway). This will give thread.c the freedom to use names like "main_thre= ad" for=20 its own purposes. Also, it'll avoid an unnecessary duplicate of the main = thread=20 ID in emacs-module.c. I installed the attached patch to do all this. --------------6D06BBF9B27EE5704F7BF973 Content-Type: text/x-diff; name="0001-Rename-main_thread-to-main_thread_id-and-simplify.patch" Content-Transfer-Encoding: quoted-printable Content-Disposition: attachment; filename*0="0001-Rename-main_thread-to-main_thread_id-and-simplify.patch" =46rom 01adadb144b7bdd456ceb38076e6add925ba6e24 Mon Sep 17 00:00:00 2001 From: Paul Eggert Date: Fri, 30 Dec 2016 13:01:39 -0800 Subject: [PATCH] Rename main_thread to main_thread_id and simplify * src/emacs-module.c: Include syssignal.h, for main_thread_id. [HAVE_PTHREAD]: Do not include pthread.h. (main_thread): Remove. All uses replaced by main_thread_id, or by dwMainThreadId on NT. Since the HAVE_PTHREAD code is now using the main_thread_id established by sysdep.c, there is no need for a separate copy of the main thread ID here. (module_init): Remove. All uses removed. * src/sysdep.c (main_thread_id) [HAVE_PTHREAD]: Rename from main_thread. All uses changed. Now extern. --- src/emacs-module.c | 30 ++++-------------------------- src/emacs.c | 6 ------ src/lisp.h | 1 - src/sysdep.c | 14 +++++++------- src/syssignal.h | 1 + 5 files changed, 12 insertions(+), 40 deletions(-) diff --git a/src/emacs-module.c b/src/emacs-module.c index 68aeb0c..280c055 100644 --- a/src/emacs-module.c +++ b/src/emacs-module.c @@ -28,6 +28,7 @@ along with GNU Emacs. If not, see . */ #include "lisp.h" #include "dynlib.h" #include "coding.h" +#include "syssignal.h" =20 #include #include @@ -41,15 +42,9 @@ enum { module_has_cleanup =3D true }; enum { module_has_cleanup =3D false }; #endif =20 -/* Handle to the main thread. Used to verify that modules call us in - the right thread. */ -#ifdef HAVE_PTHREAD -# include -static pthread_t main_thread; -#elif defined WINDOWSNT +#ifdef WINDOWSNT #include #include "w32term.h" -static DWORD main_thread; #endif =20 /* True if Lisp_Object and emacs_value have the same representation. @@ -751,9 +746,9 @@ static void check_main_thread (void) { #ifdef HAVE_PTHREAD - eassert (pthread_equal (pthread_self (), main_thread)); + eassert (pthread_equal (pthread_self (), main_thread_id)); #elif defined WINDOWSNT - eassert (GetCurrentThreadId () =3D=3D main_thread); + eassert (GetCurrentThreadId () =3D=3D dwMainThreadId); #endif } =20 @@ -1062,20 +1057,3 @@ syms_of_module (void) DEFSYM (Qinternal__module_call, "internal--module-call"); defsubr (&Sinternal_module_call); } - -/* Unlike syms_of_module, this initializer is called even from an - initialized (dumped) Emacs. */ - -void -module_init (void) -{ - /* It is not guaranteed that dynamic initializers run in the main thre= ad, - therefore detect the main thread here. */ -#ifdef HAVE_PTHREAD - main_thread =3D pthread_self (); -#elif defined WINDOWSNT - /* The 'main' function already recorded the main thread's thread ID, - so we need just to use it . */ - main_thread =3D dwMainThreadId; -#endif -} diff --git a/src/emacs.c b/src/emacs.c index eff3f9d..d461fe2 100644 --- a/src/emacs.c +++ b/src/emacs.c @@ -738,8 +738,6 @@ main (int argc, char **argv) non-ASCII file names during startup. */ w32_init_file_name_codepage (); #endif - /* 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 =20 @@ -757,10 +755,6 @@ main (int argc, char **argv) init_standard_fds (); atexit (close_output_streams); =20 -#ifdef HAVE_MODULES - module_init (); -#endif - sort_args (argc, argv); argc =3D 0; while (argv[argc]) argc++; diff --git a/src/lisp.h b/src/lisp.h index 1a586ca..8496308 100644 --- a/src/lisp.h +++ b/src/lisp.h @@ -3880,7 +3880,6 @@ extern bool let_shadows_global_binding_p (Lisp_Obje= ct symbol); extern Lisp_Object make_user_ptr (void (*finalizer) (void *), void *p); =20 /* Defined in emacs-module.c. */ -extern void module_init (void); extern void syms_of_module (void); #endif =20 diff --git a/src/sysdep.c b/src/sysdep.c index 1ba336e..1227afc 100644 --- a/src/sysdep.c +++ b/src/sysdep.c @@ -1612,7 +1612,7 @@ emacs_sigaction_init (struct sigaction *action, sig= nal_handler_t handler) } =20 #ifdef FORWARD_SIGNAL_TO_MAIN_THREAD -static pthread_t main_thread; +pthread_t main_thread_id; #endif =20 /* SIG has arrived at the current process. Deliver it to the main @@ -1636,13 +1636,13 @@ deliver_process_signal (int sig, signal_handler_t= handler) =20 bool on_main_thread =3D true; #ifdef FORWARD_SIGNAL_TO_MAIN_THREAD - if (! pthread_equal (pthread_self (), main_thread)) + if (! pthread_equal (pthread_self (), main_thread_id)) { sigset_t blocked; sigemptyset (&blocked); sigaddset (&blocked, sig); pthread_sigmask (SIG_BLOCK, &blocked, 0); - pthread_kill (main_thread, sig); + pthread_kill (main_thread_id, sig); on_main_thread =3D false; } #endif @@ -1668,12 +1668,12 @@ deliver_thread_signal (int sig, signal_handler_t = handler) int old_errno =3D errno; =20 #ifdef FORWARD_SIGNAL_TO_MAIN_THREAD - if (! pthread_equal (pthread_self (), main_thread)) + if (! pthread_equal (pthread_self (), main_thread_id)) { thread_backtrace_npointers =3D backtrace (thread_backtrace_buffer, BACKTRACE_LIMIT_MAX); sigaction (sig, &process_fatal_action, 0); - pthread_kill (main_thread, sig); + pthread_kill (main_thread_id, sig); =20 /* Avoid further damage while the main thread is exiting. */ while (1) @@ -1796,7 +1796,7 @@ handle_sigsegv (int sig, siginfo_t *siginfo, void *= arg) bool fatal =3D gc_in_progress; =20 #ifdef FORWARD_SIGNAL_TO_MAIN_THREAD - if (!fatal && !pthread_equal (pthread_self (), main_thread)) + if (!fatal && !pthread_equal (pthread_self (), main_thread_id)) fatal =3D true; #endif =20 @@ -1888,7 +1888,7 @@ init_signals (bool dumping) sigemptyset (&empty_mask); =20 #ifdef FORWARD_SIGNAL_TO_MAIN_THREAD - main_thread =3D pthread_self (); + main_thread_id =3D pthread_self (); #endif =20 #if !HAVE_DECL_SYS_SIGLIST && !defined _sys_siglist diff --git a/src/syssignal.h b/src/syssignal.h index 62704fc..215aafe 100644 --- a/src/syssignal.h +++ b/src/syssignal.h @@ -32,6 +32,7 @@ extern void unblock_tty_out_signal (sigset_t const *); =20 #ifdef HAVE_PTHREAD #include +extern pthread_t main_thread_id; /* If defined, asynchronous signals delivered to a non-main thread are forwarded to the main thread. */ #define FORWARD_SIGNAL_TO_MAIN_THREAD --=20 2.7.4 --------------6D06BBF9B27EE5704F7BF973--