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>
|
|
|
|
|
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 18:28:20 +01:00
|
|
|
* Name: group_remove
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Remove a group from the list of groups.
|
|
|
|
*
|
2018-03-13 16:52:27 +01:00
|
|
|
* Input Parameters:
|
2013-01-26 18:28:20 +01:00
|
|
|
* group - The group to be removed.
|
|
|
|
*
|
2018-02-01 17:00:02 +01:00
|
|
|
* Returned Value:
|
2013-01-26 18:28:20 +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 18:28:20 +01:00
|
|
|
|
2014-08-26 22:54:39 +02:00
|
|
|
#if defined(HAVE_GROUP_MEMBERS) || defined(CONFIG_ARCH_ADDRENV)
|
|
|
|
static void group_remove(FAR struct task_group_s *group)
|
2013-01-26 18:28:20 +01:00
|
|
|
{
|
|
|
|
FAR struct task_group_s *curr;
|
|
|
|
FAR struct task_group_s *prev;
|
|
|
|
irqstate_t flags;
|
|
|
|
|
|
|
|
/* Let's be especially careful while access the global task group list.
|
|
|
|
* This is probably un-necessary.
|
|
|
|
*/
|
2014-04-13 22:32:20 +02:00
|
|
|
|
2016-02-14 15:17:46 +01:00
|
|
|
flags = enter_critical_section();
|
2014-04-13 22:32:20 +02:00
|
|
|
|
2013-01-26 18:28:20 +01:00
|
|
|
/* Find the task group structure */
|
|
|
|
|
|
|
|
for (prev = NULL, curr = g_grouphead;
|
|
|
|
curr && curr != group;
|
|
|
|
prev = curr, curr = curr->flink);
|
|
|
|
|
|
|
|
/* Did we find it? If so, remove it from the list. */
|
|
|
|
|
|
|
|
if (curr)
|
|
|
|
{
|
|
|
|
/* Do we remove it from mid-list? Or from the head of the list? */
|
|
|
|
|
|
|
|
if (prev)
|
|
|
|
{
|
|
|
|
prev->flink = curr->flink;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
g_grouphead = curr->flink;
|
|
|
|
}
|
|
|
|
|
|
|
|
curr->flink = NULL;
|
|
|
|
}
|
|
|
|
|
2016-02-14 15:17:46 +01:00
|
|
|
leave_critical_section(flags);
|
2013-01-26 18:28:20 +01:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
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
|
|
|
|
2013-01-27 00:49:02 +01:00
|
|
|
static inline void group_release(FAR struct task_group_s *group)
|
2013-01-26 22:01:19 +01:00
|
|
|
{
|
2022-03-01 19:24:05 +01:00
|
|
|
#ifdef CONFIG_ARCH_ADDRENV
|
2022-05-06 11:58:38 +02:00
|
|
|
int i;
|
2022-03-01 19:24:05 +01:00
|
|
|
#endif
|
|
|
|
|
2021-07-20 12:59:09 +02:00
|
|
|
#if CONFIG_TLS_TASK_NELEM > 0
|
|
|
|
task_tls_destruct();
|
|
|
|
#endif
|
|
|
|
|
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);
|
|
|
|
|
2014-08-26 22:54:39 +02:00
|
|
|
#if defined(HAVE_GROUP_MEMBERS) || defined(CONFIG_ARCH_ADDRENV)
|
2013-01-26 22:01:19 +01:00
|
|
|
/* Remove the group from the list of groups */
|
|
|
|
|
|
|
|
group_remove(group);
|
2014-08-26 22:54:39 +02:00
|
|
|
#endif
|
2013-01-26 22:01:19 +01:00
|
|
|
|
2014-08-26 22:54:39 +02:00
|
|
|
#ifdef HAVE_GROUP_MEMBERS
|
2013-01-26 22:01:19 +01:00
|
|
|
/* Release the members array */
|
|
|
|
|
|
|
|
if (group->tg_members)
|
|
|
|
{
|
2020-04-07 16:31:47 +02:00
|
|
|
kmm_free(group->tg_members);
|
2013-01-26 22:01:19 +01:00
|
|
|
group->tg_members = NULL;
|
|
|
|
}
|
2013-01-26 23:25:21 +01:00
|
|
|
#endif
|
2013-01-26 22:01:19 +01:00
|
|
|
|
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
|
|
|
|
|
2022-03-01 19:24:05 +01:00
|
|
|
#ifdef CONFIG_ARCH_ADDRENV
|
|
|
|
/* Destroy the group address environment */
|
|
|
|
|
|
|
|
up_addrenv_destroy(&group->tg_addrenv);
|
|
|
|
|
|
|
|
/* Mark no address environment */
|
|
|
|
|
2022-05-06 11:58:38 +02:00
|
|
|
for (i = 0; i < CONFIG_SMP_NCPUS; i++)
|
|
|
|
{
|
|
|
|
if (group == g_group_current[i])
|
|
|
|
{
|
|
|
|
g_group_current[i] = NULL;
|
|
|
|
}
|
|
|
|
}
|
2022-03-01 19:24:05 +01:00
|
|
|
#endif
|
|
|
|
|
2015-12-22 18:48:17 +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)
|
|
|
|
{
|
|
|
|
group->tg_flags |= GROUP_FLAG_DELETED;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
#endif
|
|
|
|
{
|
|
|
|
/* Release the group container itself */
|
2013-01-26 22:01:19 +01:00
|
|
|
|
2022-03-01 19:24:05 +01:00
|
|
|
kmm_free(group);
|
2015-12-22 18:48:17 +01:00
|
|
|
}
|
2013-01-26 22:01:19 +01:00
|
|
|
}
|
|
|
|
|
2015-10-03 15:25:53 +02:00
|
|
|
/****************************************************************************
|
2013-02-05 20:50:37 +01:00
|
|
|
* Name: group_removemember
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Remove a member from a group.
|
|
|
|
*
|
2018-03-13 16:52:27 +01:00
|
|
|
* Input Parameters:
|
2013-02-05 20:50:37 +01:00
|
|
|
* group - The group from which to remove the member.
|
|
|
|
* pid - The member to be removed.
|
|
|
|
*
|
2018-02-01 17:00:02 +01:00
|
|
|
* Returned Value:
|
2013-02-05 20:50:37 +01:00
|
|
|
* On success, returns the number of members remaining in the group (>=0).
|
|
|
|
* Can fail only if the member is not found in the group. On failure,
|
|
|
|
* returns -ENOENT
|
|
|
|
*
|
|
|
|
* Assumptions:
|
|
|
|
* Called during task deletion and also from the reparenting logic, both
|
|
|
|
* in a safe context. No special precautions are required here.
|
|
|
|
*
|
2015-10-03 15:25:53 +02:00
|
|
|
****************************************************************************/
|
2013-02-05 20:50:37 +01:00
|
|
|
|
|
|
|
#ifdef HAVE_GROUP_MEMBERS
|
2020-04-09 14:46:24 +02:00
|
|
|
static inline void group_removemember(FAR struct task_group_s *group,
|
|
|
|
pid_t pid)
|
2013-02-05 20:50:37 +01:00
|
|
|
{
|
|
|
|
irqstate_t flags;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
DEBUGASSERT(group);
|
|
|
|
|
|
|
|
/* Find the member in the array of members and remove it */
|
|
|
|
|
|
|
|
for (i = 0; i < group->tg_nmembers; i++)
|
|
|
|
{
|
|
|
|
/* Does this member have the matching pid */
|
|
|
|
|
|
|
|
if (group->tg_members[i] == pid)
|
2015-10-08 03:59:14 +02:00
|
|
|
{
|
2013-02-06 16:43:28 +01:00
|
|
|
/* Remove the member from the array of members. This must be an
|
|
|
|
* atomic operation because the member array may be accessed from
|
|
|
|
* interrupt handlers (read-only).
|
2013-02-05 20:50:37 +01:00
|
|
|
*/
|
|
|
|
|
2016-02-14 15:17:46 +01:00
|
|
|
flags = enter_critical_section();
|
2013-02-06 16:43:28 +01:00
|
|
|
group->tg_members[i] = group->tg_members[group->tg_nmembers - 1];
|
|
|
|
group->tg_nmembers--;
|
2016-02-14 15:17:46 +01:00
|
|
|
leave_critical_section(flags);
|
2013-02-06 16:43:28 +01:00
|
|
|
}
|
2013-02-05 20:50:37 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif /* HAVE_GROUP_MEMBERS */
|
|
|
|
|
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-01-26 18:28:20 +01:00
|
|
|
#ifdef HAVE_GROUP_MEMBERS
|
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;
|
|
|
|
|
|
|
|
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)
|
|
|
|
{
|
2013-02-26 01:56:59 +01:00
|
|
|
/* Remove the member from group. This function may be called
|
|
|
|
* during certain error handling before the PID has been
|
|
|
|
* added to the group. In this case tcb->pid will be uninitialized
|
|
|
|
* group_removemember() will fail.
|
|
|
|
*/
|
2013-01-26 00:21:27 +01:00
|
|
|
|
2013-02-26 01:56:59 +01:00
|
|
|
group_removemember(group, tcb->pid);
|
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
|
|
|
|
2013-02-26 01:56:59 +01:00
|
|
|
if (group->tg_nmembers == 0)
|
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
|
|
|
|
2013-01-27 00:49:02 +01:00
|
|
|
group_release(group);
|
2013-01-25 20:15:05 +01:00
|
|
|
}
|
2013-01-26 18:28:20 +01:00
|
|
|
|
|
|
|
/* In any event, we can detach the group from the TCB so that we won't
|
|
|
|
* do this again.
|
|
|
|
*/
|
|
|
|
|
|
|
|
tcb->group = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#else /* HAVE_GROUP_MEMBERS */
|
|
|
|
|
2013-02-04 19:46:28 +01:00
|
|
|
void group_leave(FAR struct tcb_s *tcb)
|
2013-01-26 18:28:20 +01:00
|
|
|
{
|
|
|
|
FAR struct task_group_s *group;
|
|
|
|
|
|
|
|
DEBUGASSERT(tcb);
|
|
|
|
|
|
|
|
/* Make sure that we have a group */
|
|
|
|
|
|
|
|
group = tcb->group;
|
|
|
|
if (group)
|
|
|
|
{
|
|
|
|
/* Yes, we have a group.. Is this the last member of the group? */
|
2013-01-25 20:15:05 +01:00
|
|
|
|
|
|
|
if (group->tg_nmembers > 1)
|
|
|
|
{
|
|
|
|
/* No.. just decrement the number of members in the group */
|
|
|
|
|
|
|
|
group->tg_nmembers--;
|
2013-01-25 18:23:38 +01:00
|
|
|
}
|
2013-01-25 20:15:05 +01:00
|
|
|
|
|
|
|
/* Yes.. that was the last member remaining in the group */
|
|
|
|
|
2013-01-25 18:23:38 +01:00
|
|
|
else
|
|
|
|
{
|
2013-01-26 22:01:19 +01:00
|
|
|
/* Release all of the resource held by the task group */
|
2013-01-25 18:23:38 +01:00
|
|
|
|
2013-01-27 00:49:02 +01:00
|
|
|
group_release(group);
|
2013-01-25 18:23:38 +01:00
|
|
|
}
|
2013-01-25 20:15:05 +01:00
|
|
|
|
|
|
|
/* In any event, we can detach the group from the TCB so we won't do
|
|
|
|
* this again.
|
|
|
|
*/
|
2013-01-25 18:23:38 +01:00
|
|
|
|
|
|
|
tcb->group = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-01-26 18:28:20 +01:00
|
|
|
#endif /* HAVE_GROUP_MEMBERS */
|