tls: Move pthread key destructor to libc

Signed-off-by: Huang Qi <huangqi3@xiaomi.com>
This commit is contained in:
Huang Qi 2021-06-05 23:16:27 +08:00 committed by patacongo
parent c3c6bc3e71
commit cc514d7791
14 changed files with 116 additions and 97 deletions

View File

@ -530,14 +530,6 @@ struct task_group_s
FAR struct join_s *tg_jointail; /* Tail of a list of join data */ FAR struct join_s *tg_jointail; /* Tail of a list of join data */
#endif #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 ********************************************/ /* POSIX Signal Control Fields ********************************************/
sq_queue_t tg_sigactionq; /* List of actions for signals */ sq_queue_t tg_sigactionq; /* List of actions for signals */

View File

@ -118,6 +118,13 @@ struct task_info_s
struct tls_info_s ta_tls; /* Must be first field */ struct tls_info_s ta_tls; /* Must be first field */
#ifndef CONFIG_BUILD_KERNEL #ifndef CONFIG_BUILD_KERNEL
struct getopt_s ta_getopt; /* Globals used by getopt() */ 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 #endif
}; };
@ -320,4 +327,20 @@ void tls_destruct(void);
FAR struct task_info_s *task_get_info(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 */ #endif /* __INCLUDE_NUTTX_TLS_H */

View File

@ -294,14 +294,6 @@ SYSCALL_LOOKUP(telldir, 1)
SYSCALL_LOOKUP(shmdt, 1) SYSCALL_LOOKUP(shmdt, 1)
#endif #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 */ /* The following are defined if pthreads are enabled */
#ifndef CONFIG_DISABLE_PTHREAD #ifndef CONFIG_DISABLE_PTHREAD

View File

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

View File

@ -22,6 +22,9 @@ CSRCS += task_getinfo.c
ifneq ($(CONFIG_TLS_NELEM),0) ifneq ($(CONFIG_TLS_NELEM),0)
CSRCS += tls_setvalue.c tls_getvalue.c tls_destruct.c 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 endif
ifneq ($(CONFIG_TLS_ALIGNED),y) ifneq ($(CONFIG_TLS_ALIGNED),y)

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -53,11 +53,6 @@ ifneq ($(CONFIG_BUILD_FLAT),y)
CSRCS += group_malloc.c group_zalloc.c group_free.c CSRCS += group_malloc.c group_zalloc.c group_free.c
endif 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 # Include group build support
DEPPATH += --dep-path group DEPPATH += --dep-path group

View File

@ -142,6 +142,14 @@ int nxtask_init(FAR struct task_tcb_s *tcb, const char *name, int priority,
DEBUGASSERT(info == tcb->cmn.stack_alloc_ptr); 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 */ /* Initialize the task control block */
ret = nxtask_setup_scheduler(tcb, priority, nxtask_start, ret = nxtask_setup_scheduler(tcb, priority, nxtask_start,

View File

@ -35,6 +35,7 @@
#include <nuttx/arch.h> #include <nuttx/arch.h>
#include <nuttx/sched.h> #include <nuttx/sched.h>
#include <nuttx/signal.h> #include <nuttx/signal.h>
#include <nuttx/tls.h>
#include "sched/sched.h" #include "sched/sched.h"
#include "pthread/pthread.h" #include "pthread/pthread.h"
@ -721,3 +722,28 @@ int nxtask_setup_arguments(FAR struct task_tcb_s *tcb, FAR const char *name,
return nxtask_setup_stackargs(tcb, argv); 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,11 +177,6 @@
"timer_getoverrun","time.h","!defined(CONFIG_DISABLE_POSIX_TIMERS)","int","timer_t" "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_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 *" "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" "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 *" "unlink","unistd.h","!defined(CONFIG_DISABLE_MOUNTPOINT)","int","FAR const char *"
"unsetenv","stdlib.h","!defined(CONFIG_DISABLE_ENVIRON)","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.