2012-08-01 19:47:54 +02:00
|
|
|
/****************************************************************************
|
2014-08-09 00:57:47 +02:00
|
|
|
* sched/task/task_atexit.c
|
2007-02-18 00:21:28 +01:00
|
|
|
*
|
2021-02-08 16:33:58 +01: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 00:21:28 +01:00
|
|
|
*
|
2021-02-08 16:33:58 +01:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2007-02-18 00:21:28 +01:00
|
|
|
*
|
2021-02-08 16:33:58 +01: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 00:21:28 +01:00
|
|
|
*
|
2012-08-01 19:47:54 +02:00
|
|
|
****************************************************************************/
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2012-08-01 19:47:54 +02:00
|
|
|
/****************************************************************************
|
2007-02-18 00:21:28 +01:00
|
|
|
* Included Files
|
2012-08-01 19:47:54 +02:00
|
|
|
****************************************************************************/
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2011-03-12 04:42:08 +01:00
|
|
|
#include <nuttx/config.h>
|
|
|
|
|
2007-02-18 00:21:28 +01:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <assert.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <debug.h>
|
|
|
|
#include <errno.h>
|
2011-03-12 04:42:08 +01:00
|
|
|
|
2012-03-21 19:01:07 +01:00
|
|
|
#include <nuttx/fs/fs.h>
|
2011-03-12 04:42:08 +01:00
|
|
|
|
2014-08-09 01:53:55 +02:00
|
|
|
#include "sched/sched.h"
|
2014-08-09 00:57:47 +02:00
|
|
|
#include "task/task.h"
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2011-05-07 18:59:20 +02:00
|
|
|
#ifdef CONFIG_SCHED_ATEXIT
|
2011-03-12 04:42:08 +01:00
|
|
|
|
2020-06-03 12:12:10 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Private Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#ifdef CONFIG_SCHED_ONEXIT
|
|
|
|
static void exitfunc(int exitcode, FAR void *arg)
|
|
|
|
{
|
|
|
|
(*(atexitfunc_t)arg)();
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2012-08-01 19:47:54 +02:00
|
|
|
/****************************************************************************
|
2007-02-18 00:21:28 +01:00
|
|
|
* Public Functions
|
2012-08-01 19:47:54 +02:00
|
|
|
****************************************************************************/
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2012-08-01 19:47:54 +02:00
|
|
|
/****************************************************************************
|
2012-07-14 21:30:31 +02:00
|
|
|
* Name: atexit
|
2007-02-18 00:21:28 +01:00
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Registers a function to be called at program exit.
|
2012-08-01 19:47:54 +02: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 15:31:35 +02:00
|
|
|
* Limitations in the current implementation:
|
2012-08-01 19:47:54 +02: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-01 17:50:53 +01: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 15:31:35 +02:00
|
|
|
* number of atexit() and on_exit() calls and SCHED_ATEXIT_MAX is
|
2012-11-01 17:50:53 +01:00
|
|
|
* not used.
|
2007-02-18 00:21:28 +01:00
|
|
|
*
|
2012-11-01 17:50:53 +01:00
|
|
|
* Input Parameters:
|
2012-08-01 19:47:54 +02:00
|
|
|
* func - A pointer to the function to be called when the task exits.
|
2007-02-18 00:21:28 +01:00
|
|
|
*
|
2018-02-01 17:00:02 +01:00
|
|
|
* Returned Value:
|
2011-03-12 04:42:08 +01:00
|
|
|
* Zero on success. Non-zero on failure.
|
2007-02-18 00:21:28 +01:00
|
|
|
*
|
2012-08-01 19:47:54 +02:00
|
|
|
****************************************************************************/
|
2007-02-18 00:21:28 +01:00
|
|
|
|
|
|
|
int atexit(void (*func)(void))
|
|
|
|
{
|
2012-11-01 17:50:53 +01:00
|
|
|
#if defined(CONFIG_SCHED_ONEXIT)
|
2020-03-08 13:51:32 +01: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-01 17:50:53 +01:00
|
|
|
*/
|
|
|
|
|
2020-06-03 12:12:10 +02:00
|
|
|
return on_exit(exitfunc, func);
|
2012-11-01 17:50:53 +01:00
|
|
|
|
2020-06-03 11:08:25 +02:00
|
|
|
#else
|
2016-02-07 00:44:41 +01:00
|
|
|
FAR struct tcb_s *tcb = this_task();
|
2013-02-04 16:29:19 +01:00
|
|
|
FAR struct task_group_s *group = tcb->group;
|
|
|
|
int index;
|
|
|
|
int ret = ERROR;
|
|
|
|
|
|
|
|
DEBUGASSERT(group);
|
2012-08-01 19:47:54 +02:00
|
|
|
|
|
|
|
/* The following must be atomic */
|
|
|
|
|
|
|
|
if (func)
|
|
|
|
{
|
|
|
|
sched_lock();
|
|
|
|
|
2020-03-08 13:51:32 +01: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-01 19:47:54 +02:00
|
|
|
*/
|
|
|
|
|
2020-06-03 11:08:25 +02:00
|
|
|
for (index = 0; index < CONFIG_SCHED_EXIT_MAX; index++)
|
2012-08-01 19:47:54 +02:00
|
|
|
{
|
2020-06-03 11:08:25 +02:00
|
|
|
if (!group->tg_exit[index].func.at)
|
2012-08-01 19:47:54 +02:00
|
|
|
{
|
2020-06-03 11:08:25 +02:00
|
|
|
group->tg_exit[index].func.at = func;
|
2012-08-01 19:47:54 +02:00
|
|
|
ret = OK;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2014-04-13 22:32:20 +02:00
|
|
|
|
2012-08-01 19:47:54 +02:00
|
|
|
sched_unlock();
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
#endif
|
2007-02-18 00:21:28 +01:00
|
|
|
}
|
|
|
|
|
2011-04-04 14:43:35 +02:00
|
|
|
#endif /* CONFIG_SCHED_ATEXIT */
|