[PATCH 2/5] nptl: Add __pthread_kill_self

Adhemerval Zanella adhemerval.zanella@linaro.org
Fri Aug 29 19:53:22 GMT 2025


The function sends a signal to current thread using raw syscalls.
---
 include/pthread.h        |  1 +
 nptl/Makefile            |  1 +
 nptl/pthread_kill.c      | 15 +--------------
 nptl/pthread_kill_self.c | 39 +++++++++++++++++++++++++++++++++++++++
 sysdeps/posix/raise.c    |  2 +-
 5 files changed, 43 insertions(+), 15 deletions(-)
 create mode 100644 nptl/pthread_kill_self.c

diff --git a/include/pthread.h b/include/pthread.h
index 819bf3f235..103c5c433b 100644
--- a/include/pthread.h
+++ b/include/pthread.h
@@ -21,6 +21,7 @@ libc_hidden_proto (__pthread_barrier_wait)
 extern void __pthread_initialize (void) __attribute__ ((weak));
 
 extern int __pthread_kill (pthread_t threadid, int signo);
+extern int __pthread_kill_self (int signo) attribute_hidden;
 
 extern pthread_t __pthread_self (void);
 
diff --git a/nptl/Makefile b/nptl/Makefile
index e6481d5694..8b18ca98a9 100644
--- a/nptl/Makefile
+++ b/nptl/Makefile
@@ -131,6 +131,7 @@ routines = \
   pthread_keys \
   pthread_kill \
   pthread_kill_other_threads \
+  pthread_kill_self \
   pthread_mutex_cond_lock \
   pthread_mutex_conf \
   pthread_mutex_consistent \
diff --git a/nptl/pthread_kill.c b/nptl/pthread_kill.c
index 3938e3f8cd..3bc7f8a5a8 100644
--- a/nptl/pthread_kill.c
+++ b/nptl/pthread_kill.c
@@ -29,20 +29,7 @@ __pthread_kill_implementation (pthread_t threadid, int signo, int no_tid)
 {
   struct pthread *pd = (struct pthread *) threadid;
   if (pd == THREAD_SELF)
-    {
-      /* Use the actual TID from the kernel, so that it refers to the
-         current thread even if called after vfork.  There is no
-         signal blocking in this case, so that the signal is delivered
-         immediately, before __pthread_kill_internal returns: a signal
-         sent to the thread itself needs to be delivered
-         synchronously.  (It is unclear if Linux guarantees the
-         delivery of all pending signals after unblocking in the code
-         below.  POSIX only guarantees delivery of a single signal,
-         which may not be the right one.)  */
-      pid_t tid = INTERNAL_SYSCALL_CALL (gettid);
-      int ret = INTERNAL_SYSCALL_CALL (tgkill, __getpid (), tid, signo);
-      return INTERNAL_SYSCALL_ERROR_P (ret) ? INTERNAL_SYSCALL_ERRNO (ret) : 0;
-    }
+    return __pthread_kill_self (signo);
 
   /* Block all signals, as required by pd->exit_lock.  */
   internal_sigset_t old_mask;
diff --git a/nptl/pthread_kill_self.c b/nptl/pthread_kill_self.c
new file mode 100644
index 0000000000..bc9fa60619
--- /dev/null
+++ b/nptl/pthread_kill_self.c
@@ -0,0 +1,39 @@
+/* Internal function to send a signal to itself.
+   Copyright (C) 2025 Free Software Foundation, Inc.
+   This file is part of the GNU C Library.
+
+   The GNU C Library is free software; you can redistribute it and/or
+   modify it under the terms of the GNU Lesser General Public
+   License as published by the Free Software Foundation; either
+   version 2.1 of the License, or (at your option) any later version.
+
+   The GNU C Library 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
+   Lesser General Public License for more details.
+
+   You should have received a copy of the GNU Lesser General Public
+   License along with the GNU C Library; if not, see
+   <https://www.gnu.org/licenses/>.  */
+
+#include <sysdep.h>
+#include <pthread.h>
+#include <unistd.h>
+
+int
+__pthread_kill_self (int signo)
+{
+  /* Use the actual TID from the kernel, so that it refers to the
+     current thread even if called after vfork.  There is no
+     signal blocking in this case, so that the signal is delivered
+     immediately, before __pthread_kill_internal returns: a signal
+     sent to the thread itself needs to be delivered
+     synchronously.  (It is unclear if Linux guarantees the
+     delivery of all pending signals after unblocking in the code
+     below.  POSIX only guarantees delivery of a single signal,
+     which may not be the right one.)  */
+  pid_t tid = INTERNAL_SYSCALL_CALL (gettid);
+  pid_t pid = INTERNAL_SYSCALL_CALL (getpid);
+  int ret = INTERNAL_SYSCALL_CALL (tgkill, pid, tid, signo);
+  return INTERNAL_SYSCALL_ERROR_P (ret) ? INTERNAL_SYSCALL_ERRNO (ret) : 0;
+}
diff --git a/sysdeps/posix/raise.c b/sysdeps/posix/raise.c
index da3f11ccf2..3b426d5708 100644
--- a/sysdeps/posix/raise.c
+++ b/sysdeps/posix/raise.c
@@ -23,7 +23,7 @@
 int
 raise (int sig)
 {
-  int ret = __pthread_kill (__pthread_self (), sig);
+  int ret = __pthread_kill_self (sig);
   if (ret != 0)
     {
       __set_errno (ret);
-- 
2.43.0



More information about the Libc-alpha mailing list