2009-12-14 19:39:29 +01:00
|
|
|
/************************************************************************
|
|
|
|
* sched/sched_releasetcb.c
|
2007-02-18 00:21:28 +01:00
|
|
|
*
|
2013-01-25 18:23:38 +01:00
|
|
|
* Copyright (C) 2007, 2009, 2012-2013 Gregory Nutt. All rights reserved.
|
2012-01-31 21:32:49 +01:00
|
|
|
* Author: Gregory Nutt <gnutt@nuttx.org>
|
2007-02-18 00:21:28 +01:00
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
*
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in
|
|
|
|
* the documentation and/or other materials provided with the
|
|
|
|
* distribution.
|
2009-12-14 19:39:29 +01:00
|
|
|
* 3. Neither the name NuttX nor the names of its contributors may be
|
2007-02-18 00:21:28 +01:00
|
|
|
* used to endorse or promote products derived from this software
|
|
|
|
* without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
|
|
|
|
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
|
|
|
|
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|
|
|
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
|
|
|
|
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
|
|
|
|
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
|
|
|
|
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
|
|
|
|
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
* POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*
|
2009-12-14 19:39:29 +01:00
|
|
|
************************************************************************/
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2009-12-14 19:39:29 +01:00
|
|
|
/************************************************************************
|
2007-02-18 00:21:28 +01:00
|
|
|
* Included Files
|
2009-12-14 19:39:29 +01:00
|
|
|
************************************************************************/
|
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2009-12-14 19:39:29 +01:00
|
|
|
#include <sys/types.h>
|
2007-02-18 00:21:28 +01:00
|
|
|
#include <sched.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <nuttx/arch.h>
|
2009-12-14 19:39:29 +01:00
|
|
|
|
2007-02-18 00:21:28 +01:00
|
|
|
#include "os_internal.h"
|
2013-01-26 00:21:27 +01:00
|
|
|
#include "group_internal.h"
|
2007-03-21 18:21:26 +01:00
|
|
|
#include "timer_internal.h"
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2009-12-14 19:39:29 +01:00
|
|
|
/************************************************************************
|
2007-02-18 00:21:28 +01:00
|
|
|
* Private Functions
|
2009-12-14 19:39:29 +01:00
|
|
|
************************************************************************/
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2009-12-14 19:39:29 +01:00
|
|
|
/************************************************************************
|
2007-02-18 00:21:28 +01:00
|
|
|
* Name: sched_releasepid
|
|
|
|
*
|
|
|
|
* Description: When a task is destroyed, this function must
|
|
|
|
* be called to make its process ID available for re-use.
|
2009-12-14 19:39:29 +01:00
|
|
|
************************************************************************/
|
2007-02-18 00:21:28 +01:00
|
|
|
|
|
|
|
static void sched_releasepid(pid_t pid)
|
|
|
|
{
|
2012-07-14 21:30:31 +02:00
|
|
|
int hash_ndx = PIDHASH(pid);
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2012-07-14 21:30:31 +02:00
|
|
|
/* Make any pid associated with this hash available. Note:
|
|
|
|
* no special precautions need be taken here because the
|
|
|
|
* following action is atomic
|
|
|
|
*/
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2012-07-14 21:30:31 +02:00
|
|
|
g_pidhash[hash_ndx].tcb = NULL;
|
|
|
|
g_pidhash[hash_ndx].pid = INVALID_PROCESS_ID;
|
2007-02-18 00:21:28 +01:00
|
|
|
}
|
|
|
|
|
2009-12-14 19:39:29 +01:00
|
|
|
/************************************************************************
|
2007-02-18 00:21:28 +01:00
|
|
|
* Public Functions
|
2009-12-14 19:39:29 +01:00
|
|
|
************************************************************************/
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2009-12-14 19:39:29 +01:00
|
|
|
/************************************************************************
|
2012-07-14 21:30:31 +02:00
|
|
|
* Name: sched_releasetcb
|
2007-02-18 00:21:28 +01:00
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Free all resources contained in a TCB
|
|
|
|
*
|
|
|
|
* Parameters:
|
2013-03-20 19:22:21 +01:00
|
|
|
* tcb - The TCB to be released
|
|
|
|
* ttype - The type of the TCB to be released
|
|
|
|
*
|
|
|
|
* This thread type is normally available in the flags field of the TCB,
|
|
|
|
* however, there are certain error recovery contexts where the TCB my
|
|
|
|
* not be fully initialized when sched_releasetcb is called.
|
2007-02-18 00:21:28 +01:00
|
|
|
*
|
|
|
|
* Return Value:
|
|
|
|
* OK on success; ERROR on failure
|
|
|
|
*
|
|
|
|
* Assumptions:
|
2007-03-21 18:21:26 +01:00
|
|
|
* Interrupts are disabled.
|
2007-02-18 00:21:28 +01:00
|
|
|
*
|
2009-12-14 19:39:29 +01:00
|
|
|
************************************************************************/
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2013-03-20 19:22:21 +01:00
|
|
|
int sched_releasetcb(FAR struct tcb_s *tcb, uint8_t ttype)
|
2007-02-18 00:21:28 +01:00
|
|
|
{
|
|
|
|
int ret = OK;
|
2013-03-22 00:37:11 +01:00
|
|
|
#if defined(CONFIG_CUSTOM_STACK) || !defined(CONFIG_NUTTX_KERNEL)
|
2007-02-18 00:21:28 +01:00
|
|
|
int i;
|
2013-03-22 00:37:11 +01:00
|
|
|
#endif
|
2007-02-18 00:21:28 +01:00
|
|
|
|
|
|
|
if (tcb)
|
|
|
|
{
|
2007-03-21 18:21:26 +01:00
|
|
|
/* Relase any timers that the task might hold. We do this
|
|
|
|
* before release the PID because it may still be trying to
|
|
|
|
* deliver signals (although interrupts are should be
|
|
|
|
* disabled here).
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef CONFIG_DISABLE_POSIX_TIMERS
|
|
|
|
#ifdef CONFIG_HAVE_WEAKFUNCTIONS
|
|
|
|
if (timer_deleteall != NULL)
|
|
|
|
#endif
|
|
|
|
{
|
|
|
|
timer_deleteall(tcb->pid);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2007-02-18 00:21:28 +01:00
|
|
|
/* Release the task's process ID if one was assigned. PID
|
|
|
|
* zero is reserved for the IDLE task. The TCB of the IDLE
|
|
|
|
* task is never release so a value of zero simply means that
|
|
|
|
* the process ID was never allocated to this TCB.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (tcb->pid)
|
|
|
|
{
|
|
|
|
sched_releasepid(tcb->pid);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Delete the thread's stack if one has been allocated */
|
|
|
|
|
2007-03-02 22:27:47 +01:00
|
|
|
#ifndef CONFIG_CUSTOM_STACK
|
2007-02-18 00:21:28 +01:00
|
|
|
if (tcb->stack_alloc_ptr)
|
|
|
|
{
|
2013-03-20 19:22:21 +01:00
|
|
|
up_release_stack(tcb, ttype);
|
2007-02-18 00:21:28 +01:00
|
|
|
}
|
2007-03-02 22:27:47 +01:00
|
|
|
#endif
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2009-06-17 00:08:59 +02:00
|
|
|
/* Delete the tasks's allocated DSpace region (external modules only) */
|
|
|
|
|
2009-06-17 23:15:31 +02:00
|
|
|
#ifdef CONFIG_PIC
|
2009-06-18 01:38:05 +02:00
|
|
|
if (tcb->dspace)
|
2009-06-17 00:08:59 +02:00
|
|
|
{
|
2009-06-18 01:38:05 +02:00
|
|
|
if (tcb->dspace->crefs <= 1)
|
|
|
|
{
|
2013-03-10 16:32:33 +01:00
|
|
|
sched_kfree(tcb->dspace);
|
2009-06-18 01:38:05 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
tcb->dspace->crefs--;
|
|
|
|
}
|
2009-06-17 00:08:59 +02:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2013-03-22 00:37:11 +01:00
|
|
|
#if defined(CONFIG_CUSTOM_STACK) || !defined(CONFIG_NUTTX_KERNEL)
|
2011-04-07 03:54:59 +02:00
|
|
|
/* Release command line arguments that were allocated for task
|
|
|
|
* start/re-start.
|
2013-03-21 22:51:15 +01:00
|
|
|
*
|
|
|
|
* NOTE: In the kernel mode build, the arguments were saved on
|
|
|
|
* the task's stack and will be cleaned up when the stack memory
|
|
|
|
* is released. Nothing need be done here in that case.
|
2007-02-18 00:21:28 +01:00
|
|
|
*/
|
|
|
|
|
2013-02-04 22:24:00 +01:00
|
|
|
#ifndef CONFIG_DISABLE_PTHREAD
|
|
|
|
if ((tcb->flags & TCB_FLAG_TTYPE_MASK) != TCB_FLAG_TTYPE_PTHREAD)
|
|
|
|
#endif
|
2007-02-18 00:21:28 +01:00
|
|
|
{
|
2013-02-04 22:24:00 +01:00
|
|
|
FAR struct task_tcb_s *ttcb = (FAR struct task_tcb_s *)tcb;
|
|
|
|
for (i = 1; i < CONFIG_MAX_TASK_ARGS+1 && ttcb->argv[i]; i++)
|
2007-02-18 00:21:28 +01:00
|
|
|
{
|
2013-03-10 16:32:33 +01:00
|
|
|
sched_kfree((FAR void*)ttcb->argv[i]);
|
2007-02-18 00:21:28 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-03-22 00:37:11 +01:00
|
|
|
#endif /* CONFIG_CUSTOM_STACK || !CONFIG_NUTTX_KERNEL */
|
2013-03-21 22:51:15 +01:00
|
|
|
|
2012-12-11 22:42:15 +01:00
|
|
|
/* Release this thread's reference to the address environment */
|
|
|
|
|
|
|
|
#ifdef CONFIG_ADDRENV
|
|
|
|
ret = up_addrenv_release(tcb);
|
|
|
|
#endif
|
|
|
|
|
2013-01-25 18:23:38 +01:00
|
|
|
/* Leave the group (if we did not already leady in task_exithook.c) */
|
|
|
|
|
|
|
|
#ifdef HAVE_TASK_GROUP
|
|
|
|
group_leave(tcb);
|
|
|
|
#endif
|
2007-02-18 00:21:28 +01:00
|
|
|
/* And, finally, release the TCB itself */
|
|
|
|
|
2013-03-10 16:32:33 +01:00
|
|
|
sched_kfree(tcb);
|
2007-02-18 00:21:28 +01:00
|
|
|
}
|
2012-01-31 21:32:49 +01:00
|
|
|
|
2007-02-18 00:21:28 +01:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|