2008-01-01 15:54:10 +01:00
|
|
|
/****************************************************************************
|
2007-04-28 21:39:18 +02:00
|
|
|
* common/up_exit.c
|
|
|
|
*
|
2017-01-13 18:08:24 +01:00
|
|
|
* Copyright (C) 2007-2009, 201-2014, 2017 Gregory Nutt. All rights reserved.
|
2012-01-31 21:32:49 +01:00
|
|
|
* Author: Gregory Nutt <gnutt@nuttx.org>
|
2007-04-28 21:39:18 +02: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.
|
2008-01-01 15:54:10 +01:00
|
|
|
* 3. Neither the name NuttX nor the names of its contributors may be
|
2007-04-28 21:39:18 +02: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.
|
|
|
|
*
|
2008-01-01 15:54:10 +01:00
|
|
|
****************************************************************************/
|
2007-04-28 21:39:18 +02:00
|
|
|
|
2008-01-01 15:54:10 +01:00
|
|
|
/****************************************************************************
|
2007-04-28 21:39:18 +02:00
|
|
|
* Included Files
|
2008-01-01 15:54:10 +01:00
|
|
|
****************************************************************************/
|
2007-04-28 21:39:18 +02:00
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
2009-12-16 21:05:51 +01:00
|
|
|
|
2007-04-28 21:39:18 +02:00
|
|
|
#include <sched.h>
|
|
|
|
#include <debug.h>
|
2009-05-19 22:14:44 +02:00
|
|
|
|
2017-01-13 18:08:24 +01:00
|
|
|
#include <nuttx/arch.h>
|
|
|
|
#include <nuttx/irq.h>
|
2007-04-28 21:39:18 +02:00
|
|
|
#ifdef CONFIG_DUMP_ON_EXIT
|
2017-01-13 18:08:24 +01:00
|
|
|
# include <nuttx/fs/fs.h>
|
2007-04-28 21:39:18 +02:00
|
|
|
#endif
|
|
|
|
|
2014-08-09 00:44:08 +02:00
|
|
|
#include "task/task.h"
|
2014-08-09 01:53:55 +02:00
|
|
|
#include "sched/sched.h"
|
2014-08-28 00:31:02 +02:00
|
|
|
#include "group/group.h"
|
2016-11-24 20:33:43 +01:00
|
|
|
#include "irq/irq.h"
|
2014-08-09 00:44:08 +02:00
|
|
|
#include "up_internal.h"
|
|
|
|
|
2016-06-17 23:21:07 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Pre-processor Definitions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#ifndef CONFIG_DEBUG_SCHED_INFO
|
|
|
|
# undef CONFIG_DUMP_ON_EXIT
|
|
|
|
#endif
|
|
|
|
|
2008-01-01 15:54:10 +01:00
|
|
|
/****************************************************************************
|
2008-01-30 19:49:31 +01:00
|
|
|
* Private Functions
|
2008-01-01 15:54:10 +01:00
|
|
|
****************************************************************************/
|
2007-04-28 21:39:18 +02:00
|
|
|
|
2008-01-01 15:54:10 +01:00
|
|
|
/****************************************************************************
|
2007-04-28 21:39:18 +02:00
|
|
|
* Name: _up_dumponexit
|
|
|
|
*
|
|
|
|
* Description:
|
2008-01-30 19:49:31 +01:00
|
|
|
* Dump the state of all tasks whenever on task exits. This is debug
|
|
|
|
* instrumentation that was added to check file-related reference counting
|
|
|
|
* but could be useful again sometime in the future.
|
2007-04-28 21:39:18 +02:00
|
|
|
*
|
2008-01-01 15:54:10 +01:00
|
|
|
****************************************************************************/
|
2007-04-28 21:39:18 +02:00
|
|
|
|
2016-06-17 23:21:07 +02:00
|
|
|
#ifdef CONFIG_DUMP_ON_EXIT
|
2013-02-04 19:46:28 +01:00
|
|
|
static void _up_dumponexit(FAR struct tcb_s *tcb, FAR void *arg)
|
2007-04-28 21:39:18 +02:00
|
|
|
{
|
2013-01-26 23:25:21 +01:00
|
|
|
#if CONFIG_NFILE_DESCRIPTORS > 0
|
|
|
|
FAR struct filelist *filelist;
|
|
|
|
#if CONFIG_NFILE_STREAMS > 0
|
|
|
|
FAR struct streamlist *streamlist;
|
|
|
|
#endif
|
2007-04-28 21:39:18 +02:00
|
|
|
int i;
|
2007-11-23 22:16:43 +01:00
|
|
|
#endif
|
|
|
|
|
2016-06-17 23:21:07 +02:00
|
|
|
sinfo(" TCB=%p name=%s pid=%d\n", tcb, tcb->argv[0], tcb->pid);
|
|
|
|
sinfo(" priority=%d state=%d\n", tcb->sched_priority, tcb->task_state);
|
2007-11-20 21:32:33 +01:00
|
|
|
|
|
|
|
#if CONFIG_NFILE_DESCRIPTORS > 0
|
2013-01-26 23:25:21 +01:00
|
|
|
filelist = tcb->group->tg_filelist;
|
|
|
|
for (i = 0; i < CONFIG_NFILE_DESCRIPTORS; i++)
|
2007-04-28 21:39:18 +02:00
|
|
|
{
|
2013-01-26 23:25:21 +01:00
|
|
|
struct inode *inode = filelist->fl_files[i].f_inode;
|
|
|
|
if (inode)
|
2007-04-28 21:39:18 +02:00
|
|
|
{
|
2016-06-17 23:21:07 +02:00
|
|
|
sinfo(" fd=%d refcount=%d\n",
|
|
|
|
i, inode->i_crefssinfo);
|
2007-04-28 21:39:18 +02:00
|
|
|
}
|
|
|
|
}
|
2007-11-20 21:32:33 +01:00
|
|
|
#endif
|
2007-04-28 21:39:18 +02:00
|
|
|
|
2007-11-20 21:32:33 +01:00
|
|
|
#if CONFIG_NFILE_STREAMS > 0
|
2013-01-26 23:25:21 +01:00
|
|
|
streamlist = tcb->group->tg_streamlist;
|
|
|
|
for (i = 0; i < CONFIG_NFILE_STREAMS; i++)
|
2007-04-28 21:39:18 +02:00
|
|
|
{
|
2013-01-26 23:25:21 +01:00
|
|
|
struct file_struct *filep = &streamlist->sl_streams[i];
|
2013-09-29 00:50:07 +02:00
|
|
|
if (filep->fs_fd >= 0)
|
2007-04-28 21:39:18 +02:00
|
|
|
{
|
2017-02-09 16:24:44 +01:00
|
|
|
#ifndef CONFIG_STDIO_DISABLE_BUFFERING
|
|
|
|
if (filep->fs_bufstart != NULL)
|
|
|
|
{
|
|
|
|
sinfo(" fd=%d nbytes=%d\n",
|
|
|
|
filep->fs_fd,
|
|
|
|
filep->fs_bufpos - filep->fs_bufstart);
|
|
|
|
}
|
|
|
|
else
|
2017-02-09 21:38:15 +01:00
|
|
|
#endif
|
2017-02-09 16:24:44 +01:00
|
|
|
{
|
|
|
|
sinfo(" fd=%d\n", filep->fs_fd);
|
|
|
|
}
|
2007-04-28 21:39:18 +02:00
|
|
|
}
|
|
|
|
}
|
2007-11-20 21:32:33 +01:00
|
|
|
#endif
|
2007-04-28 21:39:18 +02:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2008-01-01 15:54:10 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
2007-04-28 21:39:18 +02:00
|
|
|
|
2008-01-01 15:54:10 +01:00
|
|
|
/****************************************************************************
|
2007-04-28 21:39:18 +02:00
|
|
|
* Name: _exit
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* This function causes the currently executing task to cease
|
2008-01-30 19:49:31 +01:00
|
|
|
* to exist. This is a special case of task_delete() where the task to
|
|
|
|
* be deleted is the currently executing task. It is more complex because
|
2009-06-25 19:44:35 +02:00
|
|
|
* a context switch must be perform to the next ready to run task.
|
2007-04-28 21:39:18 +02:00
|
|
|
*
|
2008-01-01 15:54:10 +01:00
|
|
|
****************************************************************************/
|
2007-04-28 21:39:18 +02:00
|
|
|
|
|
|
|
void _exit(int status)
|
|
|
|
{
|
2015-10-07 01:28:32 +02:00
|
|
|
struct tcb_s *tcb;
|
2007-04-28 21:39:18 +02:00
|
|
|
|
2017-01-13 18:08:24 +01:00
|
|
|
/* Make sure that we are in a critical section with local interrupts.
|
|
|
|
* The IRQ state will be restored when the next task is started.
|
2007-04-28 21:39:18 +02:00
|
|
|
*/
|
|
|
|
|
2017-01-13 18:08:24 +01:00
|
|
|
(void)enter_critical_section();
|
2007-04-28 21:39:18 +02:00
|
|
|
|
2016-06-20 19:59:15 +02:00
|
|
|
sinfo("TCB=%p exiting\n", this_task());
|
2007-04-28 21:39:18 +02:00
|
|
|
|
2016-06-17 23:21:07 +02:00
|
|
|
#ifdef CONFIG_DUMP_ON_EXIT
|
2016-06-20 19:59:15 +02:00
|
|
|
sinfo("Other tasks:\n");
|
2007-04-28 21:39:18 +02:00
|
|
|
sched_foreach(_up_dumponexit, NULL);
|
|
|
|
#endif
|
|
|
|
|
2008-01-30 19:49:31 +01:00
|
|
|
/* Destroy the task at the head of the ready to run list. */
|
2007-04-28 21:39:18 +02:00
|
|
|
|
2013-04-19 00:15:20 +02:00
|
|
|
(void)task_exit();
|
2007-04-28 21:39:18 +02:00
|
|
|
|
|
|
|
/* Now, perform the context switch to the new ready-to-run task at the
|
|
|
|
* head of the list.
|
|
|
|
*/
|
|
|
|
|
2016-02-06 20:41:28 +01:00
|
|
|
tcb = this_task();
|
2007-04-28 21:39:18 +02:00
|
|
|
|
2014-08-28 00:31:02 +02:00
|
|
|
#ifdef CONFIG_ARCH_ADDRENV
|
|
|
|
/* Make sure that the address environment for the previously running
|
|
|
|
* task is closed down gracefully (data caches dump, MMU flushed) and
|
|
|
|
* set up the address environment for the new thread at the head of
|
|
|
|
* the ready-to-run list.
|
|
|
|
*/
|
|
|
|
|
|
|
|
(void)group_addrenv(tcb);
|
|
|
|
#endif
|
|
|
|
|
2007-04-28 21:39:18 +02:00
|
|
|
/* Then switch contexts */
|
|
|
|
|
|
|
|
up_fullcontextrestore(tcb->xcp.regs);
|
|
|
|
}
|