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
|
|
|
*
|
2020-05-09 17:05:29 +02: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
|
2007-02-18 00:21:28 +01:00
|
|
|
*
|
2020-05-09 17:05:29 +02:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2007-02-18 00:21:28 +01:00
|
|
|
*
|
2020-05-09 17:05:29 +02: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.
|
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-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
|
|
|
/****************************************************************************
|
2022-05-12 08:49:44 +02:00
|
|
|
* Name: nxtask_restart
|
2007-02-18 00:21:28 +01:00
|
|
|
*
|
|
|
|
* 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:
|
2022-05-12 08:49:44 +02:00
|
|
|
* Zero (OK) on success; or negated errno on failure
|
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
|
|
|
|
2022-05-12 08:49:44 +02:00
|
|
|
int nxtask_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;
|
2022-05-12 08:49:44 +02:00
|
|
|
int ret;
|
2016-11-20 14:57:18 +01:00
|
|
|
#ifdef CONFIG_SMP
|
|
|
|
int cpu;
|
|
|
|
#endif
|
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
|
|
|
|
2022-05-12 08:49:44 +02:00
|
|
|
ret = -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
|
|
|
|
2020-05-09 16:04:45 +02:00
|
|
|
tcb = (FAR struct task_tcb_s *)nxsched_get_tcb(pid);
|
2013-02-04 22:24:00 +01:00
|
|
|
#ifndef CONFIG_DISABLE_PTHREAD
|
2020-03-08 13:51:35 +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
|
|
|
|
2022-05-12 08:49:44 +02:00
|
|
|
ret = -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
|
|
|
*/
|
|
|
|
|
2020-05-09 20:40:14 +02:00
|
|
|
cpu = nxsched_pause_cpu(&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
|
|
|
|
This commit renames all internal OS functions defined under sched/task so that they begin with the prefix. For example, nxtask_exit() vs. task_exit().
Squashed commit of the following:
Trivial, cosmetic
sched/, arch/, and include: Rename task_vforkstart() as nxtask_vforkstart()
sched/, arch/, and include: Rename task_vforkabort() as nxtask_vforkabort()
sched/, arch/, and include: Rename task_vforksetup() as nxtask_vfork_setup()
sched/: Rename notify_cancellation() as nxnotify_cancellation()
sched/: Rename task_recover() to nxtask_recover()
sched/task, sched/pthread/, Documentation/: Rename task_argsetup() and task_terminate() to nxtask_argsetup() and nxtask_terminate(), respectively.
sched/task: Rename task_schedsetup() to nxtask_schedsetup()
sched/ (plus some binfmt/, include/, and arch/): Rename task_start() and task_starthook() to nxtask_start() and nxtask_starthook().
arch/ and sched/: Rename task_exit() and task_exithook() to nxtask_exit() and nxtask_exithook(), respectively.
sched/task: Rename all internal, static, functions to begin with the nx prefix.
2019-02-04 20:42:51 +01:00
|
|
|
nxtask_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
|
2020-06-03 13:33:56 +02:00
|
|
|
group_kill_children((FAR struct tcb_s *)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
|
2022-09-19 12:17:14 +02:00
|
|
|
tasklist = TLIST_HEAD(&tcb->cmn, tcb->cmn.cpu);
|
2016-02-11 15:06:33 +01:00
|
|
|
#else
|
2022-09-19 12:17:14 +02:00
|
|
|
tasklist = TLIST_HEAD(&tcb->cmn);
|
2016-02-11 15:06:33 +01:00
|
|
|
#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-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 */
|
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
|
|
|
|
|
2020-05-09 17:05:29 +02: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;
|
2022-05-24 00:06:10 +02:00
|
|
|
tcb->cmn.boost_priority = 0;
|
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
|
|
|
|
2022-08-30 03:36:44 +02:00
|
|
|
dq_addfirst((FAR dq_entry_t *)tcb, &g_inactivetasks);
|
2016-02-11 15:06:33 +01:00
|
|
|
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)
|
|
|
|
{
|
2022-05-12 08:49:44 +02:00
|
|
|
ret = up_cpu_resume(cpu);
|
2016-11-20 14:57:18 +01:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
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
|
|
|
|
2020-06-04 14:19:40 +02:00
|
|
|
nxtask_activate((FAR struct tcb_s *)tcb);
|
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:
|
2022-05-12 08:49:44 +02:00
|
|
|
return ret;
|
2007-02-18 00:21:28 +01:00
|
|
|
}
|
2022-05-12 08:49:44 +02:00
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: task_restart
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* pid - The task ID of the task to delete. An ID of zero signifies the
|
|
|
|
* calling task.
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* Zero (OK) on success; -1 (ERROR) on failure with the errno variable set
|
|
|
|
* appropriately.
|
|
|
|
*
|
|
|
|
* This function can fail if:
|
|
|
|
* (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.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#ifndef CONFIG_BUILD_KERNEL
|
|
|
|
int task_restart(pid_t pid)
|
|
|
|
{
|
|
|
|
int ret = nxtask_restart(pid);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
set_errno(-ret);
|
|
|
|
ret = ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
#endif
|