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
|
|
|
*
|
2024-09-11 13:45:11 +02:00
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
*
|
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
|
|
|
|
2024-09-29 15:38:23 +02:00
|
|
|
#ifndef CONFIG_BUILD_KERNEL
|
|
|
|
|
2009-03-09 00:33:41 +01:00
|
|
|
/****************************************************************************
|
2024-09-29 15:38:23 +02:00
|
|
|
* Private Type Declarations
|
2009-03-09 00:33:41 +01:00
|
|
|
****************************************************************************/
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2024-09-29 15:38:23 +02:00
|
|
|
#ifdef CONFIG_SMP
|
|
|
|
struct restart_arg_s
|
|
|
|
{
|
|
|
|
pid_t pid;
|
|
|
|
cpu_set_t saved_affinity;
|
|
|
|
uint16_t saved_flags;
|
|
|
|
bool need_restore;
|
|
|
|
};
|
|
|
|
|
2009-03-09 00:33:41 +01:00
|
|
|
/****************************************************************************
|
2024-09-29 15:38:23 +02:00
|
|
|
* Private Functions
|
2009-03-09 00:33:41 +01:00
|
|
|
****************************************************************************/
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2024-09-29 15:38:23 +02:00
|
|
|
static int restart_handler(FAR void *cookie)
|
2007-02-18 00:21:28 +01:00
|
|
|
{
|
2024-09-29 15:38:23 +02:00
|
|
|
FAR struct restart_arg_s *arg = cookie;
|
|
|
|
FAR struct tcb_s *tcb;
|
2016-02-14 15:17:46 +01:00
|
|
|
irqstate_t flags;
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2024-09-29 15:38:23 +02:00
|
|
|
flags = enter_critical_section();
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2024-09-29 15:38:23 +02:00
|
|
|
/* tcb that we want restart */
|
|
|
|
|
|
|
|
tcb = nxsched_get_tcb(arg->pid);
|
|
|
|
if (!tcb || tcb->task_state == TSTATE_TASK_INVALID ||
|
|
|
|
(tcb->flags & TCB_FLAG_EXIT_PROCESSING) != 0)
|
2012-07-14 21:30:31 +02:00
|
|
|
{
|
2024-09-29 15:38:23 +02: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
|
|
|
|
2024-09-29 15:38:23 +02:00
|
|
|
leave_critical_section(flags);
|
|
|
|
return -ESRCH;
|
2012-07-14 21:30:31 +02:00
|
|
|
}
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2024-09-29 15:38:23 +02:00
|
|
|
if (arg->need_restore)
|
|
|
|
{
|
|
|
|
tcb->affinity = arg->saved_affinity;
|
|
|
|
tcb->flags = arg->saved_flags;
|
|
|
|
}
|
2016-11-20 14:57:18 +01:00
|
|
|
|
2024-09-29 15:38:23 +02:00
|
|
|
nxsched_remove_readytorun(tcb, false);
|
2016-11-20 14:57:18 +01:00
|
|
|
|
2024-09-29 15:38:23 +02:00
|
|
|
leave_critical_section(flags);
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2024-09-29 15:38:23 +02:00
|
|
|
return OK;
|
|
|
|
}
|
2013-02-04 22:24:00 +01:00
|
|
|
#endif
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2024-09-29 15:38:23 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: nxtask_reset_task
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* We use this function to reset tcb
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
2016-03-23 01:19:57 +01:00
|
|
|
|
2024-09-29 15:38:23 +02:00
|
|
|
static void nxtask_reset_task(FAR struct tcb_s *tcb, bool remove)
|
|
|
|
{
|
2016-02-11 15:06:33 +01:00
|
|
|
/* Try to recover from any bad states */
|
2014-04-13 22:32:20 +02:00
|
|
|
|
2024-09-29 15:38:23 +02:00
|
|
|
nxtask_recover(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
|
2024-09-29 15:38:23 +02:00
|
|
|
group_kill_children(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
|
|
|
|
*/
|
|
|
|
|
2024-09-29 15:38:23 +02:00
|
|
|
if (remove)
|
2024-01-10 09:03:08 +01:00
|
|
|
{
|
2024-09-29 15:38:23 +02:00
|
|
|
nxsched_remove_readytorun(tcb, false);
|
2024-01-10 09:03:08 +01:00
|
|
|
}
|
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
|
|
|
|
2024-09-29 15:38:23 +02:00
|
|
|
nxsig_cleanup(tcb); /* Deallocate Signal lists */
|
|
|
|
sigemptyset(&tcb->sigprocmask); /* 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
|
|
|
|
2024-09-29 15:38:23 +02:00
|
|
|
tcb->sched_priority = tcb->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
|
|
|
*/
|
|
|
|
|
2024-09-29 15:38:23 +02:00
|
|
|
tcb->lockcount = 0;
|
2016-11-21 14:33:23 +01:00
|
|
|
#ifdef CONFIG_SMP
|
2024-09-29 15:38:23 +02:00
|
|
|
tcb->irqcount = 0;
|
2016-11-21 14:33:23 +01:00
|
|
|
#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
|
2024-09-29 15:38:23 +02:00
|
|
|
tcb->base_priority = tcb->init_priority;
|
|
|
|
tcb->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
|
|
|
|
2024-09-29 15:38:23 +02:00
|
|
|
up_initial_state(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
|
|
|
|
2024-03-19 05:33:08 +01:00
|
|
|
dq_addfirst((FAR dq_entry_t *)tcb, list_inactivetasks());
|
2024-09-29 15:38:23 +02:00
|
|
|
tcb->task_state = TSTATE_TASK_INACTIVE;
|
|
|
|
}
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2024-09-29 15:38:23 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: nxtask_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; or negated errno on failure
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int nxtask_restart(pid_t pid)
|
|
|
|
{
|
|
|
|
FAR struct tcb_s *rtcb;
|
|
|
|
FAR struct tcb_s *tcb;
|
|
|
|
irqstate_t flags;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
/* 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();
|
|
|
|
|
|
|
|
/* Check if the task to restart is the calling task */
|
|
|
|
|
|
|
|
rtcb = this_task();
|
|
|
|
if (pid == 0 || pid == rtcb->pid)
|
|
|
|
{
|
|
|
|
/* Not implemented */
|
|
|
|
|
|
|
|
ret = -ENOSYS;
|
|
|
|
goto errout_with_lock;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Find for the TCB associated with matching pid */
|
|
|
|
|
|
|
|
tcb = nxsched_get_tcb(pid);
|
|
|
|
#ifndef CONFIG_DISABLE_PTHREAD
|
|
|
|
if (!tcb || (tcb->flags & TCB_FLAG_TTYPE_MASK) ==
|
|
|
|
TCB_FLAG_TTYPE_PTHREAD)
|
|
|
|
#else
|
|
|
|
if (!tcb)
|
|
|
|
#endif
|
|
|
|
{
|
|
|
|
/* There is no TCB with this pid or, if there is, it is not a task. */
|
|
|
|
|
|
|
|
ret = -ESRCH;
|
|
|
|
goto errout_with_lock;
|
|
|
|
}
|
2016-11-20 14:57:18 +01:00
|
|
|
|
2024-09-29 15:38:23 +02:00
|
|
|
#ifdef CONFIG_SMP
|
|
|
|
if (tcb->task_state == TSTATE_TASK_RUNNING &&
|
|
|
|
tcb->cpu != this_cpu())
|
2016-11-20 14:57:18 +01:00
|
|
|
{
|
2024-09-29 15:38:23 +02:00
|
|
|
struct restart_arg_s arg;
|
|
|
|
|
|
|
|
if ((tcb->flags & TCB_FLAG_CPU_LOCKED) != 0)
|
|
|
|
{
|
|
|
|
arg.pid = tcb->pid;
|
|
|
|
arg.need_restore = false;
|
|
|
|
}
|
|
|
|
else
|
2016-11-20 14:57:18 +01:00
|
|
|
{
|
2024-09-29 15:38:23 +02:00
|
|
|
arg.pid = tcb->pid;
|
|
|
|
arg.saved_flags = tcb->flags;
|
|
|
|
arg.saved_affinity = tcb->affinity;
|
|
|
|
arg.need_restore = true;
|
|
|
|
|
|
|
|
tcb->flags |= TCB_FLAG_CPU_LOCKED;
|
|
|
|
CPU_SET(tcb->cpu, &tcb->affinity);
|
|
|
|
}
|
|
|
|
|
|
|
|
nxsched_smp_call_single(tcb->cpu, restart_handler, &arg, true);
|
|
|
|
|
|
|
|
tcb = nxsched_get_tcb(pid);
|
|
|
|
if (!tcb || tcb->task_state != TSTATE_TASK_INVALID ||
|
|
|
|
(tcb->flags & TCB_FLAG_EXIT_PROCESSING) != 0)
|
|
|
|
{
|
|
|
|
ret = -ESRCH;
|
2016-11-20 14:57:18 +01:00
|
|
|
goto errout_with_lock;
|
|
|
|
}
|
2024-09-29 15:38:23 +02:00
|
|
|
|
|
|
|
DEBUGASSERT(tcb->task_state != TSTATE_TASK_RUNNING);
|
|
|
|
nxtask_reset_task(tcb, false);
|
|
|
|
leave_critical_section(flags);
|
|
|
|
|
|
|
|
/* Activate the task. */
|
|
|
|
|
|
|
|
nxtask_activate(tcb);
|
|
|
|
|
|
|
|
return OK;
|
2016-11-20 14:57:18 +01:00
|
|
|
}
|
|
|
|
#endif /* CONFIG_SMP */
|
|
|
|
|
2024-09-29 15:38:23 +02:00
|
|
|
nxtask_reset_task(tcb, true);
|
2016-11-21 14:33:23 +01:00
|
|
|
leave_critical_section(flags);
|
|
|
|
|
|
|
|
/* Activate the task. */
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2024-09-29 15:38:23 +02:00
|
|
|
nxtask_activate(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);
|
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
|
|
|
|
2023-05-30 06:51:09 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
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.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
int task_restart(pid_t pid)
|
|
|
|
{
|
|
|
|
int ret = nxtask_restart(pid);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
set_errno(-ret);
|
|
|
|
ret = ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
#endif
|