2015-10-03 15:25:53 +02:00
|
|
|
/****************************************************************************
|
2021-03-08 22:39:04 +01:00
|
|
|
* sched/group/group_leave.c
|
2013-01-25 18:23:38 +01:00
|
|
|
*
|
2020-05-16 17:06: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
|
2013-01-25 18:23:38 +01:00
|
|
|
*
|
2020-05-16 17:06:29 +02:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2013-01-25 18:23:38 +01:00
|
|
|
*
|
2020-05-16 17:06: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.
|
2013-01-25 18:23:38 +01:00
|
|
|
*
|
2015-10-03 15:25:53 +02:00
|
|
|
****************************************************************************/
|
2013-01-25 18:23:38 +01:00
|
|
|
|
2015-10-03 15:25:53 +02:00
|
|
|
/****************************************************************************
|
2013-01-25 18:23:38 +01:00
|
|
|
* Included Files
|
2015-10-03 15:25:53 +02:00
|
|
|
****************************************************************************/
|
2013-01-25 18:23:38 +01:00
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
|
|
|
|
|
|
|
#include <sched.h>
|
|
|
|
#include <assert.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <debug.h>
|
|
|
|
|
2024-03-04 02:19:27 +01:00
|
|
|
#include <nuttx/nuttx.h>
|
2016-02-14 15:17:46 +01:00
|
|
|
#include <nuttx/irq.h>
|
2013-01-27 00:49:02 +01:00
|
|
|
#include <nuttx/fs/fs.h>
|
|
|
|
#include <nuttx/net/net.h>
|
2022-12-14 08:37:22 +01:00
|
|
|
#include <nuttx/sched.h>
|
2013-01-27 00:49:02 +01:00
|
|
|
|
2018-08-05 16:09:54 +02:00
|
|
|
#ifdef CONFIG_BINFMT_LOADABLE
|
|
|
|
# include <nuttx/binfmt/binfmt.h>
|
|
|
|
#endif
|
|
|
|
|
2014-08-08 21:53:29 +02:00
|
|
|
#include "environ/environ.h"
|
2014-08-08 20:44:44 +02:00
|
|
|
#include "signal/signal.h"
|
2014-08-08 20:55:02 +02:00
|
|
|
#include "pthread/pthread.h"
|
2014-08-08 20:31:23 +02:00
|
|
|
#include "mqueue/mqueue.h"
|
2014-08-08 22:06:42 +02:00
|
|
|
#include "group/group.h"
|
2022-05-27 22:58:16 +02:00
|
|
|
#include "tls/tls.h"
|
2013-01-25 18:23:38 +01:00
|
|
|
|
2015-10-03 15:25:53 +02:00
|
|
|
/****************************************************************************
|
2013-01-25 18:23:38 +01:00
|
|
|
* Private Functions
|
2015-10-03 15:25:53 +02:00
|
|
|
****************************************************************************/
|
2013-01-25 18:23:38 +01:00
|
|
|
|
2015-10-03 15:25:53 +02:00
|
|
|
/****************************************************************************
|
2013-01-26 22:01:19 +01:00
|
|
|
* Name: group_release
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Release group resources after the last member has left the group.
|
|
|
|
*
|
2018-03-13 16:52:27 +01:00
|
|
|
* Input Parameters:
|
2013-01-26 22:01:19 +01:00
|
|
|
* group - The group to be removed.
|
|
|
|
*
|
2018-02-01 17:00:02 +01:00
|
|
|
* Returned Value:
|
2013-01-26 22:01:19 +01:00
|
|
|
* None.
|
|
|
|
*
|
|
|
|
* Assumptions:
|
|
|
|
* Called during task deletion in a safe context. No special precautions
|
|
|
|
* are required here.
|
|
|
|
*
|
2015-10-03 15:25:53 +02:00
|
|
|
****************************************************************************/
|
2013-01-26 22:01:19 +01:00
|
|
|
|
2024-03-04 02:19:27 +01:00
|
|
|
static inline void
|
|
|
|
group_release(FAR struct task_group_s *group, uint8_t ttype)
|
2013-01-26 22:01:19 +01:00
|
|
|
{
|
2022-05-27 22:58:16 +02:00
|
|
|
task_uninit_info(group);
|
2022-03-01 19:24:05 +01:00
|
|
|
|
2019-06-02 19:05:31 +02:00
|
|
|
#if defined(CONFIG_SCHED_HAVE_PARENT) && defined(CONFIG_SCHED_CHILD_STATUS)
|
2013-01-26 22:01:19 +01:00
|
|
|
/* Free all un-reaped child exit status */
|
|
|
|
|
2020-05-16 17:06:29 +02:00
|
|
|
group_remove_children(group);
|
2013-01-26 22:01:19 +01:00
|
|
|
#endif
|
|
|
|
|
2013-02-05 20:50:37 +01: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-05 21:25:25 +02:00
|
|
|
nxsig_release(group);
|
2013-02-05 20:50:37 +01:00
|
|
|
|
2013-02-03 17:43:58 +01:00
|
|
|
#ifndef CONFIG_DISABLE_PTHREAD
|
2013-02-03 18:39:54 +01:00
|
|
|
/* Release pthread resources */
|
|
|
|
|
|
|
|
pthread_release(group);
|
2013-02-03 17:43:58 +01:00
|
|
|
#endif
|
|
|
|
|
2013-01-26 22:01:19 +01: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 00:49:02 +01:00
|
|
|
/* Free resources held by the file descriptor list */
|
|
|
|
|
|
|
|
files_releaselist(&group->tg_filelist);
|
|
|
|
|
2014-08-22 22:49:05 +02:00
|
|
|
#ifndef CONFIG_DISABLE_ENVIRON
|
2013-01-26 22:01:19 +01:00
|
|
|
/* Release all shared environment variables */
|
|
|
|
|
2013-01-27 00:49:02 +01:00
|
|
|
env_release(group);
|
2013-01-26 22:01:19 +01:00
|
|
|
#endif
|
|
|
|
|
2022-04-07 14:17:21 +02:00
|
|
|
/* Destroy the mm_map list */
|
|
|
|
|
|
|
|
mm_map_destroy(&group->tg_mm_map);
|
|
|
|
|
2018-08-05 16:09:54 +02: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 13:45:13 +01:00
|
|
|
/* Then drop the group freeing the allocated memory */
|
|
|
|
|
2024-03-04 02:19:27 +01:00
|
|
|
#ifndef CONFIG_DISABLE_PTHREAD
|
|
|
|
if (ttype == TCB_FLAG_TTYPE_PTHREAD)
|
|
|
|
{
|
2024-03-11 03:36:20 +01:00
|
|
|
/* Mark the group as deleted now */
|
|
|
|
|
|
|
|
group->tg_flags |= GROUP_FLAG_DELETED;
|
|
|
|
|
2024-03-04 02:19:27 +01:00
|
|
|
group_drop(group);
|
|
|
|
}
|
|
|
|
#endif
|
2013-01-26 22:01:19 +01:00
|
|
|
}
|
|
|
|
|
2015-10-03 15:25:53 +02:00
|
|
|
/****************************************************************************
|
2013-01-25 18:23:38 +01:00
|
|
|
* Public Functions
|
2015-10-03 15:25:53 +02:00
|
|
|
****************************************************************************/
|
2013-01-25 18:23:38 +01:00
|
|
|
|
2015-10-03 15:25:53 +02:00
|
|
|
/****************************************************************************
|
2013-01-25 18:23:38 +01: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 16:52:27 +01:00
|
|
|
* Input Parameters:
|
2013-01-25 18:23:38 +01:00
|
|
|
* tcb - The TCB of the task that is exiting.
|
|
|
|
*
|
2018-02-01 17:00:02 +01:00
|
|
|
* Returned Value:
|
2013-01-25 18:23:38 +01:00
|
|
|
* None.
|
|
|
|
*
|
|
|
|
* Assumptions:
|
|
|
|
* Called during task deletion in a safe context. No special precautions
|
|
|
|
* are required here.
|
|
|
|
*
|
2015-10-03 15:25:53 +02:00
|
|
|
****************************************************************************/
|
2013-01-25 18:23:38 +01:00
|
|
|
|
2013-02-04 19:46:28 +01:00
|
|
|
void group_leave(FAR struct tcb_s *tcb)
|
2013-01-25 18:23:38 +01:00
|
|
|
{
|
|
|
|
FAR struct task_group_s *group;
|
2024-03-06 03:13:47 +01:00
|
|
|
#ifdef HAVE_GROUP_MEMBERS
|
|
|
|
irqstate_t flags;
|
|
|
|
#endif
|
2013-01-25 18:23:38 +01:00
|
|
|
|
|
|
|
DEBUGASSERT(tcb);
|
|
|
|
|
2013-02-26 01:56:59 +01:00
|
|
|
/* Make sure that we have a group. */
|
2013-01-25 18:23:38 +01:00
|
|
|
|
|
|
|
group = tcb->group;
|
|
|
|
if (group)
|
|
|
|
{
|
2024-03-04 02:19:27 +01: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 03:13:47 +01:00
|
|
|
/* Remove the member from group. */
|
2013-01-26 00:21:27 +01:00
|
|
|
|
2024-03-06 03:13:47 +01:00
|
|
|
#ifdef HAVE_GROUP_MEMBERS
|
|
|
|
flags = spin_lock_irqsave(NULL);
|
|
|
|
sq_rem(&tcb->member, &group->tg_members);
|
|
|
|
spin_unlock_irqrestore(NULL, flags);
|
2013-01-25 18:23:38 +01:00
|
|
|
|
2013-02-26 01:56:59 +01:00
|
|
|
/* Have all of the members left the group? */
|
2013-01-25 20:15:05 +01:00
|
|
|
|
2024-03-06 03:13:47 +01:00
|
|
|
if (sq_empty(&group->tg_members))
|
|
|
|
#endif
|
2013-01-25 20:15:05 +01:00
|
|
|
{
|
2013-02-26 01:56:59 +01:00
|
|
|
/* Yes.. Release all of the resource held by the task group */
|
2013-01-26 18:28:20 +01:00
|
|
|
|
2024-03-04 02:19:27 +01:00
|
|
|
group_release(group, tcb->flags & TCB_FLAG_TTYPE_MASK);
|
2013-01-25 20:15:05 +01:00
|
|
|
}
|
2013-01-26 18:28:20 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-26 13:45:13 +01: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 02:19:27 +01:00
|
|
|
FAR struct task_tcb_s *tcb;
|
|
|
|
|
2023-01-26 13:45:13 +01: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 02:19:27 +01:00
|
|
|
tcb = container_of(group, struct task_tcb_s, group);
|
|
|
|
|
2023-01-26 13:45:13 +01:00
|
|
|
/* Release the group container itself */
|
|
|
|
|
2024-03-04 02:19:27 +01:00
|
|
|
if (tcb->cmn.flags & TCB_FLAG_FREE_TCB)
|
|
|
|
{
|
|
|
|
kmm_free(tcb);
|
|
|
|
}
|
2023-01-26 13:45:13 +01:00
|
|
|
}
|
|
|
|
}
|