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
|
|
|
*
|
2018-09-14 14:51:38 +02:00
|
|
|
* Copyright (C) 2007, 2009, 2012-2013, 2016, 2018 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
|
|
|
*
|
2018-02-01 17:00:02 +01:00
|
|
|
* Input Parameters:
|
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
|
|
|
*
|
2018-02-01 17:00:02 +01:00
|
|
|
* Returned Value:
|
2018-09-14 14:51:38 +02:00
|
|
|
* Zero (OK) on success; -1 (ERROR) on failure with the errno variable set
|
|
|
|
* appropriately.
|
2007-02-18 00:21:28 +01:00
|
|
|
*
|
|
|
|
* 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;
|
2016-11-20 14:57:18 +01:00
|
|
|
#ifdef CONFIG_SMP
|
|
|
|
int cpu;
|
|
|
|
#endif
|
|
|
|
int ret;
|
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-11-22 18:34:16 +01:00
|
|
|
goto errout;
|
2012-07-14 21:30:31 +02:00
|
|
|
}
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2016-11-20 14:57:18 +01:00
|
|
|
/* We are restarting some other task than ourselves. Make sure that the
|
|
|
|
* task does not change its state while we are executing. In the single
|
|
|
|
* CPU state this could be done by disabling pre-emption. But we will
|
|
|
|
* a little stronger medicine on the SMP case: The task make be running
|
|
|
|
* on another CPU.
|
|
|
|
*/
|
|
|
|
|
|
|
|
flags = enter_critical_section();
|
|
|
|
|
2016-02-11 15:06:33 +01:00
|
|
|
/* 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
|
2016-11-20 14:57:18 +01:00
|
|
|
/* If the task is running on another CPU, then pause that CPU. We can
|
|
|
|
* then manipulate the TCB of the restarted task and when we resume the
|
|
|
|
* that CPU, the restart take effect.
|
2016-03-23 01:19:57 +01:00
|
|
|
*/
|
|
|
|
|
2016-11-20 14:57:18 +01:00
|
|
|
cpu = sched_cpu_pause(&tcb->cmn);
|
2016-03-23 01:19:57 +01:00
|
|
|
#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-11 15:06:33 +01:00
|
|
|
dq_rem((FAR dq_entry_t *)tcb, tasklist);
|
|
|
|
tcb->cmn.task_state = TSTATE_TASK_INVALID;
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2018-09-15 22:51:04 +02:00
|
|
|
#ifndef CONFIG_DISABLE_SIGNALS
|
2018-09-14 14:51:38 +02:00
|
|
|
/* Deallocate anything left in the TCB's signal queues */
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2018-09-14 14:51:38 +02:00
|
|
|
nxsig_cleanup((FAR struct tcb_s *)tcb); /* Deallocate Signal lists */
|
|
|
|
tcb->cmn.sigprocmask = NULL_SIGNAL_SET; /* Reset sigprocmask */
|
2018-09-15 22:51:04 +02:00
|
|
|
#endif
|
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-10-01 19:38:22 +02:00
|
|
|
tcb->cmn.sched_priority = tcb->cmn.init_priority;
|
2012-03-23 21:14:21 +01:00
|
|
|
|
2016-11-21 14:33:23 +01:00
|
|
|
/* The task should restart with pre-emption disabled and not in a critical
|
2018-09-14 14:51:38 +02:00
|
|
|
* section.
|
2016-11-21 14:33:23 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
tcb->cmn.lockcount = 0;
|
|
|
|
#ifdef CONFIG_SMP
|
|
|
|
tcb->cmn.irqcount = 0;
|
|
|
|
#endif
|
|
|
|
|
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-10-01 19:38:22 +02:00
|
|
|
tcb->cmn.base_priority = tcb->cmn.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-11-20 14:57:18 +01:00
|
|
|
#ifdef CONFIG_SMP
|
|
|
|
/* Resume the paused CPU (if any) */
|
|
|
|
|
|
|
|
if (cpu >= 0)
|
|
|
|
{
|
|
|
|
ret = up_cpu_resume(cpu);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
errcode = -ret;
|
|
|
|
goto errout_with_lock;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif /* CONFIG_SMP */
|
|
|
|
|
2016-11-21 14:33:23 +01:00
|
|
|
leave_critical_section(flags);
|
|
|
|
|
|
|
|
/* Activate the task. */
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2016-11-20 14:57:18 +01:00
|
|
|
ret = task_activate((FAR struct tcb_s *)tcb);
|
|
|
|
if (ret != OK)
|
2016-02-11 15:06:33 +01:00
|
|
|
{
|
2016-12-10 21:39:19 +01:00
|
|
|
(void)task_terminate(pid, true);
|
2016-11-20 14:57:18 +01:00
|
|
|
errcode = -ret;
|
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
|
|
|
|
|
|
|
return OK;
|
2016-03-23 01:19:57 +01:00
|
|
|
|
|
|
|
errout_with_lock:
|
2016-11-20 14:57:18 +01:00
|
|
|
leave_critical_section(flags);
|
2016-11-22 18:34:16 +01:00
|
|
|
errout:
|
|
|
|
set_errno(errcode);
|
2016-03-23 01:19:57 +01:00
|
|
|
return ERROR;
|
2007-02-18 00:21:28 +01:00
|
|
|
}
|