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
|
2022-12-14 08:37:22 +01:00
|
|
|
* sequences. nxtask_terminate() is called only from task_delete() and
|
|
|
|
* from nxtask_exit().
|
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
|
2022-12-14 08:37:22 +01:00
|
|
|
* non-running. nxtask_exit then calls nxtask_terminate() to terminate
|
|
|
|
* the non-running task.
|
2013-04-26 00:23:30 +02:00
|
|
|
*
|
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.
|
|
|
|
*
|
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
|
|
|
|
2022-12-14 08:37:22 +01:00
|
|
|
int nxtask_terminate(pid_t pid)
|
2013-04-26 00:23:30 +02:00
|
|
|
{
|
|
|
|
FAR struct tcb_s *dtcb;
|
2023-01-16 11:20:22 +01:00
|
|
|
uint8_t task_state;
|
2016-02-14 15:17:46 +01:00
|
|
|
irqstate_t flags;
|
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)
|
|
|
|
{
|
2023-01-16 11:20:22 +01:00
|
|
|
leave_critical_section(flags);
|
|
|
|
return -ESRCH;
|
2013-04-26 00:23:30 +02:00
|
|
|
}
|
|
|
|
|
2023-01-16 11:20:22 +01:00
|
|
|
/* Remove dtcb from tasklist, let remove_readtorun() do the job */
|
2016-11-20 14:57:18 +01:00
|
|
|
|
2023-01-16 11:20:22 +01:00
|
|
|
task_state = dtcb->task_state;
|
|
|
|
nxsched_remove_readytorun(dtcb, false);
|
|
|
|
dtcb->task_state = task_state;
|
2016-11-20 14:57:18 +01:00
|
|
|
|
|
|
|
leave_critical_section(flags);
|
2013-04-26 00:23:30 +02:00
|
|
|
|
2022-12-14 08:37:22 +01:00
|
|
|
/* Perform common task termination logic. We need to do
|
2013-04-26 00:23:30 +02: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 08:37:22 +01:00
|
|
|
nxtask_exithook(dtcb, EXIT_SUCCESS);
|
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);
|
2013-04-26 00:23:30 +02:00
|
|
|
}
|