pthread_cleanup: move clenup down to tls

Signed-off-by: ligd <liguiding1@xiaomi.com>
This commit is contained in:
ligd
2024-02-18 20:15:14 +08:00
committed by Xiang Xiao
parent 38af7001b6
commit ff1b54bff3
32 changed files with 230 additions and 198 deletions
+1 -1
View File
@@ -109,7 +109,7 @@ if(NOT CONFIG_DISABLE_PTHREAD)
list(APPEND SRCS pthread_spinlock.c)
endif()
if(NOT CONFIG_PTHREAD_CLEANUP_STACKSIZE EQUAL 0)
if(NOT CONFIG_TLS_NCLEANUP EQUAL 0)
list(APPEND SRCS pthread_cleanup.c)
endif()
endif()
+1 -1
View File
@@ -70,7 +70,7 @@ ifeq ($(CONFIG_PTHREAD_SPINLOCKS),y)
CSRCS += pthread_spinlock.c
endif
ifneq ($(CONFIG_PTHREAD_CLEANUP_STACKSIZE),0)
ifneq ($(CONFIG_TLS_NCLEANUP),0)
CSRCS += pthread_cleanup.c
endif
+27 -108
View File
@@ -23,77 +23,21 @@
****************************************************************************/
#include <nuttx/config.h>
#include <nuttx/tls.h>
#include <nuttx/pthread.h>
#include <pthread.h>
#include <sched.h>
#include <assert.h>
#include <nuttx/sched.h>
#include <nuttx/tls.h>
#include <nuttx/pthread.h>
#if defined(CONFIG_PTHREAD_CLEANUP_STACKSIZE) && CONFIG_PTHREAD_CLEANUP_STACKSIZE > 0
/****************************************************************************
* Private Functions
****************************************************************************/
/****************************************************************************
* Name: pthread_cleanup_pop_tls
*
* Description:
* The pthread_cleanup_pop_tcb() function will remove the routine at the
* top of the calling thread's cancellation cleanup stack and optionally
* invoke it (if 'execute' is non-zero).
*
* Input Parameters:
* tcb - The TCB of the pthread that is exiting or being canceled.
*
* Returned Value:
* None
*
****************************************************************************/
static void pthread_cleanup_pop_tls(FAR struct tls_info_s *tls, int execute)
{
if (tls->tl_tos > 0)
{
unsigned int ndx;
/* Get the index to the last cleaner function pushed onto the stack */
ndx = tls->tl_tos - 1;
DEBUGASSERT(ndx >= 0 && ndx < CONFIG_PTHREAD_CLEANUP_STACKSIZE);
/* Should we execute the cleanup routine at the top of the stack? */
if (execute != 0)
{
FAR struct pthread_cleanup_s *cb;
/* Yes.. Execute the clean-up routine. */
cb = &tls->tl_stack[ndx];
cb->pc_cleaner(cb->pc_arg);
}
tls->tl_tos = ndx;
}
}
/****************************************************************************
* Public Functions
****************************************************************************/
/****************************************************************************
* Name: pthread_cleanup_push
* pthread_cleanup_pop
*
* Description:
* The pthread_cleanup_pop() function will remove the routine at the top
* of the calling thread's cancellation cleanup stack and optionally
* invoke it (if 'execute' is non-zero).
*
* The pthread_cleanup_push() function will push the specified cancellation
* cleanup handler routine onto the calling thread's cancellation cleanup
* stack. The cancellation cleanup handler will be popped from the
@@ -106,6 +50,30 @@ static void pthread_cleanup_pop_tls(FAR struct tls_info_s *tls, int execute)
* Input Parameters:
* routine - The cleanup routine to be pushed on the cleanup stack.
* arg - An argument that will accompany the callback.
*
* Returned Value:
* None
*
****************************************************************************/
void pthread_cleanup_push(pthread_cleanup_t routine, FAR void *arg)
{
tls_cleanup_push(tls_get_info(), routine, arg);
}
/****************************************************************************
* Name: pthread_cleanup_pop
*
* Description:
* The pthread_cleanup_pop() function will remove the routine at the top
* of the calling thread's cancellation cleanup stack and optionally
* invoke it (if 'execute' is non-zero).
*
* - The thread exits (that is, calls pthread_exit()).
* - The thread acts upon a cancellation request.
* - The thread calls pthread_cleanup_pop() with non-zero execute argument.
*
* Input Parameters:
* execute - Execute the popped cleanup function immediately.
*
* Returned Value:
@@ -115,54 +83,5 @@ static void pthread_cleanup_pop_tls(FAR struct tls_info_s *tls, int execute)
void pthread_cleanup_pop(int execute)
{
FAR struct tls_info_s *tls = tls_get_info();
DEBUGASSERT(tls != NULL);
pthread_cleanup_pop_tls(tls, execute);
tls_cleanup_pop(tls_get_info(), execute);
}
void pthread_cleanup_push(pthread_cleanup_t routine, FAR void *arg)
{
FAR struct tls_info_s *tls = tls_get_info();
DEBUGASSERT(tls != NULL);
DEBUGASSERT(tls->tl_tos < CONFIG_PTHREAD_CLEANUP_STACKSIZE);
if (tls->tl_tos < CONFIG_PTHREAD_CLEANUP_STACKSIZE)
{
unsigned int ndx = tls->tl_tos;
tls->tl_tos++;
tls->tl_stack[ndx].pc_cleaner = routine;
tls->tl_stack[ndx].pc_arg = arg;
}
}
/****************************************************************************
* Name: pthread_cleanup_popall
*
* Description:
* The pthread_cleanup_popall() is an internal function that will pop and
* execute all clean-up functions. This function is only called from
* within the pthread_exit() and pthread_cancellation() logic
*
* Input Parameters:
* tls - The local storage info of the exiting thread
*
* Returned Value:
* None
*
****************************************************************************/
void pthread_cleanup_popall(FAR struct tls_info_s *tls)
{
DEBUGASSERT(tls != NULL);
while (tls->tl_tos > 0)
{
pthread_cleanup_pop_tls(tls, 1);
}
}
#endif /* defined(CONFIG_PTHREAD_CLEANUP_STACKSIZE) && CONFIG_PTHREAD_CLEANUP_STACKSIZE > 0 */
+1 -3
View File
@@ -60,9 +60,7 @@ void pthread_exit(FAR void *exit_value)
task_setcancelstate(TASK_CANCEL_DISABLE, NULL);
#if defined(CONFIG_PTHREAD_CLEANUP_STACKSIZE) && CONFIG_PTHREAD_CLEANUP_STACKSIZE > 0
pthread_cleanup_popall(tls_get_info());
#endif
tls_cleanup_popall(tls_get_info());
#if defined(CONFIG_TLS_NELEM) && CONFIG_TLS_NELEM > 0
tls_destruct();
@@ -33,14 +33,12 @@
* Private Functions
****************************************************************************/
#if defined(CONFIG_PTHREAD_CLEANUP_STACKSIZE) && CONFIG_PTHREAD_CLEANUP_STACKSIZE > 0
static void rdlock_cleanup(FAR void *arg)
{
FAR pthread_rwlock_t *rw_lock = (FAR pthread_rwlock_t *)arg;
pthread_mutex_unlock(&rw_lock->lock);
}
#endif
static int tryrdlock(FAR pthread_rwlock_t *rw_lock)
{
@@ -109,9 +107,7 @@ int pthread_rwlock_clockrdlock(FAR pthread_rwlock_t *rw_lock,
return err;
}
#if defined(CONFIG_PTHREAD_CLEANUP_STACKSIZE) && CONFIG_PTHREAD_CLEANUP_STACKSIZE > 0
pthread_cleanup_push(&rdlock_cleanup, rw_lock);
#endif
while ((err = tryrdlock(rw_lock)) == EBUSY)
{
if (ts != NULL)
@@ -130,9 +126,7 @@ int pthread_rwlock_clockrdlock(FAR pthread_rwlock_t *rw_lock,
}
}
#if defined(CONFIG_PTHREAD_CLEANUP_STACKSIZE) && CONFIG_PTHREAD_CLEANUP_STACKSIZE > 0
pthread_cleanup_pop(0);
#endif
pthread_mutex_unlock(&rw_lock->lock);
return err;
@@ -33,7 +33,6 @@
* Private Functions
****************************************************************************/
#if defined(CONFIG_PTHREAD_CLEANUP_STACKSIZE) && CONFIG_PTHREAD_CLEANUP_STACKSIZE > 0
static void wrlock_cleanup(FAR void *arg)
{
FAR pthread_rwlock_t *rw_lock = (FAR pthread_rwlock_t *)arg;
@@ -41,7 +40,6 @@ static void wrlock_cleanup(FAR void *arg)
rw_lock->num_writers--;
pthread_mutex_unlock(&rw_lock->lock);
}
#endif
/****************************************************************************
* Public Functions
@@ -104,9 +102,7 @@ int pthread_rwlock_clockwrlock(FAR pthread_rwlock_t *rw_lock,
rw_lock->num_writers++;
#if defined(CONFIG_PTHREAD_CLEANUP_STACKSIZE) && CONFIG_PTHREAD_CLEANUP_STACKSIZE > 0
pthread_cleanup_push(&wrlock_cleanup, rw_lock);
#endif
while (rw_lock->write_in_progress || rw_lock->num_readers > 0)
{
if (ts != NULL)
@@ -125,9 +121,7 @@ int pthread_rwlock_clockwrlock(FAR pthread_rwlock_t *rw_lock,
}
}
#if defined(CONFIG_PTHREAD_CLEANUP_STACKSIZE) && CONFIG_PTHREAD_CLEANUP_STACKSIZE > 0
pthread_cleanup_pop(0);
#endif
if (err == 0)
{
+2 -6
View File
@@ -99,9 +99,7 @@ void exit(int status)
task_setcancelstate(TASK_CANCEL_DISABLE, NULL);
#if defined(CONFIG_PTHREAD_CLEANUP_STACKSIZE) && CONFIG_PTHREAD_CLEANUP_STACKSIZE > 0
pthread_cleanup_popall(tls_get_info());
#endif
tls_cleanup_popall(tls_get_info());
#if defined(CONFIG_TLS_NELEM) && CONFIG_TLS_NELEM > 0
tls_destruct();
@@ -152,9 +150,7 @@ void quick_exit(int status)
task_setcancelstate(TASK_CANCEL_DISABLE, NULL);
#if defined(CONFIG_PTHREAD_CLEANUP_STACKSIZE) && CONFIG_PTHREAD_CLEANUP_STACKSIZE > 0
pthread_cleanup_popall(tls_get_info());
#endif
tls_cleanup_popall(tls_get_info());
#if defined(CONFIG_TLS_NELEM) && CONFIG_TLS_NELEM > 0
tls_destruct();
+4
View File
@@ -28,4 +28,8 @@ if(NOT CONFIG_TLS_NELEM EQUAL 0)
list(APPEND SRCS tls_destruct.c)
endif()
if(NOT CONFIG_TLS_NCLEANUP EQUAL 0)
list(APPEND SRCS tls_cleanup.c)
endif()
target_sources(c PRIVATE ${SRCS})
+10
View File
@@ -67,4 +67,14 @@ config TLS_TASK_NELEM
NOTE that the 0 value of CONFIG_SCHED_TLS_NELEM disables these
TLS interfaces.
config TLS_NCLEANUP
int "Number of cleanup stack levels"
default 0
range 0 255
---help---
The number of cleanup stack levels. These are used to support
POSIX pthread cancellation. The number of cleanup stack levels
is the maximum number of pthread_cleanup_push() calls that can
be made without a corresponding pthread_cleanup_pop() call.
endmenu # Thread Local Storage (TLS)
+4
View File
@@ -28,6 +28,10 @@ ifneq ($(CONFIG_TLS_NELEM),0)
CSRCS += tls_destruct.c
endif
ifneq ($(CONFIG_TLS_NCLEANUP),0)
CSRCS += tls_cleanup.c
endif
# Include tls build support
DEPPATH += --dep-path tls
+91
View File
@@ -0,0 +1,91 @@
/****************************************************************************
* libs/libc/tls/tls_cleanup.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/sched.h>
#include <nuttx/tls.h>
#include <nuttx/pthread.h>
/****************************************************************************
* Public Functions
****************************************************************************/
void tls_cleanup_push(FAR struct tls_info_s *tls,
tls_cleanup_t routine, FAR void *arg)
{
DEBUGASSERT(tls != NULL);
DEBUGASSERT(tls->tl_tos < CONFIG_TLS_NCLEANUP);
if (tls->tl_tos < CONFIG_TLS_NCLEANUP)
{
unsigned int ndx = tls->tl_tos;
tls->tl_tos++;
tls->tl_stack[ndx].tc_cleaner = routine;
tls->tl_stack[ndx].tc_arg = arg;
}
}
void tls_cleanup_pop(FAR struct tls_info_s *tls, int execute)
{
DEBUGASSERT(tls != NULL);
if (tls->tl_tos > 0)
{
unsigned int ndx;
/* Get the index to the last cleaner function pushed onto the stack */
ndx = tls->tl_tos - 1;
DEBUGASSERT(ndx >= 0 && ndx < CONFIG_TLS_NCLEANUP);
/* Should we execute the cleanup routine at the top of the stack? */
if (execute != 0)
{
FAR struct tls_cleanup_s *cb;
/* Yes.. Execute the clean-up routine. */
cb = &tls->tl_stack[ndx];
cb->tc_cleaner(cb->tc_arg);
}
tls->tl_tos = ndx;
}
}
void tls_cleanup_popall(FAR struct tls_info_s *tls)
{
DEBUGASSERT(tls != NULL);
while (tls->tl_tos > 0)
{
tls_cleanup_pop(tls, 1);
}
}