sched: Implement tkill/tgkill

https://linux.die.net/man/2/tgkill

Signed-off-by: Xiang Xiao <xiaoxiang@xiaomi.com>
This commit is contained in:
Xiang Xiao 2023-01-24 06:52:51 +08:00 committed by Petro Karashchenko
parent 34ac8b2e07
commit 819fbd22cc
9 changed files with 102 additions and 62 deletions

View File

@ -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);

View File

@ -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

View File

@ -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

View File

@ -0,0 +1,69 @@
/****************************************************************************
* 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
* this work for additional information regarding copyright ownership. The
* ASF licenses this file to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance with the
* License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*
****************************************************************************/
/****************************************************************************
* Included Files
****************************************************************************/
#include <signal.h>
#include <pthread.h>
#include <errno.h>
/****************************************************************************
* Public Functions
****************************************************************************/
/****************************************************************************
* Name: pthread_kill
*
* 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.
*
* Input Parameters:
* thread - The id of the thread to receive the signal. Only positive,
* non-zero values of 'thread' are supported.
* signo - The signal number to send. If 'signo' is zero, no signal is
* 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.
*
* EINVAL An invalid signal was specified.
* EPERM The thread does not have permission to send the
* signal to the target thread.
* ESRCH No thread could be found corresponding to that
* specified by the given thread ID
* ENOSYS Do not support sending signals to process groups.
*
****************************************************************************/
int pthread_kill(pthread_t thread, int signo)
{
int ret = tkill((pid_t)thread, signo);
if (ret < 0)
{
ret = get_errno();
}
return ret;
}

View File

@ -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);
}

View File

@ -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

View File

@ -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

View File

@ -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
}

View File

@ -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"

Can't render this file because it has a wrong number of fields in line 2.