2009-03-09 00:33:41 +01:00
|
|
|
/****************************************************************************
|
2014-08-09 00:44:08 +02:00
|
|
|
* sched/task/task_restart.c
|
2007-02-18 00:21:28 +01:00
|
|
|
*
|
2016-02-11 15:06:33 +01:00
|
|
|
* Copyright (C) 2007, 2009, 2012-2013, 2016 Gregory Nutt. All rights reserved.
|
2012-03-23 21:14:21 +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-03-09 00:33:41 +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-03-09 00:33:41 +01:00
|
|
|
****************************************************************************/
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2009-03-09 00:33:41 +01:00
|
|
|
/****************************************************************************
|
2007-02-18 00:21:28 +01:00
|
|
|
* Included Files
|
2009-03-09 00:33:41 +01:00
|
|
|
****************************************************************************/
|
2007-02-18 00:21:28 +01:00
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
2013-02-04 22:24:00 +01:00
|
|
|
|
2007-02-18 00:21:28 +01:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sched.h>
|
2013-02-04 22:24:00 +01:00
|
|
|
#include <errno.h>
|
|
|
|
|
2016-02-14 15:17:46 +01:00
|
|
|
#include <nuttx/irq.h>
|
2007-02-18 00:21:28 +01:00
|
|
|
#include <nuttx/arch.h>
|
2013-02-04 22:24:00 +01:00
|
|
|
|
2014-08-09 01:53:55 +02:00
|
|
|
#include "sched/sched.h"
|
2014-08-08 22:06:42 +02:00
|
|
|
#include "group/group.h"
|
2014-08-08 20:44:44 +02:00
|
|
|
#include "signal/signal.h"
|
2014-08-09 00:44:08 +02:00
|
|
|
#include "task/task.h"
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2009-03-09 00:33:41 +01:00
|
|
|
/****************************************************************************
|
2007-02-18 00:21:28 +01:00
|
|
|
* Public Functions
|
2009-03-09 00:33:41 +01:00
|
|
|
****************************************************************************/
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2009-03-09 00:33:41 +01:00
|
|
|
/****************************************************************************
|
2007-02-18 00:21:28 +01:00
|
|
|
* Name: task_restart
|
|
|
|
*
|
|
|
|
* Description:
|
2012-07-14 21:30:31 +02:00
|
|
|
* This function "restarts" a task. The task is first terminated and then
|
|
|
|
* reinitialized with same ID, priority, original entry point, stack size,
|
|
|
|
* and parameters it had when it was first started.
|
2007-02-18 00:21:28 +01:00
|
|
|
*
|
|
|
|
* Inputs:
|
2012-07-14 21:30:31 +02:00
|
|
|
* pid - The task ID of the task to delete. An ID of zero signifies the
|
|
|
|
* calling task.
|
2007-02-18 00:21:28 +01:00
|
|
|
*
|
|
|
|
* Return Value:
|
|
|
|
* OK on sucess; ERROR on failure.
|
|
|
|
*
|
|
|
|
* This function can fail if:
|
2012-07-14 21:30:31 +02:00
|
|
|
* (1) A pid of zero or the pid of the calling task is provided
|
|
|
|
* (functionality not implemented)
|
|
|
|
* (2) The pid is not associated with any task known to the system.
|
2007-02-18 00:21:28 +01:00
|
|
|
*
|
2009-03-09 00:33:41 +01:00
|
|
|
****************************************************************************/
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2009-12-13 19:01:46 +01:00
|
|
|
int task_restart(pid_t pid)
|
2007-02-18 00:21:28 +01:00
|
|
|
{
|
2013-02-04 19:46:28 +01:00
|
|
|
FAR struct tcb_s *rtcb;
|
2013-02-04 22:24:00 +01:00
|
|
|
FAR struct task_tcb_s *tcb;
|
2016-02-11 15:06:33 +01:00
|
|
|
FAR dq_queue_t *tasklist;
|
2016-02-14 15:17:46 +01:00
|
|
|
irqstate_t flags;
|
2016-06-11 22:40:07 +02:00
|
|
|
int errcode;
|
2013-02-04 19:46:28 +01:00
|
|
|
int status;
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2016-03-23 01:19:57 +01:00
|
|
|
/* Make sure this task does not become ready-to-run while we are futzing
|
|
|
|
* with its TCB
|
2007-02-18 00:21:28 +01:00
|
|
|
*/
|
|
|
|
|
2012-07-14 21:30:31 +02:00
|
|
|
sched_lock();
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2012-07-14 21:30:31 +02:00
|
|
|
/* Check if the task to restart is the calling task */
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2016-02-07 00:44:41 +01:00
|
|
|
rtcb = this_task();
|
2012-07-14 21:30:31 +02:00
|
|
|
if ((pid == 0) || (pid == rtcb->pid))
|
|
|
|
{
|
|
|
|
/* Not implemented */
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2016-06-11 22:40:07 +02:00
|
|
|
errcode = ENOSYS;
|
2016-03-23 01:19:57 +01:00
|
|
|
goto errout_with_lock;
|
2012-07-14 21:30:31 +02:00
|
|
|
}
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2016-02-11 15:06:33 +01:00
|
|
|
/* We are restarting some other task than ourselves */
|
|
|
|
/* Find for the TCB associated with matching pid */
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2016-02-11 15:06:33 +01:00
|
|
|
tcb = (FAR struct task_tcb_s *)sched_gettcb(pid);
|
2013-02-04 22:24:00 +01:00
|
|
|
#ifndef CONFIG_DISABLE_PTHREAD
|
2016-02-11 15:06:33 +01:00
|
|
|
if (!tcb || (tcb->cmn.flags & TCB_FLAG_TTYPE_MASK) == TCB_FLAG_TTYPE_PTHREAD)
|
2013-02-04 22:24:00 +01:00
|
|
|
#else
|
2016-02-11 15:06:33 +01:00
|
|
|
if (!tcb)
|
2013-02-04 22:24:00 +01:00
|
|
|
#endif
|
2016-02-11 15:06:33 +01:00
|
|
|
{
|
|
|
|
/* There is no TCB with this pid or, if there is, it is not a task. */
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2016-06-11 22:40:07 +02:00
|
|
|
errcode = ESRCH;
|
2016-03-23 01:19:57 +01:00
|
|
|
goto errout_with_lock;
|
2016-02-11 15:06:33 +01:00
|
|
|
}
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2016-03-23 01:19:57 +01:00
|
|
|
#ifdef CONFIG_SMP
|
|
|
|
/* There is currently no capability to restart a task that is actively
|
|
|
|
* running on another CPU. This is not the calling task so if it is
|
|
|
|
* running, then it could only be running a a different CPU.
|
|
|
|
*
|
|
|
|
* Also, we will need some interlocks to assure that no tasks are
|
|
|
|
* rescheduled on any other CPU while we do this.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#warning Missing SMP logic
|
|
|
|
if (tcb->cmn.task_state == TSTATE_TASK_RUNNING)
|
|
|
|
{
|
|
|
|
/* Not implemented */
|
|
|
|
|
2016-06-11 22:40:07 +02:00
|
|
|
errcode = ENOSYS;
|
2016-03-23 01:19:57 +01:00
|
|
|
goto errout_with_lock;
|
|
|
|
}
|
|
|
|
#endif /* CONFIG_SMP */
|
|
|
|
|
2016-02-11 15:06:33 +01:00
|
|
|
/* Try to recover from any bad states */
|
2014-04-13 22:32:20 +02:00
|
|
|
|
2016-02-11 15:06:33 +01:00
|
|
|
task_recover((FAR struct tcb_s *)tcb);
|
2013-02-06 16:43:28 +01:00
|
|
|
|
2016-02-11 15:06:33 +01:00
|
|
|
/* Kill any children of this thread */
|
2013-02-06 16:43:28 +01:00
|
|
|
|
2015-09-01 15:04:09 +02:00
|
|
|
#ifdef HAVE_GROUP_MEMBERS
|
2016-02-11 15:06:33 +01:00
|
|
|
(void)group_killchildren(tcb);
|
2013-02-06 16:43:28 +01:00
|
|
|
#endif
|
|
|
|
|
2016-02-11 15:06:33 +01:00
|
|
|
/* Remove the TCB from whatever list it is in. After this point, the TCB
|
|
|
|
* should no longer be accessible to the system
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifdef CONFIG_SMP
|
|
|
|
tasklist = TLIST_HEAD(tcb->cmn.task_state, tcb->cmn.cpu);
|
|
|
|
#else
|
|
|
|
tasklist = TLIST_HEAD(tcb->cmn.task_state);
|
|
|
|
#endif
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2016-02-14 15:17:46 +01:00
|
|
|
flags = enter_critical_section();
|
2016-02-11 15:06:33 +01:00
|
|
|
dq_rem((FAR dq_entry_t *)tcb, tasklist);
|
|
|
|
tcb->cmn.task_state = TSTATE_TASK_INVALID;
|
2016-02-14 15:17:46 +01:00
|
|
|
leave_critical_section(flags);
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2016-02-11 15:06:33 +01:00
|
|
|
/* Deallocate anything left in the TCB's queues */
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2016-02-11 15:06:33 +01:00
|
|
|
sig_cleanup((FAR struct tcb_s *)tcb); /* Deallocate Signal lists */
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2016-02-11 15:06:33 +01:00
|
|
|
/* Reset the current task priority */
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2016-02-11 15:06:33 +01:00
|
|
|
tcb->cmn.sched_priority = tcb->init_priority;
|
2012-03-23 21:14:21 +01:00
|
|
|
|
2016-02-11 15:06:33 +01:00
|
|
|
/* Reset the base task priority and the number of pending reprioritizations */
|
2012-03-23 21:14:21 +01:00
|
|
|
|
2009-03-09 00:33:41 +01:00
|
|
|
#ifdef CONFIG_PRIORITY_INHERITANCE
|
2016-02-11 15:06:33 +01:00
|
|
|
tcb->cmn.base_priority = tcb->init_priority;
|
2009-03-11 01:03:58 +01:00
|
|
|
# if CONFIG_SEM_NNESTPRIO > 0
|
2016-02-11 15:06:33 +01:00
|
|
|
tcb->cmn.npend_reprio = 0;
|
2009-03-11 01:03:58 +01:00
|
|
|
# endif
|
2009-03-09 00:33:41 +01:00
|
|
|
#endif
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2016-02-11 15:06:33 +01:00
|
|
|
/* Re-initialize the processor-specific portion of the TCB. This will
|
|
|
|
* reset the entry point and the start-up parameters
|
|
|
|
*/
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2016-02-11 15:06:33 +01:00
|
|
|
up_initial_state((FAR struct tcb_s *)tcb);
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2016-02-11 15:06:33 +01:00
|
|
|
/* Add the task to the inactive task list */
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2016-02-11 15:06:33 +01:00
|
|
|
dq_addfirst((FAR dq_entry_t *)tcb, (FAR dq_queue_t *)&g_inactivetasks);
|
|
|
|
tcb->cmn.task_state = TSTATE_TASK_INACTIVE;
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2016-02-11 15:06:33 +01:00
|
|
|
/* Activate the task */
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2016-02-11 15:06:33 +01:00
|
|
|
status = task_activate((FAR struct tcb_s *)tcb);
|
|
|
|
if (status != OK)
|
|
|
|
{
|
|
|
|
(void)task_delete(pid);
|
2016-06-11 22:40:07 +02:00
|
|
|
errcode = -status;
|
2016-03-23 01:19:57 +01:00
|
|
|
goto errout_with_lock;
|
2012-07-14 21:30:31 +02:00
|
|
|
}
|
2007-02-18 00:21:28 +01:00
|
|
|
|
|
|
|
sched_unlock();
|
|
|
|
return OK;
|
2016-03-23 01:19:57 +01:00
|
|
|
|
|
|
|
errout_with_lock:
|
2016-06-11 22:40:07 +02:00
|
|
|
set_errno(errcode);
|
2016-03-23 01:19:57 +01:00
|
|
|
sched_unlock();
|
|
|
|
return ERROR;
|
2007-02-18 00:21:28 +01:00
|
|
|
}
|