2020-03-08 13:51:36 +01:00
|
|
|
/*******************************************************************************
|
2014-08-09 00:44:08 +02:00
|
|
|
* sched/task/task_terminate.c
|
2013-04-26 00:23:30 +02:00
|
|
|
*
|
2021-02-08 16:33:58 +01: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
|
2013-04-26 00:23:30 +02:00
|
|
|
*
|
2021-02-08 16:33:58 +01:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2013-04-26 00:23:30 +02:00
|
|
|
*
|
2021-02-08 16:33:58 +01: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.
|
2013-04-26 00:23:30 +02:00
|
|
|
*
|
2020-03-08 13:51:36 +01:00
|
|
|
*******************************************************************************/
|
2013-04-26 00:23:30 +02:00
|
|
|
|
2020-03-08 13:51:36 +01:00
|
|
|
/*******************************************************************************
|
2013-04-26 00:23:30 +02:00
|
|
|
* Included Files
|
2020-03-08 13:51:36 +01:00
|
|
|
*******************************************************************************/
|
2013-04-26 00:23:30 +02:00
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
|
|
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <assert.h>
|
2014-02-18 18:50:32 +01:00
|
|
|
#include <errno.h>
|
2013-04-26 00:23:30 +02:00
|
|
|
|
2022-09-25 17:08:38 +02:00
|
|
|
#include <nuttx/queue.h>
|
2013-04-26 00:23:30 +02:00
|
|
|
#include <nuttx/sched.h>
|
2016-02-14 15:17:46 +01:00
|
|
|
#include <nuttx/irq.h>
|
2016-03-17 16:49:43 +01:00
|
|
|
#include <nuttx/sched_note.h>
|
2013-04-26 00:23:30 +02:00
|
|
|
|
2014-08-09 01:53:55 +02:00
|
|
|
#include "sched/sched.h"
|
2019-04-29 22:52:05 +02:00
|
|
|
#include "signal/signal.h"
|
2014-08-09 00:44:08 +02:00
|
|
|
#include "task/task.h"
|
2013-04-26 00:23:30 +02:00
|
|
|
|
2020-03-08 13:51:36 +01:00
|
|
|
/*******************************************************************************
|
2013-04-26 00:23:30 +02:00
|
|
|
* Public Functions
|
2020-03-08 13:51:36 +01:00
|
|
|
*******************************************************************************/
|
2013-04-26 00:23:30 +02:00
|
|
|
|
2020-03-08 13:51:36 +01: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
|
|
|
* Name: nxtask_terminate
|
2013-04-26 00:23:30 +02:00
|
|
|
*
|
|
|
|
* Description:
|
2019-08-01 16:09:50 +02:00
|
|
|
* This function causes a specified task to cease to exist. Its stack and
|
2013-04-26 00:23:30 +02:00
|
|
|
* TCB will be deallocated. This function is the internal implementation
|
|
|
|
* of the task_delete() function. It includes and additional parameter
|
|
|
|
* to determine if blocking is permitted or not.
|
|
|
|
*
|
|
|
|
* This function is the final function called all task termination
|
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
|
|
|
* sequences. nxtask_terminate() is called only from task_delete() (with
|
|
|
|
* nonblocking == false) and from nxtask_exit() (with nonblocking == true).
|
2013-04-26 00:23:30 +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
|
|
|
* The path through nxtask_exit() supports the final stops of the exit(),
|
2013-04-26 00:23:30 +02:00
|
|
|
* _exit(), and pthread_exit
|
|
|
|
*
|
|
|
|
* - pthread_exit(). Calls _exit()
|
|
|
|
* - exit(). Calls _exit()
|
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
|
|
|
* - _exit(). Calls nxtask_exit() making the currently running task
|
|
|
|
* non-running. nxtask_exit then calls nxtask_terminate() (with nonblocking
|
2013-04-26 00:23:30 +02:00
|
|
|
* == true) to terminate the non-running task.
|
|
|
|
*
|
|
|
|
* NOTE: that the state of non-blocking is irrelevant when called through
|
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
|
|
|
* exit() and pthread_exit(). In those cases nxtask_exithook() has already
|
2013-04-26 00:23:30 +02:00
|
|
|
* been called with nonblocking == false;
|
|
|
|
*
|
2018-02-01 17:00:02 +01:00
|
|
|
* Input Parameters:
|
2013-04-26 00:23:30 +02:00
|
|
|
* pid - The task ID of the task to delete. A pid of zero
|
|
|
|
* signifies the calling task.
|
|
|
|
* nonblocking - True: The task is an unhealthy, partially torn down
|
|
|
|
* state and is not permitted to block.
|
|
|
|
*
|
2018-02-01 17:00:02 +01:00
|
|
|
* Returned Value:
|
2013-04-26 00:23:30 +02:00
|
|
|
* OK on success; or ERROR on failure
|
|
|
|
*
|
|
|
|
* This function can fail if the provided pid does not correspond to a
|
|
|
|
* task (errno is not set)
|
|
|
|
*
|
2020-03-08 13:51:36 +01:00
|
|
|
*******************************************************************************/
|
2013-04-26 00:23:30 +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
|
|
|
int nxtask_terminate(pid_t pid, bool nonblocking)
|
2013-04-26 00:23:30 +02:00
|
|
|
{
|
|
|
|
FAR struct tcb_s *dtcb;
|
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-11-20 14:57:18 +01:00
|
|
|
#ifdef CONFIG_SMP
|
|
|
|
int cpu;
|
|
|
|
#endif
|
|
|
|
int ret;
|
2013-04-26 00:23:30 +02:00
|
|
|
|
2016-11-20 14:57:18 +01:00
|
|
|
/* Make sure the task does not become ready-to-run while we are futzing
|
|
|
|
* with its TCB. Within the critical section, no new task may be started
|
|
|
|
* or terminated (even in the SMP case).
|
2013-04-26 00:23:30 +02:00
|
|
|
*/
|
|
|
|
|
2016-11-20 14:57:18 +01:00
|
|
|
flags = enter_critical_section();
|
2013-04-26 00:23:30 +02:00
|
|
|
|
2014-02-18 18:50:32 +01:00
|
|
|
/* Find for the TCB associated with matching PID */
|
2013-04-26 00:23:30 +02:00
|
|
|
|
2020-05-09 16:04:45 +02:00
|
|
|
dtcb = nxsched_get_tcb(pid);
|
2013-04-26 00:23:30 +02:00
|
|
|
if (!dtcb)
|
|
|
|
{
|
2014-02-18 18:50:32 +01:00
|
|
|
/* This PID does not correspond to any known task */
|
2013-04-26 00:23:30 +02:00
|
|
|
|
2016-11-20 14:57:18 +01:00
|
|
|
ret = -ESRCH;
|
|
|
|
goto errout_with_lock;
|
2013-04-26 00:23:30 +02:00
|
|
|
}
|
|
|
|
|
2016-11-20 14:57:18 +01:00
|
|
|
/* Verify our internal sanity */
|
|
|
|
|
|
|
|
#ifdef CONFIG_SMP
|
|
|
|
DEBUGASSERT(dtcb->task_state < NUM_TASK_STATES);
|
|
|
|
#else
|
|
|
|
DEBUGASSERT(dtcb->task_state != TSTATE_TASK_RUNNING &&
|
|
|
|
dtcb->task_state < NUM_TASK_STATES);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Remove the task from the OS's task lists. We must be in a critical
|
|
|
|
* section and the must must not be running to do this.
|
|
|
|
*/
|
|
|
|
|
2016-02-11 15:06:33 +01:00
|
|
|
#ifdef CONFIG_SMP
|
2016-11-20 14:57:18 +01:00
|
|
|
/* In the SMP case, the thread may be running on another CPU. If that is
|
|
|
|
* the case, then we will pause the CPU that the thread is running on.
|
2016-02-11 15:06:33 +01:00
|
|
|
*/
|
|
|
|
|
2020-05-09 20:40:14 +02:00
|
|
|
cpu = nxsched_pause_cpu(dtcb);
|
2016-11-20 14:57:18 +01:00
|
|
|
|
2017-05-11 21:35:56 +02:00
|
|
|
/* Get the task list associated with the thread's state and CPU */
|
2016-11-20 14:57:18 +01:00
|
|
|
|
2022-09-19 12:17:14 +02:00
|
|
|
tasklist = TLIST_HEAD(dtcb, cpu);
|
2016-11-20 14:57:18 +01:00
|
|
|
#else
|
|
|
|
/* In the non-SMP case, we can be assured that the task to be terminated
|
|
|
|
* is not running. get the task list associated with the task state.
|
|
|
|
*/
|
|
|
|
|
2022-09-19 12:17:14 +02:00
|
|
|
tasklist = TLIST_HEAD(dtcb);
|
2016-02-11 15:06:33 +01:00
|
|
|
#endif
|
|
|
|
|
2016-11-20 14:57:18 +01:00
|
|
|
/* Remove the task from the task list */
|
|
|
|
|
|
|
|
dq_rem((FAR dq_entry_t *)dtcb, tasklist);
|
|
|
|
|
|
|
|
/* At this point, the TCB should no longer be accessible to the system */
|
|
|
|
|
|
|
|
#ifdef CONFIG_SMP
|
|
|
|
/* Resume the paused CPU (if any) */
|
2013-04-26 00:23:30 +02:00
|
|
|
|
2016-11-20 14:57:18 +01:00
|
|
|
if (cpu >= 0)
|
2013-04-26 00:23:30 +02:00
|
|
|
{
|
2016-11-20 14:57:18 +01:00
|
|
|
/* I am not yet sure how to handle a failure here. */
|
|
|
|
|
|
|
|
DEBUGVERIFY(up_cpu_resume(cpu));
|
2013-04-26 00:23:30 +02:00
|
|
|
}
|
2016-11-20 14:57:18 +01:00
|
|
|
#endif /* CONFIG_SMP */
|
|
|
|
|
|
|
|
leave_critical_section(flags);
|
2013-04-26 00:23:30 +02:00
|
|
|
|
|
|
|
/* Perform common task termination logic (flushing streams, calling
|
|
|
|
* functions registered by at_exit/on_exit, etc.). We need to do
|
|
|
|
* this as early as possible so that higher level clean-up logic
|
|
|
|
* can run in a healthy tasking environment.
|
|
|
|
*
|
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
|
|
|
* In the case where the task exits via exit(), nxtask_exithook()
|
2013-04-26 00:23:30 +02:00
|
|
|
* may be called twice.
|
|
|
|
*
|
|
|
|
* I suppose EXIT_SUCCESS is an appropriate return value???
|
|
|
|
*/
|
|
|
|
|
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_exithook(dtcb, EXIT_SUCCESS, nonblocking);
|
2013-04-26 00:23:30 +02:00
|
|
|
|
|
|
|
/* Since all tasks pass through this function as the final step in their
|
|
|
|
* exit sequence, this is an appropriate place to inform any instrumentation
|
|
|
|
* layer that the task no longer exists.
|
|
|
|
*/
|
|
|
|
|
|
|
|
sched_note_stop(dtcb);
|
|
|
|
|
|
|
|
/* Deallocate its TCB */
|
|
|
|
|
2020-05-09 16:04:45 +02:00
|
|
|
return nxsched_release_tcb(dtcb, dtcb->flags & TCB_FLAG_TTYPE_MASK);
|
2016-11-20 14:57:18 +01:00
|
|
|
|
|
|
|
errout_with_lock:
|
|
|
|
leave_critical_section(flags);
|
|
|
|
return ret;
|
2013-04-26 00:23:30 +02:00
|
|
|
}
|