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