2015-10-03 21:25:53 +08:00
|
|
|
/****************************************************************************
|
2021-03-09 05:39:04 +08:00
|
|
|
* sched/group/group_leave.c
|
2013-01-26 01:23:38 +08:00
|
|
|
*
|
2024-09-11 19:45:11 +08:00
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
*
|
2020-05-16 23:06:29 +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-01-26 01:23:38 +08:00
|
|
|
*
|
2020-05-16 23:06:29 +08:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2013-01-26 01:23:38 +08:00
|
|
|
*
|
2020-05-16 23:06:29 +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-01-26 01:23:38 +08:00
|
|
|
*
|
2015-10-03 21:25:53 +08:00
|
|
|
****************************************************************************/
|
2013-01-26 01:23:38 +08:00
|
|
|
|
2015-10-03 21:25:53 +08:00
|
|
|
/****************************************************************************
|
2013-01-26 01:23:38 +08:00
|
|
|
* Included Files
|
2015-10-03 21:25:53 +08:00
|
|
|
****************************************************************************/
|
2013-01-26 01:23:38 +08:00
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
|
|
|
|
|
|
|
#include <sched.h>
|
|
|
|
#include <assert.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <debug.h>
|
|
|
|
|
2024-03-04 09:19:27 +08:00
|
|
|
#include <nuttx/nuttx.h>
|
2016-02-14 22:17:46 +08:00
|
|
|
#include <nuttx/irq.h>
|
2013-01-27 07:49:02 +08:00
|
|
|
#include <nuttx/fs/fs.h>
|
|
|
|
#include <nuttx/net/net.h>
|
2022-12-14 15:37:22 +08:00
|
|
|
#include <nuttx/sched.h>
|
2024-07-03 15:10:49 +08:00
|
|
|
#include <nuttx/spinlock.h>
|
2013-01-27 07:49:02 +08:00
|
|
|
|
2018-08-05 22:09:54 +08:00
|
|
|
#ifdef CONFIG_BINFMT_LOADABLE
|
|
|
|
# include <nuttx/binfmt/binfmt.h>
|
|
|
|
#endif
|
|
|
|
|
2014-08-09 03:53:29 +08:00
|
|
|
#include "environ/environ.h"
|
2014-08-09 02:44:44 +08:00
|
|
|
#include "signal/signal.h"
|
2014-08-09 02:55:02 +08:00
|
|
|
#include "pthread/pthread.h"
|
2014-08-09 02:31:23 +08:00
|
|
|
#include "mqueue/mqueue.h"
|
2014-08-09 04:06:42 +08:00
|
|
|
#include "group/group.h"
|
2022-05-28 04:58:16 +08:00
|
|
|
#include "tls/tls.h"
|
2013-01-26 01:23:38 +08:00
|
|
|
|
2015-10-03 21:25:53 +08:00
|
|
|
/****************************************************************************
|
2013-01-26 01:23:38 +08:00
|
|
|
* Private Functions
|
2015-10-03 21:25:53 +08:00
|
|
|
****************************************************************************/
|
2013-01-26 01:23:38 +08:00
|
|
|
|
2015-10-03 21:25:53 +08:00
|
|
|
/****************************************************************************
|
2013-01-27 05:01:19 +08:00
|
|
|
* Name: group_release
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Release group resources after the last member has left the group.
|
|
|
|
*
|
2018-03-13 23:52:27 +08:00
|
|
|
* Input Parameters:
|
2013-01-27 05:01:19 +08:00
|
|
|
* group - The group to be removed.
|
|
|
|
*
|
2018-02-02 00:00:02 +08:00
|
|
|
* Returned Value:
|
2013-01-27 05:01:19 +08:00
|
|
|
* None.
|
|
|
|
*
|
|
|
|
* Assumptions:
|
|
|
|
* Called during task deletion in a safe context. No special precautions
|
|
|
|
* are required here.
|
|
|
|
*
|
2015-10-03 21:25:53 +08:00
|
|
|
****************************************************************************/
|
2013-01-27 05:01:19 +08:00
|
|
|
|
2024-03-04 09:19:27 +08:00
|
|
|
static inline void
|
|
|
|
group_release(FAR struct task_group_s *group, uint8_t ttype)
|
2013-01-27 05:01:19 +08:00
|
|
|
{
|
2022-05-28 04:58:16 +08:00
|
|
|
task_uninit_info(group);
|
2022-03-02 02:24:05 +08:00
|
|
|
|
2019-06-03 01:05:31 +08:00
|
|
|
#if defined(CONFIG_SCHED_HAVE_PARENT) && defined(CONFIG_SCHED_CHILD_STATUS)
|
2013-01-27 05:01:19 +08:00
|
|
|
/* Free all un-reaped child exit status */
|
|
|
|
|
2020-05-16 23:06:29 +08:00
|
|
|
group_remove_children(group);
|
2013-01-27 05:01:19 +08:00
|
|
|
#endif
|
|
|
|
|
2013-02-06 03:50:37 +08:00
|
|
|
/* Release pending signals */
|
|
|
|
|
This change renames all internal, private NuttX signal-related functions to use the prefix nxsig_ so that they cannot be confused with application interfaces that begin, primarily, with sig_
This is analogous to similar renaming that was done previously for semaphores.
Squashed commit of the following:
sched/signal: Fix a few compile warnings introduced by naming changes.
sched/signal: Rename all private, internal signl functions to use the nxsig_ prefix.
sched/signal: Rename sig_removependingsignal, sig_unmaskpendingsignal, and sig_mqnotempty to nxsig_remove_pendingsignal, nxsig_unmask_pendingsignal, and nxsig_mqnotempty to make it clear that these are OS internal interfaces.
sched/signal: Rename sig_findaction and sig_lowest to nxsig_find_action and nxsig_lowest to make it clear that these are OS internal interfaces.
sched/signal: Rename sig_allocatepingsigaction and sig_deliver to nxsig_alloc_pendingsigaction and nxsig_deliver to make it clear that these are OS internal interfaces.
sched/signal: Rename sig_cleanup, sig_release, sig_releasependingaction, and sig_releasependingsignal to nxsig_cleanup, nxsig_release, nxsig_release_pendingaction, and nxsig_release_pendingsignal to make it clear that these are OS internal interfaces.
sched/signal: Rename sig_tcbdispatch and sig_dispatch to nxsig_tcbdispatch and nxsig_dispatch to make it clear that these are OS internal interfaces.
sched/signal: Rename sig_releaseaction and sig_pendingset to nxsig_release_action and nxsig_pendingset to make it clear that these are OS internal interfaces.
sched/signal: Rename sig_initialize and sig_allocateactionblock to nxsig_initialize and nxsig_alloc_actionblock to make it clear that these are OS internal interfaces.
2017-10-06 03:25:25 +08:00
|
|
|
nxsig_release(group);
|
2013-02-06 03:50:37 +08:00
|
|
|
|
2013-02-04 00:43:58 +08:00
|
|
|
#ifndef CONFIG_DISABLE_PTHREAD
|
2013-02-04 01:39:54 +08:00
|
|
|
/* Release pthread resources */
|
|
|
|
|
|
|
|
pthread_release(group);
|
2013-02-04 00:43:58 +08:00
|
|
|
#endif
|
|
|
|
|
2013-01-27 05:01:19 +08:00
|
|
|
/* Free all file-related resources now. We really need to close files as
|
|
|
|
* soon as possible while we still have a functioning task.
|
|
|
|
*/
|
|
|
|
|
2013-01-27 07:49:02 +08:00
|
|
|
/* Free resources held by the file descriptor list */
|
|
|
|
|
2024-07-02 21:46:19 +08:00
|
|
|
files_putlist(&group->tg_filelist);
|
2013-01-27 07:49:02 +08:00
|
|
|
|
2014-08-23 04:49:05 +08:00
|
|
|
#ifndef CONFIG_DISABLE_ENVIRON
|
2013-01-27 05:01:19 +08:00
|
|
|
/* Release all shared environment variables */
|
|
|
|
|
2013-01-27 07:49:02 +08:00
|
|
|
env_release(group);
|
2013-01-27 05:01:19 +08:00
|
|
|
#endif
|
|
|
|
|
2022-04-07 20:17:21 +08:00
|
|
|
/* Destroy the mm_map list */
|
|
|
|
|
|
|
|
mm_map_destroy(&group->tg_mm_map);
|
|
|
|
|
2018-08-05 22:09:54 +08:00
|
|
|
#ifdef CONFIG_BINFMT_LOADABLE
|
|
|
|
/* If the exiting task was loaded into RAM from a file, then we need to
|
|
|
|
* lease all of the memory resource when the last thread exits the task
|
|
|
|
* group.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (group->tg_bininfo != NULL)
|
|
|
|
{
|
|
|
|
binfmt_exit(group->tg_bininfo);
|
|
|
|
group->tg_bininfo = NULL;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2023-01-26 20:45:13 +08:00
|
|
|
/* Then drop the group freeing the allocated memory */
|
|
|
|
|
2024-03-04 09:19:27 +08:00
|
|
|
#ifndef CONFIG_DISABLE_PTHREAD
|
|
|
|
if (ttype == TCB_FLAG_TTYPE_PTHREAD)
|
|
|
|
{
|
2024-03-11 10:36:20 +08:00
|
|
|
/* Mark the group as deleted now */
|
|
|
|
|
|
|
|
group->tg_flags |= GROUP_FLAG_DELETED;
|
|
|
|
|
2024-03-04 09:19:27 +08:00
|
|
|
group_drop(group);
|
|
|
|
}
|
|
|
|
#endif
|
2013-01-27 05:01:19 +08:00
|
|
|
}
|
|
|
|
|
2015-10-03 21:25:53 +08:00
|
|
|
/****************************************************************************
|
2013-01-26 01:23:38 +08:00
|
|
|
* Public Functions
|
2015-10-03 21:25:53 +08:00
|
|
|
****************************************************************************/
|
2013-01-26 01:23:38 +08:00
|
|
|
|
2015-10-03 21:25:53 +08:00
|
|
|
/****************************************************************************
|
2013-01-26 01:23:38 +08:00
|
|
|
* Name: group_leave
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Release a reference on a group. This function is called when a task or
|
|
|
|
* thread exits. It decrements the reference count on the group. If the
|
|
|
|
* reference count decrements to zero, then it frees the group and all of
|
|
|
|
* resources contained in the group.
|
|
|
|
*
|
2018-03-13 23:52:27 +08:00
|
|
|
* Input Parameters:
|
2013-01-26 01:23:38 +08:00
|
|
|
* tcb - The TCB of the task that is exiting.
|
|
|
|
*
|
2018-02-02 00:00:02 +08:00
|
|
|
* Returned Value:
|
2013-01-26 01:23:38 +08:00
|
|
|
* None.
|
|
|
|
*
|
|
|
|
* Assumptions:
|
|
|
|
* Called during task deletion in a safe context. No special precautions
|
|
|
|
* are required here.
|
|
|
|
*
|
2015-10-03 21:25:53 +08:00
|
|
|
****************************************************************************/
|
2013-01-26 01:23:38 +08:00
|
|
|
|
2013-02-05 02:46:28 +08:00
|
|
|
void group_leave(FAR struct tcb_s *tcb)
|
2013-01-26 01:23:38 +08:00
|
|
|
{
|
|
|
|
FAR struct task_group_s *group;
|
2024-03-06 10:13:47 +08:00
|
|
|
#ifdef HAVE_GROUP_MEMBERS
|
|
|
|
irqstate_t flags;
|
|
|
|
#endif
|
2013-01-26 01:23:38 +08:00
|
|
|
|
|
|
|
DEBUGASSERT(tcb);
|
|
|
|
|
2013-02-26 08:56:59 +08:00
|
|
|
/* Make sure that we have a group. */
|
2013-01-26 01:23:38 +08:00
|
|
|
|
|
|
|
group = tcb->group;
|
|
|
|
if (group)
|
|
|
|
{
|
2024-03-04 09:19:27 +08:00
|
|
|
/* In any event, we can detach the group from the TCB so that we won't
|
|
|
|
* do this again.
|
|
|
|
*/
|
|
|
|
|
|
|
|
tcb->group = NULL;
|
|
|
|
|
2024-03-06 10:13:47 +08:00
|
|
|
/* Remove the member from group. */
|
2013-01-26 07:21:27 +08:00
|
|
|
|
2024-03-06 10:13:47 +08:00
|
|
|
#ifdef HAVE_GROUP_MEMBERS
|
|
|
|
flags = spin_lock_irqsave(NULL);
|
|
|
|
sq_rem(&tcb->member, &group->tg_members);
|
|
|
|
spin_unlock_irqrestore(NULL, flags);
|
2013-01-26 01:23:38 +08:00
|
|
|
|
2013-02-26 08:56:59 +08:00
|
|
|
/* Have all of the members left the group? */
|
2013-01-26 03:15:05 +08:00
|
|
|
|
2024-03-06 10:13:47 +08:00
|
|
|
if (sq_empty(&group->tg_members))
|
|
|
|
#endif
|
2013-01-26 03:15:05 +08:00
|
|
|
{
|
2013-02-26 08:56:59 +08:00
|
|
|
/* Yes.. Release all of the resource held by the task group */
|
2013-01-27 01:28:20 +08:00
|
|
|
|
2024-03-04 09:19:27 +08:00
|
|
|
group_release(group, tcb->flags & TCB_FLAG_TTYPE_MASK);
|
2013-01-26 03:15:05 +08:00
|
|
|
}
|
2013-01-27 01:28:20 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-26 20:45:13 +08:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: group_drop
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Release the group's memory. This function is called whenever a reference
|
|
|
|
* to the group structure is released. It is not dependent on member count,
|
|
|
|
* but rather external references, which include:
|
|
|
|
* - Waiter list for waitpid()
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* group - The group that is to be dropped
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* None.
|
|
|
|
*
|
|
|
|
* Assumptions:
|
|
|
|
* Called during task deletion or context switch in a safe context. No
|
|
|
|
* special precautions are required here.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
void group_drop(FAR struct task_group_s *group)
|
|
|
|
{
|
2024-03-04 09:19:27 +08:00
|
|
|
FAR struct task_tcb_s *tcb;
|
|
|
|
|
2023-01-26 20:45:13 +08:00
|
|
|
#if defined(CONFIG_SCHED_WAITPID) && !defined(CONFIG_SCHED_HAVE_PARENT)
|
|
|
|
/* If there are threads waiting for this group to be freed, then we cannot
|
|
|
|
* yet free the memory resources. Instead just mark the group deleted
|
|
|
|
* and wait for those threads complete their waits.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (group->tg_nwaiters > 0)
|
|
|
|
{
|
|
|
|
/* Hold the group still */
|
|
|
|
|
|
|
|
sinfo("Keep group %p (waiters > 0)\n", group);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
#endif
|
|
|
|
/* Finally, if no one needs the group and it has been deleted, remove it */
|
|
|
|
|
|
|
|
if (group->tg_flags & GROUP_FLAG_DELETED)
|
|
|
|
{
|
2024-03-04 09:19:27 +08:00
|
|
|
tcb = container_of(group, struct task_tcb_s, group);
|
|
|
|
|
2023-01-26 20:45:13 +08:00
|
|
|
/* Release the group container itself */
|
|
|
|
|
2024-03-04 09:19:27 +08:00
|
|
|
if (tcb->cmn.flags & TCB_FLAG_FREE_TCB)
|
|
|
|
{
|
|
|
|
kmm_free(tcb);
|
|
|
|
}
|
2023-01-26 20:45:13 +08:00
|
|
|
}
|
|
|
|
}
|