tls: Merge tls_xxx into pthread_keyxxx

it's always better to provide the standard api directly

Signed-off-by: Xiang Xiao <xiaoxiang@xiaomi.com>
This commit is contained in:
Xiang Xiao 2022-05-27 04:55:42 +08:00 committed by Petro Karashchenko
parent c19d37adf0
commit bd76e69f8d
10 changed files with 128 additions and 432 deletions

View File

@ -209,88 +209,6 @@ struct tls_info_s
* Public Function Prototypes
****************************************************************************/
/****************************************************************************
* Name: tls_alloc
*
* Description:
* Allocate a group-unique TLS data index
*
* Input Parameters:
* dtor - The destructor of TLS data element
*
* Returned Value:
* A TLS index that is unique for use within this task group.
*
****************************************************************************/
#if CONFIG_TLS_NELEM > 0
int tls_alloc(CODE void (*dtor)(FAR void *));
#endif
/****************************************************************************
* Name: tls_free
*
* Description:
* Release a group-unique TLS data index previous obtained by tls_alloc()
*
* Input Parameters:
* tlsindex - The previously allocated TLS index to be freed
*
* Returned Value:
* OK is returned on success; a negated errno value will be returned on
* failure:
*
* -EINVAL - the index to be freed is out of range.
*
****************************************************************************/
#if CONFIG_TLS_NELEM > 0
int tls_free(int tlsindex);
#endif
/****************************************************************************
* Name: tls_get_value
*
* Description:
* Return an the TLS data value associated with the 'tlsindx'
*
* Input Parameters:
* tlsindex - Index of TLS data element to return
*
* Returned Value:
* The value of TLS element associated with 'tlsindex'. Errors are not
* reported. Zero is returned in the event of an error, but zero may also
* be valid value and returned when there is no error. The only possible
* error would be if tlsindex < 0 or tlsindex >=CONFIG_TLS_NELEM.
*
****************************************************************************/
#if CONFIG_TLS_NELEM > 0
uintptr_t tls_get_value(int tlsindex);
#endif
/****************************************************************************
* Name: tls_set_value
*
* Description:
* Set the TLS element associated with the 'tlsindex' to 'tlsvalue'
*
* Input Parameters:
* tlsindex - Index of TLS data element to set
* tlsvalue - The new value of the TLS data element
*
* Returned Value:
* Zero is returned on success, a negated errno value is return on
* failure:
*
* EINVAL - tlsindex is not in range.
*
****************************************************************************/
#if CONFIG_TLS_NELEM > 0
int tls_set_value(int tlsindex, uintptr_t tlsvalue);
#endif
#if CONFIG_TLS_TASK_NELEM > 0
/****************************************************************************

View File

@ -25,9 +25,13 @@
#include <nuttx/config.h>
#include <pthread.h>
#include <assert.h>
#include <nuttx/arch.h>
#include <nuttx/tls.h>
#if CONFIG_TLS_NELEM > 0
/****************************************************************************
* Public Functions
****************************************************************************/
@ -59,5 +63,23 @@
FAR void *pthread_getspecific(pthread_key_t key)
{
return (FAR void *)tls_get_value((int)key);
FAR struct tls_info_s *info;
FAR void *ret = NULL;
DEBUGASSERT(key >= 0 && key < CONFIG_TLS_NELEM);
if (key >= 0 && key < CONFIG_TLS_NELEM)
{
/* Get the TLS info structure from the current threads stack */
info = up_tls_info();
DEBUGASSERT(info != NULL);
/* Get the element value from the TLS info. */
ret = (FAR void *)info->tl_elem[key];
}
return ret;
}
#endif /* CONFIG_TLS_NELEM */

View File

@ -26,7 +26,9 @@
#include <pthread.h>
#include <assert.h>
#include <errno.h>
#include <nuttx/semaphore.h>
#include <nuttx/tls.h>
#if CONFIG_TLS_NELEM > 0
@ -70,23 +72,46 @@
int pthread_key_create(FAR pthread_key_t *key,
CODE void (*destructor)(FAR void *))
{
int tlsindex;
FAR struct task_info_s *info = task_get_info();
int candidate;
int ret;
DEBUGASSERT(key != NULL);
DEBUGASSERT(info != NULL);
/* Allocate a TLS index */
/* Search for an unused index. This is done in a critical section here to
* avoid concurrent modification of the group TLS index set.
*/
tlsindex = tls_alloc(destructor);
ret = _SEM_WAIT(&info->ta_sem);
/* Check if found a TLS index. */
if (tlsindex >= 0)
if (ret < 0)
{
*key = tlsindex;
return OK;
ret = _SEM_ERRNO(ret);
return ret;
}
return -tlsindex;
ret = EAGAIN;
for (candidate = 0; candidate < CONFIG_TLS_NELEM; candidate++)
{
/* Is this candidate index available? */
tls_ndxset_t mask = (tls_ndxset_t)1 << candidate;
if ((info->ta_tlsset & mask) == 0)
{
/* Yes.. allocate the index and break out of the loop */
info->ta_tlsset |= mask;
info->ta_tlsdtor[candidate] = destructor;
*key = candidate;
ret = OK;
break;
}
}
_SEM_POST(&info->ta_sem);
return ret;
}
#endif /* CONFIG_TLS_NELEM */

View File

@ -25,9 +25,14 @@
#include <nuttx/config.h>
#include <pthread.h>
#include <assert.h>
#include <errno.h>
#include <nuttx/semaphore.h>
#include <nuttx/tls.h>
#if CONFIG_TLS_NELEM > 0
/****************************************************************************
* Public Functions
****************************************************************************/
@ -52,8 +57,33 @@
int pthread_key_delete(pthread_key_t key)
{
/* Free the TLS index */
FAR struct task_info_s *info = task_get_info();
tls_ndxset_t mask;
int ret = EINVAL;
int ret = tls_free((int)key);
return ret < 0 ? -ret : 0;
DEBUGASSERT(info != NULL);
DEBUGASSERT(key >= 0 && key < CONFIG_TLS_NELEM);
if (key >= 0 && key < CONFIG_TLS_NELEM)
{
/* This is done while holding a semaphore here to avoid concurrent
* modification of the group TLS index set.
*/
mask = (tls_ndxset_t)1 << key;
ret = _SEM_WAIT(&info->ta_sem);
if (ret == OK)
{
DEBUGASSERT((info->ta_tlsset & mask) != 0);
info->ta_tlsset &= ~mask;
_SEM_POST(&info->ta_sem);
}
else
{
ret = _SEM_ERRNO(ret);
}
}
return ret;
}
#endif /* CONFIG_TLS_NELEM */

View File

@ -25,7 +25,9 @@
#include <nuttx/config.h>
#include <pthread.h>
#include <assert.h>
#include <nuttx/arch.h>
#include <nuttx/tls.h>
#if CONFIG_TLS_NELEM > 0
@ -72,9 +74,23 @@
int pthread_setspecific(pthread_key_t key, FAR const void *value)
{
int ret = tls_set_value((int)key, (uintptr_t)value);
return ret < 0 ? -ret : 0;
FAR struct tls_info_s *info;
DEBUGASSERT(key >= 0 && key < CONFIG_TLS_NELEM);
if (key >= 0 && key < CONFIG_TLS_NELEM)
{
/* Get the TLS info structure from the current threads stack */
info = up_tls_info();
DEBUGASSERT(info != NULL);
/* Set the element value int the TLS info. */
info->tl_elem[key] = (uintptr_t)value;
return OK;
}
return EINVAL;
}
#endif /* CONFIG_TLS_NELEM */

View File

@ -20,13 +20,15 @@
CSRCS += task_getinfo.c tls_getinfo.c
ifneq ($(CONFIG_TLS_TASK_NELEM),0)
CSRCS += task_tls.c
endif
ifneq ($(CONFIG_TLS_NELEM),0)
CSRCS += tls_alloc.c tls_free.c
CSRCS += tls_setvalue.c tls_getvalue.c tls_destruct.c
CSRCS += tls_destruct.c
endif
# Include tls build support
DEPPATH += --dep-path tls
VPATH += :tls

View File

@ -1,5 +1,5 @@
/****************************************************************************
* libs/libc/tls/tls_setvalue.c
* libs/libc/tls/task_tls.c
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
@ -24,61 +24,44 @@
#include <nuttx/config.h>
#include <assert.h>
#include <errno.h>
#include <nuttx/arch.h>
#include <nuttx/tls.h>
#if CONFIG_TLS_NELEM > 0
#if CONFIG_TLS_TASK_NELEM > 0
/****************************************************************************
* Public Functions
****************************************************************************/
/****************************************************************************
* Name: tls_set_value
* Name: task_tls_get_value
*
* Description:
* Set the TLS element associated with the 'tlsindex' to 'tlsvalue'
* Return an the task local storage data value associated with 'tlsindx'
*
* Input Parameters:
* tlsindex - Index of TLS data element to set
* tlsvalue - The new value of the TLS data element
* tlsindex - Index of task local storage data element to return
*
* Returned Value:
* Zero is returned on success, a negated errno value is return on
* failure:
*
* EINVAL - tlsindex is not in range.
* The value of TLS element associated with 'tlsindex'. Errors are not
* reported. Zero is returned in the event of an error, but zero may also
* be valid value and returned when there is no error. The only possible
* error would be if tlsindex < 0 or tlsindex >=CONFIG_TLS_TASK_NELEM.
*
****************************************************************************/
int tls_set_value(int tlsindex, uintptr_t tlsvalue)
uintptr_t task_tls_get_value(int tlsindex)
{
FAR struct tls_info_s *info;
FAR struct task_info_s *info = task_get_info();
DEBUGASSERT(tlsindex >= 0 && tlsindex < CONFIG_TLS_NELEM);
if (tlsindex >= 0 && tlsindex < CONFIG_TLS_NELEM)
if (tlsindex >= 0 && tlsindex < CONFIG_TLS_TASK_NELEM)
{
/* Get the TLS info structure from the current threads stack */
info = up_tls_info();
DEBUGASSERT(info != NULL);
/* Set the element value int the TLS info. */
info->tl_elem[tlsindex] = tlsvalue;
return OK;
return info->ta_telem[tlsindex];
}
return -EINVAL;
return 0;
}
#endif /* CONFIG_TLS_NELEM > 0 */
#if CONFIG_TLS_TASK_NELEM > 0
/****************************************************************************
* Name: task_tls_set_value
*

View File

@ -1,100 +0,0 @@
/****************************************************************************
* libs/libc/tls/tls_alloc.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 <nuttx/config.h>
#include <sched.h>
#include <errno.h>
#include <assert.h>
#include <debug.h>
#include <nuttx/spinlock.h>
#include <nuttx/tls.h>
#include <nuttx/sched.h>
#if CONFIG_TLS_NELEM > 0
/****************************************************************************
* Public Functions
****************************************************************************/
/****************************************************************************
* Name: tls_alloc
*
* Description:
* Allocate a group-unique TLS data index
*
* Input Parameters:
* None
*
* 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(CODE void (*dtor)(FAR void *))
{
FAR struct task_info_s *info = task_get_info();
int candidate;
int ret;
DEBUGASSERT(info);
/* 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(&info->ta_sem);
if (ret < 0)
{
ret = _SEM_ERRVAL(ret);
return ret;
}
ret = -EAGAIN;
for (candidate = 0; candidate < CONFIG_TLS_NELEM; candidate++)
{
/* Is this candidate index available? */
tls_ndxset_t mask = (tls_ndxset_t)1 << candidate;
if ((info->ta_tlsset & mask) == 0)
{
/* Yes.. allocate the index and break out of the loop */
info->ta_tlsset |= mask;
info->ta_tlsdtor[candidate] = dtor;
ret = candidate;
break;
}
}
_SEM_POST(&info->ta_sem);
return ret;
}
#endif /* CONFIG_TLS_NELEM > 0 */

View File

@ -1,90 +0,0 @@
/****************************************************************************
* libs/libc/tls/tls_free.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 <nuttx/config.h>
#include <sched.h>
#include <errno.h>
#include <assert.h>
#include <nuttx/spinlock.h>
#include <nuttx/tls.h>
#include <nuttx/sched.h>
#if CONFIG_TLS_NELEM > 0
/****************************************************************************
* Public Functions
****************************************************************************/
/****************************************************************************
* Name: tls_free
*
* Description:
* Release a group-unique TLS data index previous obtained by tls_alloc()
*
* Input Parameters:
* tlsindex - The previously allocated TLS index to be freed
*
* Returned Value:
* OK is returned on success;
* If unsuccessful an errno value will be returned and set to errno.
* -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 *info = task_get_info();
tls_ndxset_t mask;
int ret = -EINVAL;
DEBUGASSERT((unsigned)tlsindex < CONFIG_TLS_NELEM && info != NULL);
if ((unsigned)tlsindex < CONFIG_TLS_NELEM)
{
/* This is done while holding a semaphore here to avoid concurrent
* modification of the group TLS index set.
*/
mask = (1 << tlsindex);
ret = _SEM_WAIT(&info->ta_sem);
if (ret == OK)
{
DEBUGASSERT((info->ta_tlsset & mask) != 0);
info->ta_tlsset &= ~mask;
_SEM_POST(&info->ta_sem);
}
else
{
ret = _SEM_ERRVAL(ret);
}
}
return ret;
}
#endif /* CONFIG_TLS_NELEM > 0 */

View File

@ -1,110 +0,0 @@
/****************************************************************************
* libs/libc/tls/tls_getvalue.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 <nuttx/config.h>
#include <assert.h>
#include <errno.h>
#include <nuttx/arch.h>
#include <nuttx/tls.h>
#if CONFIG_TLS_NELEM > 0
/****************************************************************************
* Public Functions
****************************************************************************/
/****************************************************************************
* Name: tls_get_value
*
* Description:
* Return an the TLS data value associated with the 'tlsindx'
*
* Input Parameters:
* tlsindex - Index of TLS data element to return
*
* Returned Value:
* The value of TLS element associated with 'tlsindex'. Errors are not
* reported. Zero is returned in the event of an error, but zero may also
* be valid value and returned when there is no error. The only possible
* error would be if tlsindex < 0 or tlsindex >=CONFIG_TLS_NELEM.
*
****************************************************************************/
uintptr_t tls_get_value(int tlsindex)
{
FAR struct tls_info_s *info;
uintptr_t ret = 0;
DEBUGASSERT(tlsindex >= 0 && tlsindex < CONFIG_TLS_NELEM);
if (tlsindex >= 0 && tlsindex < CONFIG_TLS_NELEM)
{
/* Get the TLS info structure from the current threads stack */
info = up_tls_info();
DEBUGASSERT(info != NULL);
/* Get the element value from the TLS info. */
ret = info->tl_elem[tlsindex];
}
return ret;
}
#endif /* CONFIG_TLS_NELEM > 0 */
#if CONFIG_TLS_TASK_NELEM > 0
/****************************************************************************
* Name: task_tls_get_value
*
* Description:
* Return an the task local storage data value associated with 'tlsindx'
*
* Input Parameters:
* tlsindex - Index of task local storage data element to return
*
* Returned Value:
* The value of TLS element associated with 'tlsindex'. Errors are not
* reported. Zero is returned in the event of an error, but zero may also
* be valid value and returned when there is no error. The only possible
* error would be if tlsindex < 0 or tlsindex >=CONFIG_TLS_TASK_NELEM.
*
****************************************************************************/
uintptr_t task_tls_get_value(int tlsindex)
{
FAR struct task_info_s *info = task_get_info();
if (tlsindex >= 0 && tlsindex < CONFIG_TLS_TASK_NELEM)
{
return info->ta_telem[tlsindex];
}
return 0;
}
#endif