2012-08-02 01:47:54 +08:00
|
|
|
/****************************************************************************
|
2014-08-09 06:57:47 +08:00
|
|
|
* sched/task/task_atexit.c
|
2007-02-18 07:21:28 +08:00
|
|
|
*
|
2021-02-08 23:33:58 +08: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
|
2007-02-18 07:21:28 +08:00
|
|
|
*
|
2021-02-08 23:33:58 +08:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2007-02-18 07:21:28 +08:00
|
|
|
*
|
2021-02-08 23:33:58 +08:00
|
|
|
* 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.
|
2007-02-18 07:21:28 +08:00
|
|
|
*
|
2012-08-02 01:47:54 +08:00
|
|
|
****************************************************************************/
|
2007-02-18 07:21:28 +08:00
|
|
|
|
2012-08-02 01:47:54 +08:00
|
|
|
/****************************************************************************
|
2007-02-18 07:21:28 +08:00
|
|
|
* Included Files
|
2012-08-02 01:47:54 +08:00
|
|
|
****************************************************************************/
|
2007-02-18 07:21:28 +08:00
|
|
|
|
2011-03-12 11:42:08 +08:00
|
|
|
#include <nuttx/config.h>
|
|
|
|
|
2007-02-18 07:21:28 +08:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <assert.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <debug.h>
|
|
|
|
#include <errno.h>
|
2011-03-12 11:42:08 +08:00
|
|
|
|
2012-03-22 02:01:07 +08:00
|
|
|
#include <nuttx/fs/fs.h>
|
2011-03-12 11:42:08 +08:00
|
|
|
|
2014-08-09 07:53:55 +08:00
|
|
|
#include "sched/sched.h"
|
2014-08-09 06:57:47 +08:00
|
|
|
#include "task/task.h"
|
2007-02-18 07:21:28 +08:00
|
|
|
|
2011-05-08 00:59:20 +08:00
|
|
|
#ifdef CONFIG_SCHED_ATEXIT
|
2011-03-12 11:42:08 +08:00
|
|
|
|
2020-06-03 18:12:10 +08:00
|
|
|
/****************************************************************************
|
|
|
|
* Private Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#ifdef CONFIG_SCHED_ONEXIT
|
|
|
|
static void exitfunc(int exitcode, FAR void *arg)
|
|
|
|
{
|
|
|
|
(*(atexitfunc_t)arg)();
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2012-08-02 01:47:54 +08:00
|
|
|
/****************************************************************************
|
2007-02-18 07:21:28 +08:00
|
|
|
* Public Functions
|
2012-08-02 01:47:54 +08:00
|
|
|
****************************************************************************/
|
2007-02-18 07:21:28 +08:00
|
|
|
|
2012-08-02 01:47:54 +08:00
|
|
|
/****************************************************************************
|
2012-07-15 03:30:31 +08:00
|
|
|
* Name: atexit
|
2007-02-18 07:21:28 +08:00
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Registers a function to be called at program exit.
|
2012-08-02 01:47:54 +08:00
|
|
|
* The atexit() function registers the given function to be called
|
|
|
|
* at normal process termination, whether via exit or via return from
|
|
|
|
* the program's main().
|
|
|
|
*
|
|
|
|
* NOTE: CONFIG_SCHED_ATEXIT must be defined to enable this function
|
|
|
|
*
|
2015-04-14 21:31:35 +08:00
|
|
|
* Limitations in the current implementation:
|
2012-08-02 01:47:54 +08:00
|
|
|
*
|
|
|
|
* 1. Only a single atexit function can be registered unless
|
|
|
|
* CONFIG_SCHED_ATEXIT_MAX defines a larger number.
|
|
|
|
* 2. atexit functions are not inherited when a new task is
|
|
|
|
* created.
|
2012-11-02 00:50:53 +08:00
|
|
|
* 3. If both SCHED_ONEXIT and SCHED_ATEXIT are selected, then atexit()
|
|
|
|
* is built on top of the on_exit() implementation. In that case,
|
|
|
|
* CONFIG_SCHED_ONEXIT_MAX determines the size of the combined
|
2015-04-14 21:31:35 +08:00
|
|
|
* number of atexit() and on_exit() calls and SCHED_ATEXIT_MAX is
|
2012-11-02 00:50:53 +08:00
|
|
|
* not used.
|
2007-02-18 07:21:28 +08:00
|
|
|
*
|
2012-11-02 00:50:53 +08:00
|
|
|
* Input Parameters:
|
2012-08-02 01:47:54 +08:00
|
|
|
* func - A pointer to the function to be called when the task exits.
|
2007-02-18 07:21:28 +08:00
|
|
|
*
|
2018-02-02 00:00:02 +08:00
|
|
|
* Returned Value:
|
2011-03-12 11:42:08 +08:00
|
|
|
* Zero on success. Non-zero on failure.
|
2007-02-18 07:21:28 +08:00
|
|
|
*
|
2012-08-02 01:47:54 +08:00
|
|
|
****************************************************************************/
|
2007-02-18 07:21:28 +08:00
|
|
|
|
|
|
|
int atexit(void (*func)(void))
|
|
|
|
{
|
2012-11-02 00:50:53 +08:00
|
|
|
#if defined(CONFIG_SCHED_ONEXIT)
|
2020-03-08 20:51:32 +08:00
|
|
|
/* atexit is equivalent to on_exit() with no argument (Assuming that the
|
|
|
|
* ABI can handle a callback function that receives more parameters than
|
|
|
|
* it expects).
|
2012-11-02 00:50:53 +08:00
|
|
|
*/
|
|
|
|
|
2020-06-03 18:12:10 +08:00
|
|
|
return on_exit(exitfunc, func);
|
2012-11-02 00:50:53 +08:00
|
|
|
|
2020-06-03 17:08:25 +08:00
|
|
|
#else
|
2016-02-07 07:44:41 +08:00
|
|
|
FAR struct tcb_s *tcb = this_task();
|
2013-02-04 23:29:19 +08:00
|
|
|
FAR struct task_group_s *group = tcb->group;
|
|
|
|
int index;
|
|
|
|
int ret = ERROR;
|
|
|
|
|
|
|
|
DEBUGASSERT(group);
|
2012-08-02 01:47:54 +08:00
|
|
|
|
|
|
|
/* The following must be atomic */
|
|
|
|
|
|
|
|
if (func)
|
|
|
|
{
|
|
|
|
sched_lock();
|
|
|
|
|
2020-03-08 20:51:32 +08:00
|
|
|
/* Search for the first available slot. atexit() functions are
|
|
|
|
* registered from lower to higher array indices; they must be called
|
|
|
|
* in the reverse order of registration when task exists, i.e., from
|
|
|
|
* higher to lower indices.
|
2012-08-02 01:47:54 +08:00
|
|
|
*/
|
|
|
|
|
2020-06-03 17:08:25 +08:00
|
|
|
for (index = 0; index < CONFIG_SCHED_EXIT_MAX; index++)
|
2012-08-02 01:47:54 +08:00
|
|
|
{
|
2020-06-03 17:08:25 +08:00
|
|
|
if (!group->tg_exit[index].func.at)
|
2012-08-02 01:47:54 +08:00
|
|
|
{
|
2020-06-03 17:08:25 +08:00
|
|
|
group->tg_exit[index].func.at = func;
|
2012-08-02 01:47:54 +08:00
|
|
|
ret = OK;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2014-04-14 04:32:20 +08:00
|
|
|
|
2012-08-02 01:47:54 +08:00
|
|
|
sched_unlock();
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
#endif
|
2007-02-18 07:21:28 +08:00
|
|
|
}
|
|
|
|
|
2011-04-04 20:43:35 +08:00
|
|
|
#endif /* CONFIG_SCHED_ATEXIT */
|