2016-12-08 16:27:13 +01:00
|
|
|
/****************************************************************************
|
2021-04-30 12:12:09 +02:00
|
|
|
* libs/libc/pthread/pthread_cleanup.c
|
2016-12-08 16:27:13 +01:00
|
|
|
*
|
2020-06-17 16:45:06 +02:00
|
|
|
* 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.
|
2016-12-08 16:27:13 +01:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
|
|
|
|
|
|
|
#include <pthread.h>
|
2016-12-08 21:33:02 +01:00
|
|
|
#include <sched.h>
|
2021-05-18 08:59:14 +02:00
|
|
|
#include <assert.h>
|
2016-12-08 16:27:13 +01:00
|
|
|
|
2021-04-30 12:12:09 +02:00
|
|
|
#include <nuttx/arch.h>
|
2016-12-08 16:27:13 +01:00
|
|
|
#include <nuttx/sched.h>
|
2021-04-30 12:12:09 +02:00
|
|
|
#include <nuttx/tls.h>
|
|
|
|
#include <nuttx/pthread.h>
|
|
|
|
#include <arch/tls.h>
|
2016-12-08 16:27:13 +01:00
|
|
|
|
|
|
|
#ifdef CONFIG_PTHREAD_CLEANUP
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
2021-04-30 12:12:09 +02:00
|
|
|
* Name: pthread_cleanup_pop_tls
|
2016-12-08 16:27:13 +01:00
|
|
|
*
|
|
|
|
* Description:
|
2020-06-03 20:11:04 +02:00
|
|
|
* The pthread_cleanup_pop_tcb() function will remove the routine at the
|
|
|
|
* top of the calling thread's cancellation cleanup stack and optionally
|
2016-12-08 16:27:13 +01:00
|
|
|
* invoke it (if 'execute' is non-zero).
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* tcb - The TCB of the pthread that is exiting or being canceled.
|
|
|
|
*
|
2018-02-01 17:00:02 +01:00
|
|
|
* Returned Value:
|
2016-12-08 16:27:13 +01:00
|
|
|
* None
|
|
|
|
*
|
2016-12-09 14:23:00 +01:00
|
|
|
* Assumptions:
|
|
|
|
* The scheduler is locked.
|
|
|
|
*
|
2016-12-08 16:27:13 +01:00
|
|
|
****************************************************************************/
|
|
|
|
|
2021-04-30 12:12:09 +02:00
|
|
|
static void pthread_cleanup_pop_tls(FAR struct tls_info_s *tls, int execute)
|
2016-12-08 16:27:13 +01:00
|
|
|
{
|
2021-04-30 12:12:09 +02:00
|
|
|
if (tls->tos > 0)
|
2016-12-08 16:27:13 +01:00
|
|
|
{
|
|
|
|
unsigned int ndx;
|
|
|
|
|
|
|
|
/* Get the index to the last cleaner function pushed onto the stack */
|
|
|
|
|
2021-04-30 12:12:09 +02:00
|
|
|
ndx = tls->tos - 1;
|
2016-12-08 21:33:02 +01:00
|
|
|
DEBUGASSERT(ndx >= 0 && ndx < CONFIG_PTHREAD_CLEANUP_STACKSIZE);
|
2016-12-08 16:27:13 +01:00
|
|
|
|
|
|
|
/* 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.
|
|
|
|
*
|
|
|
|
* REVISIT: This is a security problem In the PROTECTED and KERNEL
|
|
|
|
* builds: We must not call the registered function in supervisor
|
|
|
|
* mode! See also on_exit() and atexit() callbacks.
|
|
|
|
*/
|
|
|
|
|
2021-04-30 12:12:09 +02:00
|
|
|
cb = &tls->stack[ndx];
|
2016-12-08 16:27:13 +01:00
|
|
|
cb->pc_cleaner(cb->pc_arg);
|
|
|
|
}
|
|
|
|
|
2021-04-30 12:12:09 +02:00
|
|
|
tls->tos = ndx;
|
2016-12-08 16:27:13 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* 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
|
|
|
|
* cancellation cleanup stack and invoked with the argument arg when:
|
|
|
|
*
|
|
|
|
* - The thread exits (that is, calls pthread_exit()).
|
|
|
|
* - The thread acts upon a cancellation request.
|
2020-06-03 20:11:04 +02:00
|
|
|
* - The thread calls pthread_cleanup_pop() with non-zero execute argument.
|
2016-12-08 16:27:13 +01:00
|
|
|
*
|
|
|
|
* Input Parameters:
|
2017-05-11 21:35:56 +02:00
|
|
|
* routine - The cleanup routine to be pushed on the cleanup stack.
|
2016-12-08 16:27:13 +01:00
|
|
|
* arg - An argument that will accompany the callback.
|
|
|
|
* execute - Execute the popped cleanup function immediately.
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* None
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
void pthread_cleanup_pop(int execute)
|
|
|
|
{
|
2021-04-30 12:12:09 +02:00
|
|
|
FAR struct tls_info_s *tls = up_tls_info();
|
2016-12-08 16:27:13 +01:00
|
|
|
|
2021-04-30 12:12:09 +02:00
|
|
|
DEBUGASSERT(tls != NULL);
|
2016-12-08 16:27:13 +01:00
|
|
|
|
2016-12-08 21:33:02 +01:00
|
|
|
/* sched_lock() should provide sufficient protection. We only need to
|
|
|
|
* have this TCB stationary; the pthread cleanup stack should never be
|
|
|
|
* modified by interrupt level logic.
|
|
|
|
*/
|
|
|
|
|
|
|
|
sched_lock();
|
2021-04-30 12:12:09 +02:00
|
|
|
pthread_cleanup_pop_tls(tls, execute);
|
2016-12-08 21:33:02 +01:00
|
|
|
sched_unlock();
|
2016-12-08 16:27:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void pthread_cleanup_push(pthread_cleanup_t routine, FAR void *arg)
|
|
|
|
{
|
2021-04-30 12:12:09 +02:00
|
|
|
FAR struct tls_info_s *tls = up_tls_info();
|
2016-12-08 16:27:13 +01:00
|
|
|
|
2021-04-30 12:12:09 +02:00
|
|
|
DEBUGASSERT(tls != NULL);
|
|
|
|
DEBUGASSERT(tls->tos < CONFIG_PTHREAD_CLEANUP_STACKSIZE);
|
2016-12-08 16:27:13 +01:00
|
|
|
|
2016-12-08 21:33:02 +01:00
|
|
|
/* sched_lock() should provide sufficient protection. We only need to
|
|
|
|
* have this TCB stationary; the pthread cleanup stack should never be
|
|
|
|
* modified by interrupt level logic.
|
|
|
|
*/
|
|
|
|
|
|
|
|
sched_lock();
|
2021-04-30 12:12:09 +02:00
|
|
|
if (tls->tos < CONFIG_PTHREAD_CLEANUP_STACKSIZE)
|
2016-12-08 16:27:13 +01:00
|
|
|
{
|
2021-04-30 12:12:09 +02:00
|
|
|
unsigned int ndx = tls->tos;
|
2016-12-08 16:27:13 +01:00
|
|
|
|
2021-04-30 12:12:09 +02:00
|
|
|
tls->tos++;
|
|
|
|
tls->stack[ndx].pc_cleaner = routine;
|
|
|
|
tls->stack[ndx].pc_arg = arg;
|
2016-12-08 16:27:13 +01:00
|
|
|
}
|
|
|
|
|
2016-12-08 21:33:02 +01:00
|
|
|
sched_unlock();
|
2016-12-08 16:27:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: pthread_cleanup_popall
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* The pthread_cleanup_popall() is an internal function that will pop and
|
2020-06-03 20:11:04 +02:00
|
|
|
* execute all clean-up functions. This function is only called from
|
|
|
|
* within the pthread_exit() and pthread_cancellation() logic
|
2016-12-08 16:27:13 +01:00
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* None
|
|
|
|
*
|
2021-04-29 11:56:28 +02:00
|
|
|
* Returned Value:
|
2021-04-30 12:12:09 +02:00
|
|
|
* None
|
2021-04-29 11:56:28 +02:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2021-04-30 12:12:09 +02:00
|
|
|
void pthread_cleanup_popall(void)
|
2021-04-29 11:56:28 +02:00
|
|
|
{
|
2021-04-30 12:12:09 +02:00
|
|
|
FAR struct tls_info_s *tls = up_tls_info();
|
2021-04-29 11:56:28 +02:00
|
|
|
|
2021-04-30 12:12:09 +02:00
|
|
|
DEBUGASSERT(tls != NULL);
|
2021-04-29 11:56:28 +02:00
|
|
|
|
2021-04-30 12:12:09 +02:00
|
|
|
sched_lock();
|
|
|
|
while (tls->tos > 0)
|
2021-04-29 11:56:28 +02:00
|
|
|
{
|
2021-04-30 12:12:09 +02:00
|
|
|
pthread_cleanup_pop_tls(tls, 1);
|
2021-04-29 11:56:28 +02:00
|
|
|
}
|
|
|
|
|
2021-04-30 12:12:09 +02:00
|
|
|
sched_unlock();
|
2021-04-29 11:56:28 +02:00
|
|
|
}
|
|
|
|
|
2016-12-08 16:27:13 +01:00
|
|
|
#endif /* CONFIG_PTHREAD_CLEANUP */
|