2015-10-03 21:25:53 +08:00
|
|
|
/****************************************************************************
|
2021-03-09 05:39:04 +08:00
|
|
|
* sched/group/group_join.c
|
2013-01-26 01:23:38 +08:00
|
|
|
*
|
2021-02-08 23:33:58 +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
|
|
|
*
|
2021-02-08 23:33:58 +08:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2013-01-26 01:23:38 +08:00
|
|
|
*
|
2021-02-08 23:33:58 +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>
|
2013-01-26 03:15:05 +08:00
|
|
|
#include <errno.h>
|
2013-01-26 01:23:38 +08:00
|
|
|
#include <debug.h>
|
|
|
|
|
2016-02-14 22:17:46 +08:00
|
|
|
#include <nuttx/irq.h>
|
2013-01-26 03:15:05 +08:00
|
|
|
#include <nuttx/kmalloc.h>
|
|
|
|
|
2014-08-09 08:39:28 +08:00
|
|
|
#include "sched/sched.h"
|
2014-08-09 04:06:42 +08:00
|
|
|
#include "group/group.h"
|
2014-08-09 03:53:29 +08:00
|
|
|
#include "environ/environ.h"
|
2013-01-26 01:23:38 +08:00
|
|
|
|
2019-02-12 02:09:26 +08:00
|
|
|
#ifndef CONFIG_DISABLE_PTHREAD
|
2013-01-26 01:23:38 +08:00
|
|
|
|
2015-10-03 21:25:53 +08:00
|
|
|
/****************************************************************************
|
2013-01-26 01:23:38 +08:00
|
|
|
* Pre-processor Definitions
|
2015-10-03 21:25:53 +08:00
|
|
|
****************************************************************************/
|
2019-03-02 00:50:02 +08:00
|
|
|
|
2013-01-26 03:15:05 +08:00
|
|
|
/* Is this worth making a configuration option? */
|
|
|
|
|
|
|
|
#define GROUP_REALLOC_MEMBERS 4
|
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-02-06 03:50:37 +08:00
|
|
|
* Name: group_addmember
|
|
|
|
*
|
|
|
|
* Description:
|
2014-04-14 04:32:20 +08:00
|
|
|
* Add a new member to a group.
|
2013-02-06 03:50:37 +08:00
|
|
|
*
|
2018-03-13 23:52:27 +08:00
|
|
|
* Input Parameters:
|
2013-02-06 03:50:37 +08:00
|
|
|
* group - The task group to add the new member
|
|
|
|
* pid - The new member
|
|
|
|
*
|
2018-02-02 00:00:02 +08:00
|
|
|
* Returned Value:
|
2013-02-06 03:50:37 +08:00
|
|
|
* 0 (OK) on success; a negated errno value on failure.
|
|
|
|
*
|
|
|
|
* Assumptions:
|
|
|
|
* Called during thread creation and during reparenting in a safe context.
|
|
|
|
* No special precautions are required here.
|
|
|
|
*
|
2015-10-03 21:25:53 +08:00
|
|
|
****************************************************************************/
|
2013-02-06 03:50:37 +08:00
|
|
|
|
|
|
|
#ifdef HAVE_GROUP_MEMBERS
|
|
|
|
static inline int group_addmember(FAR struct task_group_s *group, pid_t pid)
|
|
|
|
{
|
|
|
|
irqstate_t flags;
|
|
|
|
|
|
|
|
DEBUGASSERT(group && group->tg_nmembers < UINT8_MAX);
|
|
|
|
|
|
|
|
/* Will we need to extend the size of the array of groups? */
|
|
|
|
|
|
|
|
if (group->tg_nmembers >= group->tg_mxmembers)
|
|
|
|
{
|
|
|
|
FAR pid_t *newmembers;
|
|
|
|
unsigned int newmax;
|
|
|
|
|
|
|
|
/* Yes... reallocate the array of members */
|
|
|
|
|
|
|
|
newmax = group->tg_mxmembers + GROUP_REALLOC_MEMBERS;
|
|
|
|
if (newmax > UINT8_MAX)
|
|
|
|
{
|
|
|
|
newmax = UINT8_MAX;
|
|
|
|
}
|
|
|
|
|
|
|
|
newmembers = (FAR pid_t *)
|
2014-09-01 05:27:37 +08:00
|
|
|
kmm_realloc(group->tg_members, sizeof(pid_t) * newmax);
|
2013-02-06 03:50:37 +08:00
|
|
|
|
|
|
|
if (!newmembers)
|
|
|
|
{
|
2016-06-12 05:50:49 +08:00
|
|
|
serr("ERROR: Failed to reallocate tg_members\n");
|
2013-02-06 03:50:37 +08:00
|
|
|
return -ENOMEM;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Save the new number of members in the reallocated members array.
|
|
|
|
* We need to make the following atomic because the member list
|
|
|
|
* may be traversed from an interrupt handler (read-only).
|
|
|
|
*/
|
|
|
|
|
2016-02-14 22:17:46 +08:00
|
|
|
flags = enter_critical_section();
|
2013-02-06 03:50:37 +08:00
|
|
|
group->tg_members = newmembers;
|
|
|
|
group->tg_mxmembers = newmax;
|
2016-02-14 22:17:46 +08:00
|
|
|
leave_critical_section(flags);
|
2013-02-06 03:50:37 +08:00
|
|
|
}
|
|
|
|
|
2014-04-14 04:32:20 +08:00
|
|
|
/* Assign this new pid to the group; group->tg_nmembers will be incremented
|
2013-02-06 03:50:37 +08:00
|
|
|
* by the caller.
|
|
|
|
*/
|
|
|
|
|
|
|
|
group->tg_members[group->tg_nmembers] = pid;
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
#endif /* HAVE_GROUP_MEMBERS */
|
|
|
|
|
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 03:15:05 +08:00
|
|
|
* Name: group_bind
|
2013-01-26 01:23:38 +08:00
|
|
|
*
|
|
|
|
* Description:
|
2021-02-09 01:46:31 +08:00
|
|
|
* A thread joins the group when it is created. This is a two step process,
|
2013-01-26 03:15:05 +08:00
|
|
|
* first, the group must bound to the new threads TCB. group_bind() does
|
|
|
|
* this (at the return from group_join, things are a little unstable: The
|
2014-08-28 04:22:00 +08:00
|
|
|
* group has been bound, but tg_nmembers has not yet been incremented).
|
2013-01-26 03:15:05 +08:00
|
|
|
* Then, after the new thread is initialized and has a PID assigned to it,
|
2014-04-14 04:32:20 +08:00
|
|
|
* group_join() is called, incrementing the tg_nmembers count on the group.
|
2013-01-26 01:23:38 +08:00
|
|
|
*
|
2018-03-13 23:52:27 +08:00
|
|
|
* Input Parameters:
|
2013-01-26 01:23:38 +08:00
|
|
|
* tcb - The TCB of the new "child" task that need to join the group.
|
|
|
|
*
|
2018-02-02 00:00:02 +08:00
|
|
|
* Returned Value:
|
2013-01-26 03:15:05 +08:00
|
|
|
* 0 (OK) on success; a negated errno value on failure.
|
2013-01-26 01:23:38 +08:00
|
|
|
*
|
|
|
|
* Assumptions:
|
|
|
|
* - The parent task from which the group will be inherited is the task at
|
2017-05-12 03:35:56 +08:00
|
|
|
* the head of the ready to run list.
|
2013-01-26 01:23:38 +08:00
|
|
|
* - Called during thread creation 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 06:38:59 +08:00
|
|
|
int group_bind(FAR struct pthread_tcb_s *tcb)
|
2013-01-26 01:23:38 +08:00
|
|
|
{
|
2016-02-07 07:44:41 +08:00
|
|
|
FAR struct tcb_s *ptcb = this_task();
|
2013-01-26 01:23:38 +08:00
|
|
|
|
2013-02-05 06:38:59 +08:00
|
|
|
DEBUGASSERT(ptcb && tcb && ptcb->group && !tcb->cmn.group);
|
2013-01-26 01:23:38 +08:00
|
|
|
|
2013-01-26 03:15:05 +08:00
|
|
|
/* Copy the group reference from the parent to the child */
|
2013-01-26 01:23:38 +08:00
|
|
|
|
2013-02-05 06:38:59 +08:00
|
|
|
tcb->cmn.group = ptcb->group;
|
2013-01-26 03:15:05 +08:00
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
2015-10-03 21:25:53 +08:00
|
|
|
/****************************************************************************
|
2013-01-26 03:15:05 +08:00
|
|
|
* Name: group_join
|
|
|
|
*
|
|
|
|
* Description:
|
2021-02-09 01:46:31 +08:00
|
|
|
* A thread joins the group when it is created. This is a two step process,
|
2013-01-26 03:15:05 +08:00
|
|
|
* first, the group must bound to the new threads TCB. group_bind() does
|
|
|
|
* this (at the return from group_join, things are a little unstable: The
|
2014-08-28 04:22:00 +08:00
|
|
|
* group has been bound, but tg_nmembers has not yet been incremented).
|
2013-01-26 03:15:05 +08:00
|
|
|
* Then, after the new thread is initialized and has a PID assigned to it,
|
2014-04-14 04:32:20 +08:00
|
|
|
* group_join() is called, incrementing the tg_nmembers count on the group.
|
2013-01-26 03:15:05 +08:00
|
|
|
*
|
2018-03-13 23:52:27 +08:00
|
|
|
* Input Parameters:
|
2013-01-26 03:15:05 +08:00
|
|
|
* tcb - The TCB of the new "child" task that need to join the group.
|
|
|
|
*
|
2018-02-02 00:00:02 +08:00
|
|
|
* Returned Value:
|
2013-01-26 03:15:05 +08:00
|
|
|
* 0 (OK) on success; a negated errno value on failure.
|
|
|
|
*
|
|
|
|
* Assumptions:
|
|
|
|
* - The parent task from which the group will be inherited is the task at
|
2017-05-12 03:35:56 +08:00
|
|
|
* the head of the ready to run list.
|
2013-01-26 03:15:05 +08:00
|
|
|
* - Called during thread creation in a safe context. No special precautions
|
|
|
|
* are required here.
|
|
|
|
*
|
2015-10-03 21:25:53 +08:00
|
|
|
****************************************************************************/
|
2013-01-26 03:15:05 +08:00
|
|
|
|
2013-02-05 06:38:59 +08:00
|
|
|
int group_join(FAR struct pthread_tcb_s *tcb)
|
2013-01-26 03:15:05 +08:00
|
|
|
{
|
|
|
|
FAR struct task_group_s *group;
|
2013-01-27 01:28:20 +08:00
|
|
|
#ifdef HAVE_GROUP_MEMBERS
|
|
|
|
int ret;
|
|
|
|
#endif
|
2013-01-26 03:15:05 +08:00
|
|
|
|
2013-02-05 06:38:59 +08:00
|
|
|
DEBUGASSERT(tcb && tcb->cmn.group &&
|
|
|
|
tcb->cmn.group->tg_nmembers < UINT8_MAX);
|
2013-01-26 03:15:05 +08:00
|
|
|
|
|
|
|
/* Get the group from the TCB */
|
|
|
|
|
2013-02-05 06:38:59 +08:00
|
|
|
group = tcb->cmn.group;
|
2013-01-27 01:28:20 +08:00
|
|
|
|
2013-01-26 07:21:27 +08:00
|
|
|
#ifdef HAVE_GROUP_MEMBERS
|
2013-01-27 01:28:20 +08:00
|
|
|
/* Add the member to the group */
|
|
|
|
|
2013-02-05 06:38:59 +08:00
|
|
|
ret = group_addmember(group, tcb->cmn.pid);
|
2013-01-27 01:28:20 +08:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
group->tg_nmembers++;
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
2019-02-12 02:09:26 +08:00
|
|
|
#endif /* !CONFIG_DISABLE_PTHREAD */
|