diff options
Diffstat (limited to 'nuttx/sched/task_reparent.c')
-rw-r--r-- | nuttx/sched/task_reparent.c | 77 |
1 files changed, 37 insertions, 40 deletions
diff --git a/nuttx/sched/task_reparent.c b/nuttx/sched/task_reparent.c index 9daa0743b..244825f80 100644 --- a/nuttx/sched/task_reparent.c +++ b/nuttx/sched/task_reparent.c @@ -60,8 +60,8 @@ * Change the parent of a task. * * Parameters: - * oldpid - PID of the old parent task (0 if this task) - * newpid - PID ot the new parent task (0 for the parent of this task) + * ppid - PID of the new parent task (0 for grandparent, i.e. the parent + * of the current parent task) * chpid - PID of the child to be reparented. * * Return Value: @@ -69,72 +69,69 @@ * *****************************************************************************/ -int task_reparent(pid_t oldpid, pid_t newpid, pid_t chpid) +int task_reparent(pid_t ppid, pid_t chpid) { - _TCB *oldtcb; - _TCB *newtcb; + _TCB *ptcb; _TCB *chtcb; + _TCB *otcb; + pid_t opid; irqstate_t flags; int ret; - /* If oldpid is zero, then we are parent task. */ + /* Disable interrupts so that nothing can change in the relatinoship of + * the three task: Child, current parent, and new parent. + */ - if (oldpid == 0) - { - oldpid = getpid(); - } + flags = irqsave(); - /* Get the current parent task's TCB */ + /* Get the child tasks TCB (chtcb) */ - oldtcb = sched_gettcb(oldpid); - if (!oldtcb) + chtcb = sched_gettcb(chpid); + if (!chtcb) { - return -ESRCH; + ret = -ECHILD; + goto errout_with_ints; } - /* Disable interrupts so that nothing can change from this point */ + /* Get the PID of the child task's parent (opid) */ - flags = irqsave(); + opid = chtcb->parent; - /* If newpid is zero, then new is the parent of oldpid. */ + /* Get the TCB of the child task's parent (otcb) */ - if (newpid == 0) - { - newpid = oldtcb->parent; - } - - /* Get the new parent task's TCB */ - - newtcb = sched_gettcb(newpid); - if (!newtcb) + otcb = sched_gettcb(opid); + if (!otcb) { ret = -ESRCH; goto errout_with_ints; } - /* Get the child tasks TCB */ + /* If new parent task's PID (ppid) is zero, then new parent is the + * grandparent will be the new parent, i.e., the parent of the current + * parent task. + */ - chtcb = sched_gettcb(chpid); - if (!chtcb) + if (ppid == 0) { - ret = -ECHILD; - goto errout_with_ints; + ppid = otcb->parent; } + + /* Get the new parent task's TCB (ptcb) */ - /* Verify that oldpid is the parent of chpid */ - - if (chtcb->parent != oldpid) + ptcb = sched_gettcb(ppid); + if (!ptcb) { - ret = -ECHILD; + ret = -ESRCH; goto errout_with_ints; } - /* Okay, reparent the child */ + /* Then reparent the child */ + + DEBUGASSERT(otcb->nchildren > 0); - DEBUGASSERT(oldtcb->nchildren > 0); - chtcb->parent = newpid; - oldtcb->nchildren--; - newtcb->nchildren++; + chtcb->parent = ppid; /* The task specified by ppid is the new parent */ + otcb->nchildren--; /* The orignal parent now has one few children */ + ptcb->nchildren++; /* The new parent has one additional child */ ret = OK; errout_with_ints: |