2023-05-11 16:36:08 +08:00
|
|
|
/****************************************************************************
|
2014-08-09 06:44:08 +08:00
|
|
|
* sched/task/task_terminate.c
|
2013-04-26 06:23:30 +08:00
|
|
|
*
|
2024-09-11 19:45:11 +08:00
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
*
|
2021-02-08 23:33:58 +08: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 06:23:30 +08:00
|
|
|
*
|
2021-02-08 23:33:58 +08:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2013-04-26 06:23:30 +08:00
|
|
|
*
|
2021-02-08 23:33:58 +08: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 06:23:30 +08:00
|
|
|
*
|
2023-05-11 16:36:08 +08:00
|
|
|
****************************************************************************/
|
2013-04-26 06:23:30 +08:00
|
|
|
|
2023-05-11 16:36:08 +08:00
|
|
|
/****************************************************************************
|
2013-04-26 06:23:30 +08:00
|
|
|
* Included Files
|
2023-05-11 16:36:08 +08:00
|
|
|
****************************************************************************/
|
2013-04-26 06:23:30 +08:00
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
|
|
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <assert.h>
|
2014-02-19 01:50:32 +08:00
|
|
|
#include <errno.h>
|
2013-04-26 06:23:30 +08:00
|
|
|
|
2022-09-25 23:08:38 +08:00
|
|
|
#include <nuttx/queue.h>
|
2013-04-26 06:23:30 +08:00
|
|
|
#include <nuttx/sched.h>
|
2016-02-14 22:17:46 +08:00
|
|
|
#include <nuttx/irq.h>
|
2016-03-17 23:49:43 +08:00
|
|
|
#include <nuttx/sched_note.h>
|
2013-04-26 06:23:30 +08:00
|
|
|
|
2014-08-09 07:53:55 +08:00
|
|
|
#include "sched/sched.h"
|
2019-04-30 04:52:05 +08:00
|
|
|
#include "signal/signal.h"
|
2014-08-09 06:44:08 +08:00
|
|
|
#include "task/task.h"
|
2013-04-26 06:23:30 +08:00
|
|
|
|
2023-05-11 16:36:08 +08:00
|
|
|
/****************************************************************************
|
2013-04-26 06:23:30 +08:00
|
|
|
* Public Functions
|
2023-05-11 16:36:08 +08:00
|
|
|
****************************************************************************/
|
2013-04-26 06:23:30 +08:00
|
|
|
|
2023-05-11 16:36:08 +08: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-05 03:42:51 +08:00
|
|
|
* Name: nxtask_terminate
|
2013-04-26 06:23:30 +08:00
|
|
|
*
|
|
|
|
* Description:
|
2019-08-01 22:09:50 +08:00
|
|
|
* This function causes a specified task to cease to exist. Its stack and
|
2013-04-26 06:23:30 +08: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
|
2022-12-14 15:37:22 +08:00
|
|
|
* sequences. nxtask_terminate() is called only from task_delete() and
|
|
|
|
* from nxtask_exit().
|
2013-04-26 06:23:30 +08: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-05 03:42:51 +08:00
|
|
|
* The path through nxtask_exit() supports the final stops of the exit(),
|
2013-04-26 06:23:30 +08: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-05 03:42:51 +08:00
|
|
|
* - _exit(). Calls nxtask_exit() making the currently running task
|
2022-12-14 15:37:22 +08:00
|
|
|
* non-running. nxtask_exit then calls nxtask_terminate() to terminate
|
|
|
|
* the non-running task.
|
2013-04-26 06:23:30 +08:00
|
|
|
*
|
2018-02-02 00:00:02 +08:00
|
|
|
* Input Parameters:
|
2013-04-26 06:23:30 +08:00
|
|
|
* pid - The task ID of the task to delete. A pid of zero
|
|
|
|
* signifies the calling task.
|
|
|
|
*
|
2018-02-02 00:00:02 +08:00
|
|
|
* Returned Value:
|
2013-04-26 06:23:30 +08: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)
|
|
|
|
*
|
2023-05-11 16:36:08 +08:00
|
|
|
****************************************************************************/
|
2013-04-26 06:23:30 +08:00
|
|
|
|
2022-12-14 15:37:22 +08:00
|
|
|
int nxtask_terminate(pid_t pid)
|
2013-04-26 06:23:30 +08:00
|
|
|
{
|
|
|
|
FAR struct tcb_s *dtcb;
|
2023-01-16 18:20:22 +08:00
|
|
|
uint8_t task_state;
|
2016-02-14 22:17:46 +08:00
|
|
|
irqstate_t flags;
|
2013-04-26 06:23:30 +08:00
|
|
|
|
2016-11-20 21:57:18 +08:00
|
|
|
flags = enter_critical_section();
|
2013-04-26 06:23:30 +08:00
|
|
|
|
2014-02-19 01:50:32 +08:00
|
|
|
/* Find for the TCB associated with matching PID */
|
2013-04-26 06:23:30 +08:00
|
|
|
|
2020-05-09 22:04:45 +08:00
|
|
|
dtcb = nxsched_get_tcb(pid);
|
2013-04-26 06:23:30 +08:00
|
|
|
if (!dtcb)
|
|
|
|
{
|
2023-01-16 18:20:22 +08:00
|
|
|
leave_critical_section(flags);
|
|
|
|
return -ESRCH;
|
2013-04-26 06:23:30 +08:00
|
|
|
}
|
|
|
|
|
2023-01-16 18:20:22 +08:00
|
|
|
/* Remove dtcb from tasklist, let remove_readtorun() do the job */
|
2016-11-20 21:57:18 +08:00
|
|
|
|
2023-01-16 18:20:22 +08:00
|
|
|
task_state = dtcb->task_state;
|
|
|
|
nxsched_remove_readytorun(dtcb, false);
|
|
|
|
dtcb->task_state = task_state;
|
2016-11-20 21:57:18 +08:00
|
|
|
|
|
|
|
leave_critical_section(flags);
|
2013-04-26 06:23:30 +08:00
|
|
|
|
2022-12-14 15:37:22 +08:00
|
|
|
/* Perform common task termination logic. We need to do
|
2013-04-26 06:23:30 +08:00
|
|
|
* this as early as possible so that higher level clean-up logic
|
|
|
|
* can run in a healthy tasking environment.
|
|
|
|
*
|
|
|
|
* I suppose EXIT_SUCCESS is an appropriate return value???
|
|
|
|
*/
|
|
|
|
|
2022-12-14 15:37:22 +08:00
|
|
|
nxtask_exithook(dtcb, EXIT_SUCCESS);
|
2013-04-26 06:23:30 +08:00
|
|
|
|
|
|
|
/* Since all tasks pass through this function as the final step in their
|
2023-05-11 16:36:08 +08:00
|
|
|
* exit sequence, this is an appropriate place to inform any
|
|
|
|
* instrumentation layer that the task no longer exists.
|
2013-04-26 06:23:30 +08:00
|
|
|
*/
|
|
|
|
|
|
|
|
sched_note_stop(dtcb);
|
|
|
|
|
|
|
|
/* Deallocate its TCB */
|
|
|
|
|
2020-05-09 22:04:45 +08:00
|
|
|
return nxsched_release_tcb(dtcb, dtcb->flags & TCB_FLAG_TTYPE_MASK);
|
2013-04-26 06:23:30 +08:00
|
|
|
}
|