2015-10-03 15:25:53 +02:00
|
|
|
/****************************************************************************
|
2014-08-08 22:06:42 +02:00
|
|
|
* sched/group/group_join.c
|
2013-01-25 18:23:38 +01:00
|
|
|
*
|
2016-02-14 15:17:46 +01:00
|
|
|
* Copyright (C) 2013, 2016 Gregory Nutt. All rights reserved.
|
2013-01-25 18:23:38 +01:00
|
|
|
* Author: Gregory Nutt <gnutt@nuttx.org>
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
*
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in
|
|
|
|
* the documentation and/or other materials provided with the
|
|
|
|
* distribution.
|
|
|
|
* 3. Neither the name NuttX nor the names of its contributors may be
|
|
|
|
* used to endorse or promote products derived from this software
|
|
|
|
* without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
|
|
|
|
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
|
|
|
|
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|
|
|
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
|
|
|
|
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
|
|
|
|
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
|
|
|
|
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
|
|
|
|
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
* POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*
|
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
|
|
|
|
2013-02-04 23:38:59 +01:00
|
|
|
#if defined(HAVE_TASK_GROUP) && !defined(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
|
|
|
****************************************************************************/
|
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
|
|
|
*
|
|
|
|
* Parameters:
|
|
|
|
* group - The task group to add the new member
|
|
|
|
* pid - The new member
|
|
|
|
*
|
|
|
|
* Return Value:
|
|
|
|
* 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:
|
2013-01-25 20:15:05 +01:00
|
|
|
* A thread joins the group when it is created. This is a two step process,
|
|
|
|
* 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
|
|
|
*
|
|
|
|
* Parameters:
|
|
|
|
* tcb - The TCB of the new "child" task that need to join the group.
|
|
|
|
*
|
|
|
|
* Return 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
|
|
|
|
* the thead of the ready to run list.
|
|
|
|
* - 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:
|
|
|
|
* A thread joins the group when it is created. This is a two step process,
|
|
|
|
* 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
|
|
|
*
|
|
|
|
* Parameters:
|
|
|
|
* tcb - The TCB of the new "child" task that need to join the group.
|
|
|
|
*
|
|
|
|
* Return Value:
|
|
|
|
* 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
|
|
|
|
* the thead of the ready to run list.
|
|
|
|
* - 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;
|
|
|
|
}
|
|
|
|
|
2013-02-04 23:38:59 +01:00
|
|
|
#endif /* HAVE_TASK_GROUP && !CONFIG_DISABLE_PTHREAD */
|