libc: Call pthread_exit in user-space by up_pthread_exit

Drop to user-space in kernel/protected build with up_pthread_exit,
now all pthread_cleanup functions executed in user mode.

* A new syscall SYS_pthread_exit added
* A new tcb flag TCB_FLAG_CANCEL_DOING added
* up_pthread_exit implemented for riscv/arm arch

Signed-off-by: Huang Qi <huangqi3@xiaomi.com>
This commit is contained in:
Huang Qi
2021-05-08 18:20:02 +08:00
committed by patacongo
parent 81a01d089b
commit f4a0b7aedd
90 changed files with 533 additions and 118 deletions
+10
View File
@@ -24,6 +24,8 @@
#include <nuttx/config.h>
#include <nuttx/arch.h>
#include <sys/types.h>
#include <unistd.h>
#include <pthread.h>
@@ -82,7 +84,15 @@ int pthread_cancel(pthread_t thread)
if (tcb == this_task())
{
#if !defined(CONFIG_BUILD_FLAT) && defined(__KERNEL__)
tcb->flags &= ~TCB_FLAG_CANCEL_PENDING;
tcb->flags |= TCB_FLAG_CANCEL_DOING;
up_pthread_exit(((FAR struct pthread_tcb_s *)tcb)->exit,
PTHREAD_CANCELED);
#else
pthread_exit(PTHREAD_CANCELED);
#endif
}
/* Complete pending join operations */
+15 -4
View File
@@ -79,6 +79,7 @@ const pthread_attr_t g_default_pthread_attr = PTHREAD_ATTR_INITIALIZER;
* tcb - Address of the new task's TCB
* trampoline - User space pthread startup function
* arg - The argument to provide to the pthread on startup.
* exit - The user-space pthread exit function
*
* Returned Value:
* None
@@ -87,7 +88,8 @@ const pthread_attr_t g_default_pthread_attr = PTHREAD_ATTR_INITIALIZER;
static inline void pthread_tcb_setup(FAR struct pthread_tcb_s *ptcb,
pthread_trampoline_t trampoline,
pthread_addr_t arg)
pthread_addr_t arg,
pthread_exitroutine_t exit)
{
#if CONFIG_TASK_NAME_SIZE > 0
/* Copy the pthread name into the TCB */
@@ -102,6 +104,7 @@ static inline void pthread_tcb_setup(FAR struct pthread_tcb_s *ptcb,
ptcb->trampoline = trampoline;
ptcb->arg = arg;
ptcb->exit = exit;
}
/****************************************************************************
@@ -193,7 +196,12 @@ static void pthread_start(void)
/* The thread has returned (should never happen) */
DEBUGPANIC();
pthread_exit(NULL);
#ifndef CONFIG_BUILD_FLAT
ptcb->cmn.flags &= ~TCB_FLAG_CANCEL_PENDING;
ptcb->cmn.flags |= TCB_FLAG_CANCEL_DOING;
up_pthread_exit(ptcb->exit, NULL);
#endif
}
/****************************************************************************
@@ -215,6 +223,7 @@ static void pthread_start(void)
* for the new thread
* entry - The new thread starts execution by invoking entry
* arg - It is passed as the sole argument of entry
* exit - The user-space pthread exit function
*
* Returned Value:
* OK (0) on success; a (non-negated) errno value on failure. The errno
@@ -224,7 +233,8 @@ static void pthread_start(void)
int nx_pthread_create(pthread_trampoline_t trampoline, FAR pthread_t *thread,
FAR const pthread_attr_t *attr,
pthread_startroutine_t entry, pthread_addr_t arg)
pthread_startroutine_t entry, pthread_addr_t arg,
pthread_exitroutine_t exit)
{
FAR struct pthread_tcb_s *ptcb;
FAR struct tls_info_s *info;
@@ -237,6 +247,7 @@ int nx_pthread_create(pthread_trampoline_t trampoline, FAR pthread_t *thread,
bool group_joined = false;
DEBUGASSERT(trampoline != NULL);
DEBUGASSERT(exit != NULL);
/* If attributes were not supplied, use the default attributes */
@@ -450,7 +461,7 @@ int nx_pthread_create(pthread_trampoline_t trampoline, FAR pthread_t *thread,
* passed by value
*/
pthread_tcb_setup(ptcb, trampoline, arg);
pthread_tcb_setup(ptcb, trampoline, arg, exit);
/* Join the parent's task group */