* sched/group/group_leave.c
*
* SPDX-License-Identifier: Apache-2.0
*
* 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
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* 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.
*
****************************************************************************/
* Included Files
****************************************************************************/
#include <nuttx/config.h>
#include <sched.h>
#include <assert.h>
#include <errno.h>
#include <debug.h>
#include <nuttx/nuttx.h>
#include <nuttx/irq.h>
#include <nuttx/fs/fs.h>
#include <nuttx/net/net.h>
#include <nuttx/sched.h>
#include <nuttx/spinlock.h>
#include <nuttx/mutex.h>
#include <nuttx/environ.h>
#ifdef CONFIG_BINFMT_LOADABLE
# include <nuttx/binfmt/binfmt.h>
#endif
#include "signal/signal.h"
#include "pthread/pthread.h"
#include "mqueue/mqueue.h"
#include "group/group.h"
#include "task/task.h"
#include "tls/tls.h"
* Private Functions
****************************************************************************/
* Name: group_release
*
* Description:
* Release group resources after the last member has left the group.
*
* Input Parameters:
* group - The group to be removed.
*
* Returned Value:
* None.
*
* Assumptions:
* Called during task deletion in a safe context. No special precautions
* are required here.
*
****************************************************************************/
static inline void
group_release(FAR struct task_group_s *group, int ttype)
{
nxrmutex_destroy(&group->tg_mutex);
#if defined(CONFIG_SCHED_HAVE_PARENT) && defined(CONFIG_SCHED_CHILD_STATUS)
group_remove_children(group);
#endif
nxsig_release(group);
#ifndef CONFIG_DISABLE_PTHREAD
pthread_release(group);
#endif
mm_map_destroy(&group->tg_mm_map);
* soon as possible while we still have a functioning task.
*/
fdlist_free(&group->tg_fdlist);
#ifdef CONFIG_BINFMT_LOADABLE
* 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
task_uninit_info(group);
#ifndef CONFIG_DISABLE_PTHREAD
if (ttype == TCB_FLAG_TTYPE_PTHREAD)
{
atomic_or(&group->tg_flags, GROUP_FLAG_DELETED);
group_drop(group);
}
#endif
}
* Public Functions
****************************************************************************/
* 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.
*
* Input Parameters:
* tcb - The TCB of the task that is exiting.
*
* Returned Value:
* None.
*
* Assumptions:
* Called during task deletion in a safe context. No special precautions
* are required here.
*
****************************************************************************/
void group_leave(FAR struct tcb_s *tcb)
{
FAR struct task_group_s *group;
#ifdef HAVE_GROUP_MEMBERS
irqstate_t flags;
#endif
DEBUGASSERT(tcb);
group = tcb->group;
if (group)
{
#ifdef HAVE_GROUP_MEMBERS
flags = spin_lock_irqsave(&group->tg_lock);
sq_rem(&tcb->member, &group->tg_members);
spin_unlock_irqrestore(&group->tg_lock, flags);
if (sq_empty(&group->tg_members))
#endif
{
group_release(group,
atomic_read(&tcb->flags) & TCB_FLAG_TTYPE_MASK);
}
}
}
* 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)
{
FAR struct tcb_s *tcb;
#if defined(CONFIG_SCHED_WAITPID) && !defined(CONFIG_SCHED_HAVE_PARENT)
* yet free the memory resources. Instead just mark the group deleted
* and wait for those threads complete their waits.
*/
if (group->tg_nwaiters > 0)
{
sinfo("Keep group %p (waiters > 0)\n", group);
}
else
#endif
if (atomic_read(&group->tg_flags) & GROUP_FLAG_DELETED)
{
tcb = (FAR struct tcb_s *)
((uintptr_t)group - sizeof(struct tcb_s));
if (atomic_read(&tcb->flags) & TCB_FLAG_FREE_TCB)
{
if ((atomic_read(&tcb->flags) & TCB_FLAG_TTYPE_MASK) ==
TCB_FLAG_TTYPE_TASK)
{
#if !defined(CONFIG_BUILD_KERNEL) && !defined(CONFIG_ARCH_ADDRENV)
* If use addrenv, it will uninitialized in up_addrenv_destroy.
*/
group_heap_uninitialize(group->tg_heap);
#endif
}
kmm_free(tcb);
}
}
}