Skip to content

Commit

Permalink
pthread: add tls exit flags when pthread_exit()
Browse files Browse the repository at this point in the history
If receive SIGKILL during pthread_exit, pthread_exit may be
called twice, so add tls flags to prevent repeated calls.

Signed-off-by: ligd <[email protected]>
  • Loading branch information
GUIDINGLI authored and zyfeier committed Oct 2, 2024
1 parent 38ddf7d commit 0bdf6b6
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 1 deletion.
3 changes: 3 additions & 0 deletions include/nuttx/tls.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@
# define TLS_INFO(sp) ((FAR struct tls_info_s *)((sp) & ~TLS_STACK_MASK))
#endif

#define TLS_THREAD_EIXT (1 << 0)

/****************************************************************************
* Public Types
****************************************************************************/
Expand Down Expand Up @@ -218,6 +220,7 @@ struct tls_info_s

uint16_t tl_size; /* Actual size with alignments */
int tl_errno; /* Per-thread error number */
int flags; /* Per-thread flags */
};

/****************************************************************************
Expand Down
4 changes: 4 additions & 0 deletions libs/libc/pthread/pthread_exit.c
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,10 @@

void pthread_exit(FAR void *exit_value)
{
FAR struct tls_info_s *info = tls_get_info();

info->flags |= TLS_THREAD_EIXT;

/* Mark the pthread as non-cancelable to avoid additional calls to
* pthread_exit() due to any cancellation point logic that might get
* kicked off by actions taken during pthread_exit processing.
Expand Down
8 changes: 7 additions & 1 deletion sched/signal/sig_default.c
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
#include <nuttx/sched.h>
#include <nuttx/spinlock.h>
#include <nuttx/signal.h>
#include <nuttx/tls.h>

#include "group/group.h"
#include "sched/sched.h"
Expand Down Expand Up @@ -230,12 +231,17 @@ static void nxsig_abnormal_termination(int signo)

if ((rtcb->flags & TCB_FLAG_TTYPE_MASK) == TCB_FLAG_TTYPE_PTHREAD)
{
FAR struct tls_info_s *info = tls_get_info();

/* Exit the final thread of the task group.
*
* REVISIT: This will not work if HAVE_GROUP_MEMBERS is not set.
*/

pthread_exit(NULL);
if ((info->flags & TLS_THREAD_EIXT) == 0)
{
pthread_exit(NULL);
}
}
else
#endif
Expand Down

0 comments on commit 0bdf6b6

Please sign in to comment.