2012-08-01 19:47:54 +02:00
|
|
|
/****************************************************************************
|
2014-08-09 00:57:47 +02:00
|
|
|
* sched/task/task_onexit.c
|
2012-02-01 00:39:12 +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
|
2012-02-01 00:39:12 +01:00
|
|
|
*
|
2021-02-08 16:33:58 +01:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2012-02-01 00:39:12 +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.
|
2012-02-01 00:39:12 +01:00
|
|
|
*
|
2012-08-01 19:47:54 +02:00
|
|
|
****************************************************************************/
|
2012-02-01 00:39:12 +01:00
|
|
|
|
2012-08-01 19:47:54 +02:00
|
|
|
/****************************************************************************
|
2012-02-01 00:39:12 +01:00
|
|
|
* Included Files
|
2012-08-01 19:47:54 +02:00
|
|
|
****************************************************************************/
|
2012-02-01 00:39:12 +01:00
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <assert.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <debug.h>
|
|
|
|
#include <errno.h>
|
|
|
|
|
2012-03-21 19:01:07 +01:00
|
|
|
#include <nuttx/fs/fs.h>
|
2012-02-01 00:39:12 +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"
|
2012-02-01 00:39:12 +01:00
|
|
|
|
|
|
|
#ifdef CONFIG_SCHED_ONEXIT
|
|
|
|
|
2012-08-01 19:47:54 +02:00
|
|
|
/****************************************************************************
|
2012-02-01 00:39:12 +01:00
|
|
|
* Public Functions
|
2012-08-01 19:47:54 +02:00
|
|
|
****************************************************************************/
|
2012-02-01 00:39:12 +01:00
|
|
|
|
2012-08-01 19:47:54 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: on_exit
|
2012-02-01 00:39:12 +01:00
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Registers a function to be called at program exit.
|
|
|
|
* The on_exit() function registers the given function to be called
|
|
|
|
* at normal process termination, whether via exit or via return from
|
|
|
|
* the program's main(). The function is passed the status argument
|
|
|
|
* given to the last call to exit and the arg argument from on_exit().
|
|
|
|
*
|
2012-08-01 19:47:54 +02:00
|
|
|
* NOTE 1: This function comes from SunOS 4, but is also present in
|
|
|
|
* libc4, libc5 and glibc. It no longer occurs in Solaris (SunOS 5).
|
2014-04-13 22:32:20 +02:00
|
|
|
* Avoid this function, and use the standard atexit() instead.
|
2012-08-01 19:47:54 +02:00
|
|
|
*
|
|
|
|
* NOTE 2: CONFIG_SCHED_ONEXIT must be defined to enable this function
|
|
|
|
*
|
2020-02-23 09:50:23 +01:00
|
|
|
* Limitations in the current implementation:
|
2012-02-01 00:39:12 +01:00
|
|
|
*
|
2012-08-01 19:47:54 +02:00
|
|
|
* 1. Only a single on_exit function can be registered unless
|
|
|
|
* CONFIG_SCHED_ONEXIT_MAX defines a larger number.
|
2012-02-01 00:39:12 +01:00
|
|
|
* 2. on_exit functions are not inherited when a new task is
|
|
|
|
* created.
|
|
|
|
*
|
2018-03-13 16:52:27 +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.
|
|
|
|
* arg - An argument that will be provided to the on_exit() function when
|
|
|
|
* the task exits.
|
2012-02-01 00:39:12 +01:00
|
|
|
*
|
2018-02-01 17:00:02 +01:00
|
|
|
* Returned Value:
|
2012-02-01 00:39:12 +01:00
|
|
|
* Zero on success. Non-zero on failure.
|
|
|
|
*
|
2012-08-01 19:47:54 +02:00
|
|
|
****************************************************************************/
|
2012-02-01 00:39:12 +01:00
|
|
|
|
|
|
|
int on_exit(CODE void (*func)(int, FAR void *), FAR void *arg)
|
|
|
|
{
|
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;
|
2020-06-03 11:08:25 +02:00
|
|
|
int index;
|
|
|
|
int ret = ENOSPC;
|
2012-08-01 19:47:54 +02:00
|
|
|
|
2013-02-04 16:29:19 +01:00
|
|
|
DEBUGASSERT(group);
|
|
|
|
|
2012-08-01 19:47:54 +02:00
|
|
|
/* The following must be atomic */
|
|
|
|
|
|
|
|
if (func)
|
|
|
|
{
|
|
|
|
sched_lock();
|
|
|
|
|
2020-03-08 13:51:34 +01:00
|
|
|
/* Search for the first available slot. on_exit() 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.on)
|
2012-08-01 19:47:54 +02:00
|
|
|
{
|
2020-06-03 11:08:25 +02:00
|
|
|
group->tg_exit[index].func.on = func;
|
|
|
|
group->tg_exit[index].arg = arg;
|
2012-08-01 19:47:54 +02:00
|
|
|
ret = OK;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
sched_unlock();
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
2012-02-01 00:39:12 +01:00
|
|
|
}
|
|
|
|
|
2012-08-01 19:47:54 +02:00
|
|
|
#endif /* CONFIG_SCHED_ONEXIT */
|