Revert "tls: Move pthread key destructor to libc"

This reverts commit cc514d7791.

* It introduced a regression.
  https://github.com/apache/incubator-nuttx/issues/3868

* It seems conceptually wrong to have per-process data in
  the main thread's stack.
This commit is contained in:
YAMAMOTO Takashi 2021-06-09 15:53:23 +09:00 committed by Xiang Xiao
parent 8079d9fac3
commit b3e8535ad6
14 changed files with 97 additions and 116 deletions

View File

@ -530,6 +530,14 @@ struct task_group_s
FAR struct join_s *tg_jointail; /* Tail of a list of join data */
#endif
/* Thread local storage ***************************************************/
#if CONFIG_TLS_NELEM > 0
tls_ndxset_t tg_tlsset; /* Set of TLS indexes allocated */
tls_dtor_t tg_tlsdestr[CONFIG_TLS_NELEM]; /* List of TLS destructors */
#endif
/* POSIX Signal Control Fields ********************************************/
sq_queue_t tg_sigactionq; /* List of actions for signals */

View File

@ -118,13 +118,6 @@ struct task_info_s
struct tls_info_s ta_tls; /* Must be first field */
#ifndef CONFIG_BUILD_KERNEL
struct getopt_s ta_getopt; /* Globals used by getopt() */
#endif
/* Thread local storage ***************************************************/
#if CONFIG_TLS_NELEM > 0
sem_t ta_tlssem;
tls_ndxset_t ta_tlsset; /* Set of TLS index allocated */
tls_dtor_t ta_tlsdtor[CONFIG_TLS_NELEM]; /* List of TLS destructors */
#endif
};
@ -327,20 +320,4 @@ void tls_destruct(void);
FAR struct task_info_s *task_get_info(void);
/****************************************************************************
* Name: task_setup_info
*
* Description:
* Setup task_info_s for task
*
* Input Parameters:
* info - New created task_info_s
*
* Returned Value:
* OK on success; ERROR on failure
*
****************************************************************************/
int task_setup_info(FAR struct task_info_s *info);
#endif /* __INCLUDE_NUTTX_TLS_H */

View File

@ -294,6 +294,14 @@ SYSCALL_LOOKUP(telldir, 1)
SYSCALL_LOOKUP(shmdt, 1)
#endif
#if CONFIG_TLS_NELEM > 0
SYSCALL_LOOKUP(tls_alloc, 0)
SYSCALL_LOOKUP(tls_free, 1)
SYSCALL_LOOKUP(tls_get_set, 1)
SYSCALL_LOOKUP(tls_get_dtor, 1)
SYSCALL_LOOKUP(tls_set_dtor, 2)
#endif
/* The following are defined if pthreads are enabled */
#ifndef CONFIG_DISABLE_PTHREAD

View File

@ -89,7 +89,7 @@ int pthread_key_create(FAR pthread_key_t *key,
return OK;
}
return ERROR;
return -tlsindex;
}
#endif /* CONFIG_TLS_NELEM */

View File

@ -22,9 +22,6 @@ CSRCS += task_getinfo.c
ifneq ($(CONFIG_TLS_NELEM),0)
CSRCS += tls_setvalue.c tls_getvalue.c tls_destruct.c
CSRCS += tls_getdtor.c tls_setdtor.c
CSRCS += tls_alloc.c tls_free.c
CSRCS += tls_getset.c
endif
ifneq ($(CONFIG_TLS_ALIGNED),y)

View File

@ -53,6 +53,11 @@ ifneq ($(CONFIG_BUILD_FLAT),y)
CSRCS += group_malloc.c group_zalloc.c group_free.c
endif
ifneq ($(CONFIG_TLS_NELEM),0)
CSRCS += group_tlsalloc.c group_tlsfree.c
CSRCS += group_tlsgetset.c group_tlsgetdtor.c group_tlssetdtor.c
endif
# Include group build support
DEPPATH += --dep-path group

View File

@ -1,5 +1,5 @@
/****************************************************************************
* libs/libc/tls/tls_alloc.c
* sched/group/group_tlsalloc.c
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
@ -32,6 +32,9 @@
#include <nuttx/spinlock.h>
#include <nuttx/tls.h>
#include "sched/sched.h"
#include "group/group.h"
#if CONFIG_TLS_NELEM > 0
/****************************************************************************
@ -49,45 +52,39 @@
*
* Returned Value:
* A TLS index that is unique for use within this task group.
* If unsuccessful, an errno value will be returned and set to errno.
*
****************************************************************************/
int tls_alloc(void)
{
FAR struct task_info_s *tinfo = task_get_info();
FAR struct tcb_s *rtcb = this_task();
FAR struct task_group_s *group = rtcb->group;
irqstate_t flags;
int candidate;
int ret = -EAGAIN;
DEBUGASSERT(tinfo != NULL);
DEBUGASSERT(group != NULL);
/* Search for an unused index. This is done in a critical section here to
* avoid concurrent modification of the group TLS index set.
*/
ret = _SEM_WAIT(&tinfo->ta_tlssem);
if (ERROR == ret)
{
ret = -get_errno();
goto errout_with_errno;
}
flags = spin_lock_irqsave(NULL);
for (candidate = 0; candidate < CONFIG_TLS_NELEM; candidate++)
{
/* Is this candidate index available? */
tls_ndxset_t mask = (1 << candidate);
if ((tinfo->ta_tlsset & mask) == 0)
if ((group->tg_tlsset & mask) == 0)
{
/* Yes.. allocate the index and break out of the loop */
tinfo->ta_tlsset |= mask;
group->tg_tlsset |= mask;
break;
}
}
_SEM_POST(&tinfo->ta_tlssem);
spin_unlock_irqrestore(NULL, flags);
/* Check if found a valid TLS data index. */
@ -98,8 +95,6 @@ int tls_alloc(void)
ret = candidate;
}
errout_with_errno:
return ret;
}

View File

@ -1,5 +1,5 @@
/****************************************************************************
* libs/libc/tls/tls_free.c
* sched/group/group_tlsfree.c
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
@ -31,6 +31,9 @@
#include <nuttx/spinlock.h>
#include <nuttx/tls.h>
#include "sched/sched.h"
#include "group/group.h"
#if CONFIG_TLS_NELEM > 0
/****************************************************************************
@ -47,40 +50,36 @@
* tlsindex - The previously allocated TLS index to be freed
*
* Returned Value:
* OK is returned on success; a negated errno value will be returned and
* set to errno on failure:
* OK is returned on success; a negated errno value will be returned on
* failure:
*
* -EINVAL - the index to be freed is out of range.
* -EINTR - the wait operation interrupted by signal
* -ECANCELED - the thread was canceled during waiting
*
****************************************************************************/
int tls_free(int tlsindex)
{
FAR struct task_info_s *tinfo = task_get_info();
FAR struct tcb_s *rtcb = this_task();
FAR struct task_group_s *group = rtcb->group;
tls_ndxset_t mask;
irqstate_t flags;
int ret = -EINVAL;
DEBUGASSERT((unsigned)tlsindex < CONFIG_TLS_NELEM && tinfo != NULL);
DEBUGASSERT((unsigned)tlsindex < CONFIG_TLS_NELEM && group != NULL);
if ((unsigned)tlsindex < CONFIG_TLS_NELEM)
{
/* This is done while holding a semaphore here to avoid concurrent
/* This is done in a critical section here to avoid concurrent
* modification of the group TLS index set.
*/
mask = (1 << tlsindex);
ret = _SEM_WAIT(&tinfo->ta_tlssem);
if (OK == ret)
{
DEBUGASSERT((tinfo->ta_tlsset & mask) != 0);
tinfo->ta_tlsset &= ~mask;
_SEM_POST(&tinfo->ta_tlssem);
}
else
{
ret = -get_errno();
}
flags = spin_lock_irqsave(NULL);
DEBUGASSERT((group->tg_tlsset & mask) != 0);
group->tg_tlsset &= ~mask;
spin_unlock_irqrestore(NULL, flags);
ret = OK;
}
return ret;

View File

@ -1,5 +1,5 @@
/****************************************************************************
* libs/libc/tls/tls_getdtor.c
* sched/group/group_tlsgetdtor.c
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
@ -32,6 +32,9 @@
#include <nuttx/tls.h>
#include <arch/tls.h>
#include "sched/sched.h"
#include "group/group.h"
#if CONFIG_TLS_NELEM > 0
/****************************************************************************
@ -42,7 +45,7 @@
* Name: tls_get_dtor
*
* Description:
* Get the TLS element destructor associated with the 'tlsindex' to 'dtor'
* Get the TLS element destructor associated with the 'tlsindex' to 'destr'
*
* Input Parameters:
* tlsindex - Index of TLS data destructor to get
@ -54,15 +57,19 @@
tls_dtor_t tls_get_dtor(int tlsindex)
{
FAR struct task_info_s *tinfo = task_get_info();
tls_dtor_t dtor;
FAR struct tcb_s *rtcb = this_task();
FAR struct task_group_s *group = rtcb->group;
irqstate_t flags;
tls_dtor_t destr;
DEBUGASSERT(tinfo != NULL);
DEBUGASSERT(group != NULL);
DEBUGASSERT(tlsindex >= 0 && tlsindex < CONFIG_TLS_NELEM);
dtor = tinfo->ta_tlsdtor[tlsindex];
flags = spin_lock_irqsave(NULL);
destr = group->tg_tlsdestr[tlsindex];
spin_unlock_irqrestore(NULL, flags);
return dtor;
return destr;
}
#endif /* CONFIG_TLS_NELEM > 0 */

View File

@ -1,5 +1,5 @@
/****************************************************************************
* libs/libc/tls/tls_getset.c
* sched/group/group_tlsgetset.c
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
@ -32,6 +32,9 @@
#include <nuttx/tls.h>
#include <arch/tls.h>
#include "sched/sched.h"
#include "group/group.h"
#if CONFIG_TLS_NELEM > 0
/****************************************************************************
@ -53,12 +56,16 @@
tls_ndxset_t tls_get_set(void)
{
FAR struct task_info_s *tinfo = task_get_info();
FAR struct tcb_s *rtcb = this_task();
FAR struct task_group_s *group = rtcb->group;
irqstate_t flags;
tls_ndxset_t tlsset;
DEBUGASSERT(tinfo != NULL);
DEBUGASSERT(group != NULL);
tlsset = tinfo->ta_tlsset;
flags = spin_lock_irqsave(NULL);
tlsset = group->tg_tlsset;
spin_unlock_irqrestore(NULL, flags);
return tlsset;
}

View File

@ -1,5 +1,5 @@
/****************************************************************************
* libs/libc/tls/tls_setdtor.c
* sched/group/group_tlssetdtor.c
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
@ -32,6 +32,9 @@
#include <nuttx/tls.h>
#include <arch/tls.h>
#include "sched/sched.h"
#include "group/group.h"
#if CONFIG_TLS_NELEM > 0
/****************************************************************************
@ -42,11 +45,11 @@
* Name: tls_set_dtor
*
* Description:
* Set the TLS element destructor associated with the 'tlsindex' to 'dtor'
* Set the TLS element destructor associated with the 'tlsindex' to 'destr'
*
* Input Parameters:
* tlsindex - Index of TLS data destructor to set
* dtor - The dtor of TLS data element
* destr - The destr of TLS data element
*
* Returned Value:
* Zero is returned on success, a negated errno value is return on
@ -56,14 +59,18 @@
*
****************************************************************************/
int tls_set_dtor(int tlsindex, tls_dtor_t dtor)
int tls_set_dtor(int tlsindex, tls_dtor_t destr)
{
FAR struct task_info_s *tinfo = task_get_info();
FAR struct tcb_s *rtcb = this_task();
FAR struct task_group_s *group = rtcb->group;
irqstate_t flags;
DEBUGASSERT(tinfo != NULL);
DEBUGASSERT(group != NULL);
DEBUGASSERT(tlsindex >= 0 && tlsindex < CONFIG_TLS_NELEM);
tinfo->ta_tlsdtor[tlsindex] = dtor;
flags = spin_lock_irqsave(NULL);
group->tg_tlsdestr[tlsindex] = destr;
spin_unlock_irqrestore(NULL, flags);
return OK;
}

View File

@ -142,14 +142,6 @@ int nxtask_init(FAR struct task_tcb_s *tcb, const char *name, int priority,
DEBUGASSERT(info == tcb->cmn.stack_alloc_ptr);
ret = task_setup_info(info);
if (ret < OK)
{
ret = -EINVAL;
goto errout_with_group;
}
/* Initialize the task control block */
ret = nxtask_setup_scheduler(tcb, priority, nxtask_start,

View File

@ -35,7 +35,6 @@
#include <nuttx/arch.h>
#include <nuttx/sched.h>
#include <nuttx/signal.h>
#include <nuttx/tls.h>
#include "sched/sched.h"
#include "pthread/pthread.h"
@ -722,28 +721,3 @@ int nxtask_setup_arguments(FAR struct task_tcb_s *tcb, FAR const char *name,
return nxtask_setup_stackargs(tcb, argv);
}
/****************************************************************************
* Name: task_setup_info
*
* Description:
* Setup task_info_s for task
*
* Input Parameters:
* info - New created task_info_s
*
* Returned Value:
* OK on success; ERROR on failure
*
****************************************************************************/
int task_setup_info(FAR struct task_info_s *info)
{
int ret = OK;
#if CONFIG_TLS_NELEM > 0
ret = _SEM_INIT(&info->ta_tlssem, 0, 1);
#endif
return ret;
}

View File

@ -177,6 +177,11 @@
"timer_getoverrun","time.h","!defined(CONFIG_DISABLE_POSIX_TIMERS)","int","timer_t"
"timer_gettime","time.h","!defined(CONFIG_DISABLE_POSIX_TIMERS)","int","timer_t","FAR struct itimerspec *"
"timer_settime","time.h","!defined(CONFIG_DISABLE_POSIX_TIMERS)","int","timer_t","int","FAR const struct itimerspec *","FAR struct itimerspec *"
"tls_alloc","nuttx/tls.h","CONFIG_TLS_NELEM > 0","int"
"tls_free","nuttx/tls.h","CONFIG_TLS_NELEM > 0","int","int"
"tls_get_dtor","nuttx/tls.h","CONFIG_TLS_NELEM > 0","tls_dtor_t","int"
"tls_get_set","nuttx/tls.h","CONFIG_TLS_NELEM > 0","tls_ndxset_t"
"tls_set_dtor","nuttx/tls.h","CONFIG_TLS_NELEM > 0","int","int","tls_dtor_t"
"umount2","sys/mount.h","!defined(CONFIG_DISABLE_MOUNTPOINT)","int","FAR const char *","unsigned int"
"unlink","unistd.h","!defined(CONFIG_DISABLE_MOUNTPOINT)","int","FAR const char *"
"unsetenv","stdlib.h","!defined(CONFIG_DISABLE_ENVIRON)","int","FAR const char *"

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