nuttx/sched/group/group_foreachchild.c
wangzhi16 31e562b10f Revert "sched/group: move task group into task_tcb_s to improve performance"
This reverts commit 29e50ffa73.

reason:
Placing the main thread and the gourd in the same memory block, and allocating and freeing memory simultaneously, presents the following two problems:

When the main thread creates a child thread and performs a detach operation, there is a possibility that the main thread may have exited, but the main thread's TCB (Transaction Control Block) may not have been released.

This could potentially cause the main thread's TCB to be double-freed. The core contradiction in this problem lies in binding the main thread's TCB (Trust Container Registry) and the group together. When releasing the main thread's TCB, an additional check is needed to ensure the main thread was the last to leave the group. If this check and the free operation are atomically guaranteed, the logic is sound, and double freeing won't occur. However, this atomicity cannot be completely guaranteed. If other free operations cause a block, problems still arise.

Signed-off-by: wangzhi16 <wangzhi16@xiaomi.com>
2025-12-21 08:53:46 -03:00

88 lines
2.7 KiB
C

/****************************************************************************
* sched/group/group_foreachchild.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 <assert.h>
#include <nuttx/nuttx.h>
#include <nuttx/sched.h>
#include <nuttx/queue.h>
#include "group/group.h"
#ifdef HAVE_GROUP_MEMBERS
/****************************************************************************
* Public Functions
****************************************************************************/
/****************************************************************************
* Name: group_foreachchild
*
* Description:
* Execute a function for each child of a group.
*
* Input Parameters:
* group - The group containing the children
* handler - The function to be called
* arg - An additional argument to provide to the handler
*
* Returned Value:
* Success (OK) is always returned unless the handler returns a non-zero
* value (a negated errno on errors). In that case, the traversal
* terminates and that non-zero value is returned.
*
* Assumptions:
*
****************************************************************************/
int group_foreachchild(FAR struct task_group_s *group,
foreachchild_t handler, FAR void *arg)
{
FAR sq_entry_t *curr;
FAR sq_entry_t *next;
int ret;
DEBUGASSERT(group);
/* Visit the main thread last (if present) */
sq_for_every_safe(&group->tg_members, curr, next)
{
FAR struct tcb_s *mtcb =
container_of(curr, struct tcb_s, member);
ret = handler(mtcb->pid, arg);
if (ret != OK)
{
break;
}
}
return ret;
}
#endif /* HAVE_GROUP_MEMBERS */