From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!.POSTED.blaine.gmane.org!not-for-mail From: Mattias =?UTF-8?Q?Engdeg=C3=A5rd?= Newsgroups: gmane.emacs.bugs Subject: bug#38632: 27.0.50; Emacs process name is changed permanently upon creating a named thread Date: Mon, 6 Jan 2020 22:58:25 +0100 Message-ID: <4612F422-B860-4864-BB60-EA18468BCCDB@acm.org> References: Mime-Version: 1.0 (Mac OS X Mail 12.4 \(3445.104.11\)) Content-Type: multipart/mixed; boundary="Apple-Mail=_AC979AAC-638F-452F-8A80-1145AC9D8C9F" Injection-Info: blaine.gmane.org; posting-host="blaine.gmane.org:195.159.176.226"; logging-data="31940"; mail-complaints-to="usenet@blaine.gmane.org" Cc: Robert Pluim To: 38632@debbugs.gnu.org Original-X-From: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane.org@gnu.org Mon Jan 06 22:59:11 2020 Return-path: Envelope-to: geb-bug-gnu-emacs@m.gmane.org Original-Received: from lists.gnu.org ([209.51.188.17]) by blaine.gmane.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.89) (envelope-from ) id 1ioaOz-00089l-UY for geb-bug-gnu-emacs@m.gmane.org; Mon, 06 Jan 2020 22:59:10 +0100 Original-Received: from localhost ([::1]:39930 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1ioaOy-0005ju-5p for geb-bug-gnu-emacs@m.gmane.org; Mon, 06 Jan 2020 16:59:08 -0500 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]:39379) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1ioaOt-0005ja-3c for bug-gnu-emacs@gnu.org; Mon, 06 Jan 2020 16:59:03 -0500 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1ioaOs-0006tl-8s for bug-gnu-emacs@gnu.org; Mon, 06 Jan 2020 16:59:03 -0500 Original-Received: from debbugs.gnu.org ([209.51.188.43]:40953) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1ioaOr-0006tA-N4 for bug-gnu-emacs@gnu.org; Mon, 06 Jan 2020 16:59:02 -0500 Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1ioaOr-00036G-Kz for bug-gnu-emacs@gnu.org; Mon, 06 Jan 2020 16:59:01 -0500 X-Loop: help-debbugs@gnu.org Resent-From: Mattias =?UTF-8?Q?Engdeg=C3=A5rd?= Original-Sender: "Debbugs-submit" Resent-CC: bug-gnu-emacs@gnu.org Resent-Date: Mon, 06 Jan 2020 21:59:01 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 38632 X-GNU-PR-Package: emacs Original-Received: via spool by 38632-submit@debbugs.gnu.org id=B38632.157834791711880 (code B ref 38632); Mon, 06 Jan 2020 21:59:01 +0000 Original-Received: (at 38632) by debbugs.gnu.org; 6 Jan 2020 21:58:37 +0000 Original-Received: from localhost ([127.0.0.1]:46926 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1ioaOT-00035Y-8S for submit@debbugs.gnu.org; Mon, 06 Jan 2020 16:58:37 -0500 Original-Received: from mail1467c50.megamailservers.eu ([91.136.14.67]:50092 helo=mail268c50.megamailservers.eu) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1ioaOR-00035H-47 for 38632@debbugs.gnu.org; Mon, 06 Jan 2020 16:58:36 -0500 X-Authenticated-User: mattiase@bredband.net DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=megamailservers.eu; s=maildub; t=1578347908; bh=rv07485o8H62mBgbLrpWn3vIalwoRclbqr/qPYWtEro=; h=From:Subject:Date:In-Reply-To:Cc:To:References:From; b=CfCN6bT6zm1hWoNzVJ2wG5B7YTe3cT4EXldJ1FjYqG3nlt+8Wh+Ev2+PG2d41MMWo /D5eiBp6iQ0azwkKCw/IClcxlM4R1dIBbOF+v2GJyiXDOX5qCkhYLNaKwu42/vuERU riWyX8P2bufPpQm3Or7W7tXZCQk2e5ZoKBxRQTik= Feedback-ID: mattiase@acm.or Original-Received: from [192.168.0.4] (c188-150-171-71.bredband.comhem.se [188.150.171.71]) (authenticated bits=0) by mail268c50.megamailservers.eu (8.14.9/8.13.1) with ESMTP id 006LwPxR004628; Mon, 6 Jan 2020 21:58:27 +0000 In-Reply-To: X-Mailer: Apple Mail (2.3445.104.11) X-CTCH-RefID: str=0001.0A0B0212.5E13AD84.001F, ss=1, re=0.000, recu=0.000, reip=0.000, cl=1, cld=1, fgs=0 X-CTCH-VOD: Unknown X-CTCH-Spam: Unknown X-CTCH-Score: 0.000 X-CTCH-Flags: 0 X-CTCH-ScoreCust: 0.000 X-CSC: 0 X-CHA: v=2.3 cv=J53UEzvS c=1 sm=1 tr=0 a=SF+I6pRkHZhrawxbOkkvaA==:117 a=SF+I6pRkHZhrawxbOkkvaA==:17 a=jpOVt7BSZ2e4Z31A5e1TngXxSK0=:19 a=M51BFTxLslgA:10 a=xKR4U9U0X0AW0op-uYYA:9 a=CjuIK1q_8ugA:10 a=vohCn5ePn4EUMMBJDXIA:9 a=De_Ol2h6w80A:10 X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 209.51.188.43 X-BeenThere: bug-gnu-emacs@gnu.org List-Id: "Bug reports for GNU Emacs, the Swiss army knife of text editors" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane.org@gnu.org Original-Sender: "bug-gnu-emacs" Xref: news.gmane.org gmane.emacs.bugs:174272 Archived-At: --Apple-Mail=_AC979AAC-638F-452F-8A80-1145AC9D8C9F Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset=us-ascii Here is a quick-and-dirty patch that repairs the macOS/BSD builds. Not = tested on anything else. --Apple-Mail=_AC979AAC-638F-452F-8A80-1145AC9D8C9F Content-Disposition: attachment; filename=pthread-setname.diff Content-Type: application/octet-stream; x-unix-mode=0644; name="pthread-setname.diff" Content-Transfer-Encoding: 7bit diff --git a/configure.ac b/configure.ac index de4710f150..068ef5c20c 100644 --- a/configure.ac +++ b/configure.ac @@ -4183,6 +4183,23 @@ AC_DEFUN cfmakeraw cfsetspeed __executable_start log2 pthread_setname_np) LIBS=$OLD_LIBS +if test "$ac_cv_func_pthread_setname_np" = "yes"; then + AC_CACHE_CHECK( + [whether pthread_setname_np takes a single argument], + [emacs_cv_pthread_setname_np_1arg], + [AC_COMPILE_IFELSE( + [AC_LANG_PROGRAM( + [[#include ]], + [[pthread_setname_np ("a");]])], + [emacs_cv_pthread_setname_np_1arg=yes], + [emacs_cv_pthread_setname_np_1arg=no])]) + if test "$emacs_cv_pthread_setname_np_1arg" = "yes"; then + AC_DEFINE( + HAVE_PTHREAD_SETNAME_NP_1ARG, 1, + [Define to 1 if pthread_setname_np takes a single argument.]) + fi +fi + dnl No need to check for posix_memalign if aligned_alloc works. AC_CHECK_FUNCS([aligned_alloc posix_memalign], [break]) AC_CHECK_DECLS([aligned_alloc], [], [], [[#include ]]) diff --git a/src/systhread.c b/src/systhread.c index 1dda036cc2..19fc9e1596 100644 --- a/src/systhread.c +++ b/src/systhread.c @@ -200,9 +200,28 @@ sys_thread_equal (sys_thread_t t, sys_thread_t u) return pthread_equal (t, u); } +void +sys_thread_set_name (const char *name) +{ +#ifdef HAVE_PTHREAD_SETNAME_NP + /* We need to truncate here otherwise pthread_setname_np + fails to set the name. TASK_COMM_LEN is what the length + is called in the Linux kernel headers (Bug#38632). */ +#define TASK_COMM_LEN 16 + char p_name[TASK_COMM_LEN]; + strncpy (p_name, name, TASK_COMM_LEN - 1); + p_name[TASK_COMM_LEN - 1] = '\0'; + #ifdef HAVE_PTHREAD_SETNAME_NP_1ARG + pthread_setname_np (p_name); + #else + pthread_setname_np (pthread_self (), p_name); + #endif +#endif +} + bool -sys_thread_create (sys_thread_t *thread_ptr, const char *name, - thread_creation_function *func, void *arg) +sys_thread_create (sys_thread_t *thread_ptr, thread_creation_function *func, + void *arg) { pthread_attr_t attr; bool result = false; @@ -221,22 +240,7 @@ sys_thread_create (sys_thread_t *thread_ptr, const char *name, } if (!pthread_attr_setdetachstate (&attr, PTHREAD_CREATE_DETACHED)) - { - result = pthread_create (thread_ptr, &attr, func, arg) == 0; -#ifdef HAVE_PTHREAD_SETNAME_NP - if (result && name != NULL) - { - /* We need to truncate here otherwise pthread_setname_np - fails to set the name. TASK_COMM_LEN is what the length - is called in the Linux kernel headers (Bug#38632). */ -#define TASK_COMM_LEN 16 - char p_name[TASK_COMM_LEN]; - strncpy (p_name, name, TASK_COMM_LEN - 1); - p_name[TASK_COMM_LEN - 1] = '\0'; - pthread_setname_np (*thread_ptr, p_name); - } -#endif - } + result = pthread_create (thread_ptr, &attr, func, arg) == 0; out: ; int error = pthread_attr_destroy (&attr); @@ -455,7 +459,11 @@ w32_set_thread_name (DWORD thread_id, const char *name) (ULONG_PTR *) &tninfo); } -static thread_creation_function *thread_start_address; +void +sys_thread_set_name (const char *name) +{ + w32_set_thread_name (GetCurrentThreadId (), name); +} /* _beginthread wants a void function, while we are passed a function that returns a pointer. So we use a wrapper. See the command in @@ -463,20 +471,12 @@ w32_set_thread_name (DWORD thread_id, const char *name) static void ALIGN_STACK w32_beginthread_wrapper (void *arg) { - /* FIXME: This isn't very clean: systhread.c is not supposed to know - that ARG is a pointer to a thread_state object, or be familiar - with thread_state object's structure in general. */ - struct thread_state *this_thread = arg; - - if (this_thread->thread_name) - w32_set_thread_name (GetCurrentThreadId (), this_thread->thread_name); - (void)thread_start_address (arg); } bool -sys_thread_create (sys_thread_t *thread_ptr, const char *name, - thread_creation_function *func, void *arg) +sys_thread_create (sys_thread_t *thread_ptr, thread_creation_function *func, + void *arg) { /* FIXME: Do threads that run Lisp require some minimum amount of stack? Zero here means each thread will get the same amount as diff --git a/src/systhread.h b/src/systhread.h index 5368acfb52..005388fd5a 100644 --- a/src/systhread.h +++ b/src/systhread.h @@ -112,10 +112,11 @@ #define SYSTHREAD_H extern bool sys_thread_equal (sys_thread_t, sys_thread_t) ATTRIBUTE_WARN_UNUSED_RESULT; -extern bool sys_thread_create (sys_thread_t *, const char *, - thread_creation_function *, void *) +extern bool sys_thread_create (sys_thread_t *, thread_creation_function *, + void *) ATTRIBUTE_WARN_UNUSED_RESULT; extern void sys_thread_yield (void); +extern void sys_thread_set_name (const char *); #endif /* SYSTHREAD_H */ diff --git a/src/thread.c b/src/thread.c index f7e39dc427..c7fe061426 100644 --- a/src/thread.c +++ b/src/thread.c @@ -725,6 +725,9 @@ run_thread (void *state) self->m_stack_bottom = self->stack_top = (char *) &stack_pos; self->thread_id = sys_thread_self (); + if (self->thread_name) + sys_thread_set_name (self->thread_name); + acquire_global_lock (self); /* Put a dummy catcher at top-level so that handlerlist is never NULL. @@ -832,7 +835,7 @@ DEFUN ("make-thread", Fmake_thread, Smake_thread, 1, 2, 0, else new_thread->thread_name = NULL; sys_thread_t thr; - if (! sys_thread_create (&thr, c_name, run_thread, new_thread)) + if (! sys_thread_create (&thr, run_thread, new_thread)) { /* Restore the previous situation. */ all_threads = all_threads->next_thread; --Apple-Mail=_AC979AAC-638F-452F-8A80-1145AC9D8C9F--