You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@nuttx.apache.org by pk...@apache.org on 2023/01/26 06:12:07 UTC

[nuttx] 02/02: sched: Implement tkill/tgkill

This is an automated email from the ASF dual-hosted git repository.

pkarashchenko pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/nuttx.git

commit 819fbd22ccf75a89fc6a9b227dbc3e9a69828af8
Author: Xiang Xiao <xi...@xiaomi.com>
AuthorDate: Tue Jan 24 06:52:51 2023 +0800

    sched: Implement tkill/tgkill
    
    https://linux.die.net/man/2/tgkill
    
    Signed-off-by: Xiang Xiao <xi...@xiaomi.com>
---
 include/signal.h                                   |  3 +
 include/sys/syscall_lookup.h                       |  2 +-
 libs/libc/pthread/Make.defs                        |  2 +-
 {sched => libs/libc}/pthread/pthread_kill.c        | 85 +---------------------
 libs/libc/signal/sig_raise.c                       | 34 +--------
 sched/pthread/Make.defs                            |  3 +-
 sched/signal/Make.defs                             |  2 +-
 .../pthread_kill.c => signal/sig_tgkill.c}         | 47 ++++++------
 syscall/syscall.csv                                |  2 +-
 9 files changed, 37 insertions(+), 143 deletions(-)

diff --git a/include/signal.h b/include/signal.h
index 81ed3c377e..fa053b29d7 100644
--- a/include/signal.h
+++ b/include/signal.h
@@ -306,6 +306,8 @@
 #  define SIG_HOLD      ((_sa_handler_t)1)   /* Used only with sigset() */
 #endif
 
+#define tkill(tid, signo)            tgkill((pid_t)-1, tid, signo)
+
 #define sigisemptyset(set)           (!*(set))
 #define sigorset(dest, left, right)  (!(*(dest) = *(left) | *(right)))
 #define sigandset(dest, left, right) (!(*(dest) = *(left) & *(right)))
@@ -424,6 +426,7 @@ extern "C"
 #endif
 
 int  kill(pid_t pid, int signo);
+int  tgkill(pid_t pid, pid_t tid, int signo);
 void psignal(int signum, FAR const char *message);
 void psiginfo(FAR const siginfo_t *pinfo, FAR const char *message);
 int  raise(int signo);
diff --git a/include/sys/syscall_lookup.h b/include/sys/syscall_lookup.h
index e1bd2efa1b..b0a690d9ca 100644
--- a/include/sys/syscall_lookup.h
+++ b/include/sys/syscall_lookup.h
@@ -153,6 +153,7 @@ SYSCALL_LOOKUP(task_setcancelstate,        2)
  */
 
 SYSCALL_LOOKUP(kill,                       2)
+SYSCALL_LOOKUP(tgkill,                     3)
 SYSCALL_LOOKUP(sigaction,                  3)
 SYSCALL_LOOKUP(sigpending,                 1)
 SYSCALL_LOOKUP(sigprocmask,                3)
@@ -319,7 +320,6 @@ SYSCALL_LOOKUP(munmap,                     2)
   SYSCALL_LOOKUP(pthread_getaffinity_np,   3)
 #endif
   SYSCALL_LOOKUP(pthread_cond_clockwait,   4)
-  SYSCALL_LOOKUP(pthread_kill,             2)
   SYSCALL_LOOKUP(pthread_sigmask,          3)
 #endif
 
diff --git a/libs/libc/pthread/Make.defs b/libs/libc/pthread/Make.defs
index fe9849c3c1..138fe8befe 100644
--- a/libs/libc/pthread/Make.defs
+++ b/libs/libc/pthread/Make.defs
@@ -42,7 +42,7 @@ CSRCS += pthread_condattr_init.c pthread_condattr_destroy.c
 CSRCS += pthread_condattr_getpshared.c pthread_condattr_setpshared.c
 CSRCS += pthread_condattr_setclock.c pthread_condattr_getclock.c
 CSRCS += pthread_condinit.c pthread_conddestroy.c pthread_condtimedwait.c
-CSRCS += pthread_create.c pthread_exit.c
+CSRCS += pthread_create.c pthread_exit.c pthread_kill.c
 CSRCS += pthread_setname_np.c pthread_getname_np.c
 CSRCS += pthread_get_stackaddr_np.c pthread_get_stacksize_np.c
 CSRCS += pthread_mutexattr_init.c pthread_mutexattr_destroy.c
diff --git a/sched/pthread/pthread_kill.c b/libs/libc/pthread/pthread_kill.c
similarity index 58%
copy from sched/pthread/pthread_kill.c
copy to libs/libc/pthread/pthread_kill.c
index b8b2295d00..67683c679e 100644
--- a/sched/pthread/pthread_kill.c
+++ b/libs/libc/pthread/pthread_kill.c
@@ -1,5 +1,5 @@
 /****************************************************************************
- * sched/pthread/pthread_kill.c
+ * libs/libc/pthread/pthread_kill.c
  *
  * Licensed to the Apache Software Foundation (ASF) under one or more
  * contributor license agreements.  See the NOTICE file distributed with
@@ -22,19 +22,9 @@
  * Included Files
  ****************************************************************************/
 
-#include <nuttx/config.h>
-
-#include <sys/types.h>
 #include <signal.h>
 #include <pthread.h>
 #include <errno.h>
-#include <debug.h>
-
-#include <nuttx/sched.h>
-#include <nuttx/signal.h>
-
-#include "sched/sched.h"
-#include "signal/signal.h"
 
 /****************************************************************************
  * Public Functions
@@ -69,78 +59,11 @@
 
 int pthread_kill(pthread_t thread, int signo)
 {
-#ifdef HAVE_GROUP_MEMBERS
-  /* If group members are supported then pthread_kill() differs from
-   * nxsig_kill().  nxsig_kill(), in this case, must follow the POSIX rules
-   * for delivery of signals in the group environment.  Otherwise,
-   * nxsig_kill(), like pthread_kill() will just deliver the signal to the
-   * thread ID it is requested to use.
-   */
-
-#ifdef CONFIG_SCHED_HAVE_PARENT
-  FAR struct tcb_s *rtcb = this_task();
-#endif
-  FAR struct tcb_s *stcb;
-  siginfo_t info;
-  int ret;
-
-  /* Make sure that the signal is valid */
-
-  if (!GOOD_SIGNO(signo))
-    {
-      ret = -EINVAL;
-      goto errout;
-    }
-
-  /* Keep things stationary through the following */
-
-  sched_lock();
-
-  /* Create the siginfo structure */
-
-  info.si_signo           = signo;
-  info.si_code            = SI_USER;
-  info.si_errno           = EINTR;
-  info.si_value.sival_ptr = NULL;
-#ifdef CONFIG_SCHED_HAVE_PARENT
-  info.si_pid             = rtcb->pid;
-  info.si_status          = OK;
-#endif
-
-  /* Get the TCB associated with the thread */
-
-  stcb = nxsched_get_tcb((pid_t)thread);
-  if (!stcb)
-    {
-      ret = -ESRCH;
-      goto errout_with_lock;
-    }
-
-  /* Dispatch the signal to thread, bypassing normal task group thread
-   * dispatch rules.
-   */
-
-  ret = nxsig_tcbdispatch(stcb, &info);
-  sched_unlock();
-
+  int ret = tkill((pid_t)thread, signo);
   if (ret < 0)
     {
-      goto errout;
+      ret = get_errno();
     }
 
-  return OK;
-
-errout_with_lock:
-  sched_unlock();
-errout:
-  return -ret;
-
-#else
-  /* If group members are not supported then pthread_kill is basically the
-   * same as nxsig_kill() other than the sign of the returned value.
-   */
-
-  int ret = nxsig_kill((pid_t)thread, signo);
-  return (ret < 0) ? -ret : OK;
-#endif
+  return ret;
 }
diff --git a/libs/libc/signal/sig_raise.c b/libs/libc/signal/sig_raise.c
index c15b89f861..9359aea02a 100644
--- a/libs/libc/signal/sig_raise.c
+++ b/libs/libc/signal/sig_raise.c
@@ -22,52 +22,24 @@
  * Included Files
  ****************************************************************************/
 
-#include <nuttx/config.h>
-
 #include <signal.h>
-#include <pthread.h>
-#include <assert.h>
-#include <errno.h>
+#include <unistd.h>
 
 /****************************************************************************
  * Public Functions
  ****************************************************************************/
 
 /****************************************************************************
- * Name: sig_raise
+ * Name: raise
  *
  * Description:
  *   The raise() function sends the signal signo to the executing thread or
  *   process. If a signal handler is called, the raise() function does not
  *   return until after the signal handler does.
  *
- *   If the implementation supports the Threads option, the effect of the
- *   raise() function is equivalent to calling:
- *
- *     pthread_kill(pthread_self(), signo);
- *
- *   except that on failures, -1 (ERROR) is returned and the errno() variable
- *   is set accordingly.  Otherwise, the effect of the raise() function is
- *   equivalent to calling:
- *
- *     kill(getpid(), signo)
- *
  ****************************************************************************/
 
 int raise(int signo)
 {
-#ifndef CONFIG_DISABLE_PTHREAD
-  int errcode = pthread_kill(pthread_self(), signo);
-  if (errcode != OK)
-    {
-      DEBUGASSERT(errcode > 0);
-      set_errno(errcode);
-      return ERROR;
-    }
-
-  return OK;
-
-#else
-  return kill(getpid(), signo);
-#endif
+  return tkill(gettid(), signo);
 }
diff --git a/sched/pthread/Make.defs b/sched/pthread/Make.defs
index 681fe3ec3a..6913a8dee0 100644
--- a/sched/pthread/Make.defs
+++ b/sched/pthread/Make.defs
@@ -25,8 +25,7 @@ CSRCS += pthread_getschedparam.c pthread_setschedparam.c
 CSRCS += pthread_mutexinit.c pthread_mutexdestroy.c
 CSRCS += pthread_mutextimedlock.c pthread_mutextrylock.c pthread_mutexunlock.c
 CSRCS += pthread_condwait.c pthread_condsignal.c pthread_condbroadcast.c
-CSRCS += pthread_condclockwait.c pthread_kill.c pthread_sigmask.c
-CSRCS += pthread_cancel.c
+CSRCS += pthread_condclockwait.c pthread_sigmask.c pthread_cancel.c
 CSRCS += pthread_initialize.c pthread_completejoin.c pthread_findjoininfo.c
 CSRCS += pthread_release.c pthread_setschedprio.c
 
diff --git a/sched/signal/Make.defs b/sched/signal/Make.defs
index eea4a23139..d38a0b554f 100644
--- a/sched/signal/Make.defs
+++ b/sched/signal/Make.defs
@@ -20,7 +20,7 @@
 
 CSRCS += sig_initialize.c
 CSRCS += sig_action.c sig_procmask.c sig_pending.c sig_suspend.c
-CSRCS += sig_kill.c sig_queue.c sig_waitinfo.c sig_timedwait.c
+CSRCS += sig_kill.c sig_tgkill.c sig_queue.c sig_waitinfo.c sig_timedwait.c
 CSRCS += sig_findaction.c sig_allocpendingsigaction.c
 CSRCS += sig_releasependingsigaction.c sig_unmaskpendingsignal.c
 CSRCS += sig_removependingsignal.c sig_releasependingsignal.c sig_lowest.c
diff --git a/sched/pthread/pthread_kill.c b/sched/signal/sig_tgkill.c
similarity index 72%
rename from sched/pthread/pthread_kill.c
rename to sched/signal/sig_tgkill.c
index b8b2295d00..3f09ca248d 100644
--- a/sched/pthread/pthread_kill.c
+++ b/sched/signal/sig_tgkill.c
@@ -1,5 +1,5 @@
 /****************************************************************************
- * sched/pthread/pthread_kill.c
+ * sched/signal/sig_tgkill.c
  *
  * Licensed to the Apache Software Foundation (ASF) under one or more
  * contributor license agreements.  See the NOTICE file distributed with
@@ -24,11 +24,8 @@
 
 #include <nuttx/config.h>
 
-#include <sys/types.h>
 #include <signal.h>
-#include <pthread.h>
 #include <errno.h>
-#include <debug.h>
 
 #include <nuttx/sched.h>
 #include <nuttx/signal.h>
@@ -41,22 +38,23 @@
  ****************************************************************************/
 
 /****************************************************************************
- * Name: pthread_kill
+ * Name: tgkill
  *
  * Description:
- *   The pthread_kill() system call can be used to send any signal to a
- *   thread.  See nxsig_kill() for further information as this is just a
- *   simple wrapper around the nxsig_kill() function.
+ *   The tgkill() system call can be used to send any signal to a thread.
+ *   See kill() for further information as this is just a simple wrapper
+ *   around the kill() function.
  *
  * Input Parameters:
- *   thread - The id of the thread to receive the signal. Only positive,
- *     non-zero values of 'thread' are supported.
+ *   gid   - The id of the task to receive the signal.
+ *   tid   - The id of the thread to receive the signal. Only positive,
+ *           non-zero values of 'tid' are supported.
  *   signo - The signal number to send.  If 'signo' is zero, no signal is
- *    sent, but all error checking is performed.
+ *           sent, but all error checking is performed.
  *
  * Returned Value:
- *    On success the signal was send and zero is returned. On error one
- *    of the following error numbers is returned.
+ *    On success the signal was send and zero is returned. On error -1 is
+ *    returned, and errno is set one of the following error numbers:
  *
  *    EINVAL An invalid signal was specified.
  *    EPERM  The thread does not have permission to send the
@@ -67,14 +65,13 @@
  *
  ****************************************************************************/
 
-int pthread_kill(pthread_t thread, int signo)
+int tgkill(pid_t pid, pid_t tid, int signo)
 {
 #ifdef HAVE_GROUP_MEMBERS
-  /* If group members are supported then pthread_kill() differs from
-   * nxsig_kill().  nxsig_kill(), in this case, must follow the POSIX rules
-   * for delivery of signals in the group environment.  Otherwise,
-   * nxsig_kill(), like pthread_kill() will just deliver the signal to the
-   * thread ID it is requested to use.
+  /* If group members are supported then tgkill() differs from kill().
+   * kill(), in this case, must follow the POSIX rules for delivery of
+   * signals in the group environment.  Otherwise, kill(), like tgkill()
+   * will just deliver the signal to the thread ID it is requested to use.
    */
 
 #ifdef CONFIG_SCHED_HAVE_PARENT
@@ -109,7 +106,7 @@ int pthread_kill(pthread_t thread, int signo)
 
   /* Get the TCB associated with the thread */
 
-  stcb = nxsched_get_tcb((pid_t)thread);
+  stcb = nxsched_get_tcb(tid);
   if (!stcb)
     {
       ret = -ESRCH;
@@ -133,14 +130,14 @@ int pthread_kill(pthread_t thread, int signo)
 errout_with_lock:
   sched_unlock();
 errout:
-  return -ret;
+  set_errno(-ret);
+  return ERROR;
 
 #else
-  /* If group members are not supported then pthread_kill is basically the
-   * same as nxsig_kill() other than the sign of the returned value.
+  /* If group members are not supported then tgkill is basically the
+   * same as kill() other than the sign of the returned value.
    */
 
-  int ret = nxsig_kill((pid_t)thread, signo);
-  return (ret < 0) ? -ret : OK;
+  return kill(tid, signo);
 #endif
 }
diff --git a/syscall/syscall.csv b/syscall/syscall.csv
index 01a2d17b20..6d93154b39 100644
--- a/syscall/syscall.csv
+++ b/syscall/syscall.csv
@@ -92,7 +92,6 @@
 "pthread_getaffinity_np","pthread.h","!defined(CONFIG_DISABLE_PTHREAD) && defined(CONFIG_SMP)","int","pthread_t","size_t","FAR cpu_set_t*"
 "pthread_getschedparam","pthread.h","!defined(CONFIG_DISABLE_PTHREAD)","int","pthread_t","FAR int *","FAR struct sched_param *"
 "pthread_join","pthread.h","!defined(CONFIG_DISABLE_PTHREAD)","int","pthread_t","FAR pthread_addr_t *"
-"pthread_kill","pthread.h","!defined(CONFIG_DISABLE_PTHREAD)","int","pthread_t","int"
 "pthread_mutex_consistent","pthread.h","!defined(CONFIG_DISABLE_PTHREAD) && !defined(CONFIG_PTHREAD_MUTEX_UNSAFE)","int","FAR pthread_mutex_t *"
 "pthread_mutex_destroy","pthread.h","!defined(CONFIG_DISABLE_PTHREAD)","int","FAR pthread_mutex_t *"
 "pthread_mutex_init","pthread.h","!defined(CONFIG_DISABLE_PTHREAD)","int","FAR pthread_mutex_t *","FAR const pthread_mutexattr_t *"
@@ -174,6 +173,7 @@
 "task_spawn","nuttx/spawn.h","!defined(CONFIG_BUILD_KERNEL)","int","FAR const char *","main_t","FAR const posix_spawn_file_actions_t *","FAR const posix_spawnattr_t *","FAR char * const []|FAR char * const *","FAR char * const []|FAR char * const *"
 "task_testcancel","sched.h","defined(CONFIG_CANCELLATION_POINTS)","void"
 "task_tls_alloc","nuttx/tls.h","CONFIG_TLS_TASK_NELEM > 0","int","tls_dtor_t"
+"tgkill","signal.h","","int","pid_t","pid_t","int"
 "timer_create","time.h","!defined(CONFIG_DISABLE_POSIX_TIMERS)","int","clockid_t","FAR struct sigevent *","FAR timer_t *"
 "timer_delete","time.h","!defined(CONFIG_DISABLE_POSIX_TIMERS)","int","timer_t"
 "timer_getoverrun","time.h","!defined(CONFIG_DISABLE_POSIX_TIMERS)","int","timer_t"