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
+1 -1
View File
@@ -89,7 +89,7 @@ int pthread_key_create(FAR pthread_key_t *key,
return OK;
}
return -tlsindex;
return ERROR;
}
#endif /* CONFIG_TLS_NELEM */
+3
View File
@@ -22,6 +22,9 @@ 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)
+106
View File
@@ -0,0 +1,106 @@
/****************************************************************************
* 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>
#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(void)
{
FAR struct task_info_s *tinfo = task_get_info();
int candidate;
int ret = -EAGAIN;
DEBUGASSERT(tinfo != 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;
}
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)
{
/* Yes.. allocate the index and break out of the loop */
tinfo->ta_tlsset |= mask;
break;
}
}
_SEM_POST(&tinfo->ta_tlssem);
/* Check if found a valid TLS data index. */
if (candidate < CONFIG_TLS_NELEM)
{
/* Yes.. Return the TLS index and success */
ret = candidate;
}
errout_with_errno:
return ret;
}
#endif /* CONFIG_TLS_NELEM > 0 */
+89
View File
@@ -0,0 +1,89 @@
/****************************************************************************
* 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>
#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; a negated errno value will be returned and
* set to errno 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();
tls_ndxset_t mask;
int ret = -EINVAL;
DEBUGASSERT((unsigned)tlsindex < CONFIG_TLS_NELEM && tinfo != 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(&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();
}
}
return ret;
}
#endif /* CONFIG_TLS_NELEM > 0 */
+68
View File
@@ -0,0 +1,68 @@
/****************************************************************************
* libs/libc/tls/tls_getdtor.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 <stdint.h>
#include <assert.h>
#include <nuttx/arch.h>
#include <nuttx/spinlock.h>
#include <nuttx/tls.h>
#include <arch/tls.h>
#if CONFIG_TLS_NELEM > 0
/****************************************************************************
* Public Functions
****************************************************************************/
/****************************************************************************
* Name: tls_get_dtor
*
* Description:
* Get the TLS element destructor associated with the 'tlsindex' to 'dtor'
*
* Input Parameters:
* tlsindex - Index of TLS data destructor to get
*
* Returned Value:
* A non-null destruct function pointer.
*
****************************************************************************/
tls_dtor_t tls_get_dtor(int tlsindex)
{
FAR struct task_info_s *tinfo = task_get_info();
tls_dtor_t dtor;
DEBUGASSERT(tinfo != NULL);
DEBUGASSERT(tlsindex >= 0 && tlsindex < CONFIG_TLS_NELEM);
dtor = tinfo->ta_tlsdtor[tlsindex];
return dtor;
}
#endif /* CONFIG_TLS_NELEM > 0 */
+66
View File
@@ -0,0 +1,66 @@
/****************************************************************************
* libs/libc/tls/tls_getset.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 <stdint.h>
#include <assert.h>
#include <nuttx/arch.h>
#include <nuttx/spinlock.h>
#include <nuttx/tls.h>
#include <arch/tls.h>
#if CONFIG_TLS_NELEM > 0
/****************************************************************************
* Public Functions
****************************************************************************/
/****************************************************************************
* Name: tls_get_set
*
* Description:
* Get the set map of TLE element index.
*
* Input Parameters:
*
* Returned Value:
* TLS element index set map.
*
****************************************************************************/
tls_ndxset_t tls_get_set(void)
{
FAR struct task_info_s *tinfo = task_get_info();
tls_ndxset_t tlsset;
DEBUGASSERT(tinfo != NULL);
tlsset = tinfo->ta_tlsset;
return tlsset;
}
#endif /* CONFIG_TLS_NELEM > 0 */
+71
View File
@@ -0,0 +1,71 @@
/****************************************************************************
* libs/libc/tls/tls_setdtor.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 <stdint.h>
#include <assert.h>
#include <nuttx/arch.h>
#include <nuttx/spinlock.h>
#include <nuttx/tls.h>
#include <arch/tls.h>
#if CONFIG_TLS_NELEM > 0
/****************************************************************************
* Public Functions
****************************************************************************/
/****************************************************************************
* Name: tls_set_dtor
*
* Description:
* Set the TLS element destructor associated with the 'tlsindex' to 'dtor'
*
* Input Parameters:
* tlsindex - Index of TLS data destructor to set
* dtor - The dtor of TLS data element
*
* Returned Value:
* Zero is returned on success, a negated errno value is return on
* failure:
*
* EINVAL - tlsindex is not in range.
*
****************************************************************************/
int tls_set_dtor(int tlsindex, tls_dtor_t dtor)
{
FAR struct task_info_s *tinfo = task_get_info();
DEBUGASSERT(tinfo != NULL);
DEBUGASSERT(tlsindex >= 0 && tlsindex < CONFIG_TLS_NELEM);
tinfo->ta_tlsdtor[tlsindex] = dtor;
return OK;
}
#endif /* CONFIG_TLS_NELEM > 0 */