mirror of
https://gitee.com/mirrors_PX4/PX4-Autopilot.git
synced 2026-10-27 12:03:33 +08:00
Move file data from TCB to task group
git-svn-id: http://svn.code.sf.net/p/nuttx/code/trunk@5567 42af7a65-404d-4744-a932-0658087f49c3
This commit is contained in:
@@ -176,6 +176,13 @@ void group_leave(FAR _TCB *tcb)
|
||||
|
||||
#if defined(CONFIG_SCHED_HAVE_PARENT) && defined(CONFIG_SCHED_CHILD_STATUS)
|
||||
group_removechildren(tcb->group);
|
||||
#endif
|
||||
/* Free all file-related resources now. We really need to close
|
||||
* files as soon as possible while we still have a functioning task.
|
||||
*/
|
||||
|
||||
#if CONFIG_NFILE_DESCRIPTORS > 0
|
||||
(void)sched_releasefiles(tcb);
|
||||
#endif
|
||||
/* Release all shared environment variables */
|
||||
|
||||
@@ -231,7 +238,14 @@ void group_leave(FAR _TCB *tcb)
|
||||
#if defined(CONFIG_SCHED_HAVE_PARENT) && defined(CONFIG_SCHED_CHILD_STATUS)
|
||||
group_removechildren(tcb->group);
|
||||
#endif
|
||||
/* Release all shared environment variables */
|
||||
/* Free all file-related resources now. We really need to close
|
||||
* files as soon as possible while we still have a functioning task.
|
||||
*/
|
||||
|
||||
#if CONFIG_NFILE_DESCRIPTORS > 0
|
||||
(void)sched_releasefiles(tcb);
|
||||
#endif
|
||||
/* Release all shared environment variables */
|
||||
|
||||
#ifndef CONFIG_DISABLE_ENVIRON
|
||||
env_release(tcb);
|
||||
|
||||
@@ -70,7 +70,10 @@
|
||||
FAR struct filelist *sched_getfiles(void)
|
||||
{
|
||||
FAR _TCB *rtcb = (FAR _TCB*)g_readytorun.head;
|
||||
return rtcb->filelist;
|
||||
FAR struct task_group_s *group = rtcb->group;
|
||||
|
||||
DEBUGASSERT(group);
|
||||
return &group->tg_filelist;
|
||||
}
|
||||
|
||||
#endif /* CONFIG_NFILE_DESCRIPTORS */
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
/****************************************************************************
|
||||
* sched/sched_releasefiles.c
|
||||
*
|
||||
* Copyright (C) 2007, 2008, 2012 Gregory Nutt. All rights reserved.
|
||||
* Copyright (C) 2007, 2008, 2012-2013 Gregory Nutt. All rights reserved.
|
||||
* Author: Gregory Nutt <gnutt@nuttx.org>
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
@@ -78,13 +78,12 @@ int sched_releasefiles(_TCB *tcb)
|
||||
if (tcb)
|
||||
{
|
||||
#if CONFIG_NFILE_DESCRIPTORS > 0
|
||||
/* Free the file descriptor list */
|
||||
FAR struct task_group_s *group = tcb->group;
|
||||
DEBUGASSERT(group);
|
||||
|
||||
if (tcb->filelist)
|
||||
{
|
||||
files_releaselist(tcb->filelist);
|
||||
tcb->filelist = NULL;
|
||||
}
|
||||
/* Free resources used by the file descriptor list */
|
||||
|
||||
files_releaselist(&group->tg_filelist);
|
||||
|
||||
#if CONFIG_NFILE_STREAMS > 0
|
||||
/* Free the stream list */
|
||||
|
||||
@@ -163,10 +163,6 @@ int sched_releasetcb(FAR _TCB *tcb)
|
||||
}
|
||||
}
|
||||
|
||||
/* Release any allocated file structures */
|
||||
|
||||
ret = sched_releasefiles(tcb);
|
||||
|
||||
/* Release this thread's reference to the address environment */
|
||||
|
||||
#ifdef CONFIG_ADDRENV
|
||||
|
||||
@@ -79,18 +79,21 @@
|
||||
|
||||
int sched_setupidlefiles(FAR _TCB *tcb)
|
||||
{
|
||||
#if CONFIG_NFILE_DESCRIPTORS > 0
|
||||
FAR struct task_group_s *group = tcb->group;
|
||||
#endif
|
||||
#if CONFIG_NFILE_DESCRIPTORS > 0 && defined(CONFIG_DEV_CONSOLE)
|
||||
int fd;
|
||||
#endif
|
||||
|
||||
/* Allocate file descriptors for the TCB */
|
||||
#if CONFIG_NFILE_DESCRIPTORS > 0
|
||||
DEBUGASSERT(group);
|
||||
#endif
|
||||
|
||||
/* Initialize file descriptors for the TCB */
|
||||
|
||||
#if CONFIG_NFILE_DESCRIPTORS > 0
|
||||
tcb->filelist = files_alloclist();
|
||||
if (!tcb->filelist)
|
||||
{
|
||||
return -ENOMEM;
|
||||
}
|
||||
files_initlist(&group->tg_filelist);
|
||||
#endif
|
||||
|
||||
/* Allocate socket descriptors for the TCB */
|
||||
|
||||
@@ -79,14 +79,6 @@ int sched_setuppthreadfiles(FAR _TCB *tcb)
|
||||
{
|
||||
FAR _TCB *rtcb = (FAR _TCB*)g_readytorun.head;
|
||||
|
||||
#if CONFIG_NFILE_DESCRIPTORS > 0
|
||||
/* The child thread inherits the parent file descriptors */
|
||||
|
||||
tcb->filelist = rtcb->filelist;
|
||||
files_addreflist(tcb->filelist);
|
||||
|
||||
#endif /* CONFIG_NFILE_DESCRIPTORS */
|
||||
|
||||
#if CONFIG_NSOCKET_DESCRIPTORS > 0
|
||||
/* The child thread inherits the parent socket descriptors */
|
||||
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
/****************************************************************************
|
||||
* sched/sched_setuptaskfiles.c
|
||||
*
|
||||
* Copyright (C) 2007-2008, 2010, 2012 Gregory Nutt. All rights reserved.
|
||||
* Copyright (C) 2007-2008, 2010, 2012-2013 Gregory Nutt. All rights reserved.
|
||||
* Author: Gregory Nutt <gnutt@nuttx.org>
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
@@ -93,34 +93,33 @@ static inline void sched_dupfiles(FAR _TCB *tcb)
|
||||
FAR struct file *child;
|
||||
int i;
|
||||
|
||||
DEBUGASSERT(tcb && tcb->group && rtcb->group);
|
||||
|
||||
/* Duplicate the file descriptors. This will be either all of the
|
||||
* file descriptors or just the first three (stdin, stdout, and stderr)
|
||||
* if CONFIG_FDCLONE_STDIO is defined. NFSDS_TOCLONE is set
|
||||
* accordingly above.
|
||||
*/
|
||||
|
||||
if (rtcb->filelist)
|
||||
/* Get pointers to the parent and child task file lists */
|
||||
|
||||
parent = rtcb->group->tg_filelist.fl_files;
|
||||
child = tcb->group->tg_filelist.fl_files;
|
||||
|
||||
/* Check each file in the parent file list */
|
||||
|
||||
for (i = 0; i < NFDS_TOCLONE; i++)
|
||||
{
|
||||
/* Get pointers to the parent and child task file lists */
|
||||
/* Check if this file is opened by the parent. We can tell if
|
||||
* if the file is open because it contain a reference to a non-NULL
|
||||
* i-node structure.
|
||||
*/
|
||||
|
||||
parent = rtcb->filelist->fl_files;
|
||||
child = tcb->filelist->fl_files;
|
||||
|
||||
/* Check each file in the parent file list */
|
||||
|
||||
for (i = 0; i < NFDS_TOCLONE; i++)
|
||||
if (parent[i].f_inode)
|
||||
{
|
||||
/* Check if this file is opened by the parent. We can tell if
|
||||
* if the file is open because it contain a reference to a non-NULL
|
||||
* i-node structure.
|
||||
*/
|
||||
/* Yes... duplicate it for the child */
|
||||
|
||||
if (parent[i].f_inode)
|
||||
{
|
||||
/* Yes... duplicate it for the child */
|
||||
|
||||
(void)files_dup(&parent[i], &child[i]);
|
||||
}
|
||||
(void)files_dup(&parent[i], &child[i]);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -209,14 +208,14 @@ static inline void sched_dupsockets(FAR _TCB *tcb)
|
||||
|
||||
int sched_setuptaskfiles(FAR _TCB *tcb)
|
||||
{
|
||||
/* Allocate file descriptors for the TCB */
|
||||
|
||||
#if CONFIG_NFILE_DESCRIPTORS > 0
|
||||
tcb->filelist = files_alloclist();
|
||||
if (!tcb->filelist)
|
||||
{
|
||||
return -ENOMEM;
|
||||
}
|
||||
FAR struct task_group_s *group = tcb->group;
|
||||
|
||||
DEBUGASSERT(group);
|
||||
|
||||
/* Initialize file descriptors for the TCB */
|
||||
|
||||
files_initlist(&group->tg_filelist);
|
||||
#endif
|
||||
|
||||
/* Allocate socket descriptors for the TCB */
|
||||
|
||||
+116
-76
@@ -177,6 +177,89 @@ static inline void task_onexit(FAR _TCB *tcb, int status)
|
||||
# define task_onexit(tcb,status)
|
||||
#endif
|
||||
|
||||
/****************************************************************************
|
||||
* Name: task_exitstatus
|
||||
*
|
||||
* Description:
|
||||
* Report exit status when main task of a task group exits
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
#ifdef CONFIG_SCHED_CHILD_STATUS
|
||||
static inline void task_exitstatus(FAR struct task_group_s *group, int status)
|
||||
{
|
||||
FAR struct child_status_s *child;
|
||||
|
||||
/* Check if the parent task group has suppressed retention of
|
||||
* child exit status information.
|
||||
*/
|
||||
|
||||
if ((group->tg_flags & GROUP_FLAG_NOCLDWAIT) == 0)
|
||||
{
|
||||
/* No.. Find the exit status entry for this task in the parent TCB */
|
||||
|
||||
child = group_findchild(group, getpid());
|
||||
DEBUGASSERT(child);
|
||||
if (child)
|
||||
{
|
||||
#ifndef HAVE_GROUP_MEMBERS
|
||||
/* No group members? Save the exit status */
|
||||
|
||||
child->ch_status = status;
|
||||
#endif
|
||||
/* Save the exit status.. For the case of HAVE_GROUP_MEMBERS,
|
||||
* the child status will be as exited until the last member
|
||||
* of the task group exits.
|
||||
*/
|
||||
|
||||
child->ch_status = status;
|
||||
}
|
||||
}
|
||||
}
|
||||
#else
|
||||
|
||||
# define task_exitstatus(group,status)
|
||||
|
||||
#endif /* CONFIG_SCHED_CHILD_STATUS */
|
||||
|
||||
/****************************************************************************
|
||||
* Name: task_groupexit
|
||||
*
|
||||
* Description:
|
||||
* Mark that the final thread of a child task group as exited.
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
#ifdef CONFIG_SCHED_CHILD_STATUS
|
||||
static inline void task_groupexit(FAR struct task_group_s *group)
|
||||
{
|
||||
FAR struct child_status_s *child;
|
||||
|
||||
/* Check if the parent task group has suppressed retention of child exit
|
||||
* status information.
|
||||
*/
|
||||
|
||||
if ((group->tg_flags & GROUP_FLAG_NOCLDWAIT) == 0)
|
||||
{
|
||||
/* No.. Find the exit status entry for this task in the parent TCB */
|
||||
|
||||
child = group_findchild(group, getpid());
|
||||
DEBUGASSERT(child);
|
||||
if (child)
|
||||
{
|
||||
/* Mark that all members of the child task group has exit'ed */
|
||||
|
||||
child->ch_flags |= CHILD_FLAG_EXITED;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#else
|
||||
|
||||
# define task_groupexit(group)
|
||||
|
||||
#endif /* CONFIG_SCHED_CHILD_STATUS */
|
||||
|
||||
/****************************************************************************
|
||||
* Name: task_sigchild
|
||||
*
|
||||
@@ -195,55 +278,41 @@ static inline void task_sigchild(gid_t pgid, FAR _TCB *ctcb, int status)
|
||||
|
||||
DEBUGASSERT(chgrp);
|
||||
|
||||
/* Only the final exiting thread in a task group should generate SIGCHLD. */
|
||||
/* Get the parent task group. It is possible that all of the members of
|
||||
* the parent task group have exited. This would not be an error. In
|
||||
* this case, the child task group has been orphaned.
|
||||
*/
|
||||
|
||||
pgrp = group_find(chgrp->tg_pgid);
|
||||
if (!pgrp)
|
||||
{
|
||||
/* Set the task group ID to an invalid group ID. The dead parent
|
||||
* task group ID could get reused some time in the future.
|
||||
*/
|
||||
|
||||
chgrp->tg_pgid = INVALID_GROUP_ID;
|
||||
return;
|
||||
}
|
||||
|
||||
/* Save the exit status now if this is the main thread of the task group
|
||||
* that is exiting. Only the exiting main task of a task group carries
|
||||
* interpretable exit Check if this is the main task that is exiting.
|
||||
*/
|
||||
|
||||
if ((ctcb->flags & TCB_FLAG_TTYPE_MASK) == TCB_FLAG_TTYPE_TASK)
|
||||
{
|
||||
task_exitstatus(pgrp, status);
|
||||
}
|
||||
|
||||
/* But only the final exiting thread in a task group, whatever it is,
|
||||
* should generate SIGCHLD.
|
||||
*/
|
||||
|
||||
if (chgrp->tg_nmembers == 1)
|
||||
{
|
||||
/* Get the parent task group */
|
||||
/* Mark that all of the threads in the task group have exited */
|
||||
|
||||
pgrp = group_find(chgrp->tg_pgid);
|
||||
|
||||
/* It is possible that all of the members of the parent task group
|
||||
* have exited. This would not be an error. In this case, the
|
||||
* child task group has been orphaned.
|
||||
*/
|
||||
|
||||
if (!pgrp)
|
||||
{
|
||||
/* Set the task group ID to an invalid group ID. The dead parent
|
||||
* task group ID could get reused some time in the future.
|
||||
*/
|
||||
|
||||
chgrp->tg_pgid = INVALID_GROUP_ID;
|
||||
return;
|
||||
}
|
||||
|
||||
#ifdef CONFIG_SCHED_CHILD_STATUS
|
||||
/* Check if the parent task group has suppressed retention of child exit
|
||||
* status information. Only 'tasks' report exit status, not pthreads.
|
||||
* pthreads have a different mechanism.
|
||||
*/
|
||||
|
||||
if ((pgrp->tg_flags & GROUP_FLAG_NOCLDWAIT) == 0)
|
||||
{
|
||||
FAR struct child_status_s *child;
|
||||
|
||||
/* No.. Find the exit status entry for this task in the parent TCB */
|
||||
|
||||
child = group_findchild(pgrp, getpid());
|
||||
DEBUGASSERT(child);
|
||||
if (child)
|
||||
{
|
||||
/* Mark that the child has exit'ed */
|
||||
|
||||
child->ch_flags |= CHILD_FLAG_EXITED;
|
||||
|
||||
/* Save the exit status */
|
||||
|
||||
child->ch_status = status;
|
||||
}
|
||||
}
|
||||
#endif /* CONFIG_SCHED_CHILD_STATUS */
|
||||
task_groupexit(pgrp);
|
||||
|
||||
/* Create the siginfo structure. We don't actually know the cause.
|
||||
* That is a bug. Let's just say that the child task just exit-ted
|
||||
@@ -278,30 +347,9 @@ static inline void task_sigchild(FAR _TCB *ptcb, FAR _TCB *ctcb, int status)
|
||||
if ((ctcb->flags & TCB_FLAG_TTYPE_MASK) == TCB_FLAG_TTYPE_TASK)
|
||||
{
|
||||
#ifdef CONFIG_SCHED_CHILD_STATUS
|
||||
/* Check if the parent task group has suppressed retention of child exit
|
||||
* status information. Only 'tasks' report exit status, not pthreads.
|
||||
* pthreads have a different mechanism.
|
||||
*/
|
||||
/* Save the exit status now of the main thread */
|
||||
|
||||
if ((ptcb->group->tg_flags & GROUP_FLAG_NOCLDWAIT) == 0)
|
||||
{
|
||||
FAR struct child_status_s *child;
|
||||
|
||||
/* No.. Find the exit status entry for this task in the parent TCB */
|
||||
|
||||
child = group_findchild(ptcb->group, getpid());
|
||||
DEBUGASSERT(child);
|
||||
if (child)
|
||||
{
|
||||
/* Mark that the child has exit'ed */
|
||||
|
||||
child->ch_flags |= CHILD_FLAG_EXITED;
|
||||
|
||||
/* Save the exit status */
|
||||
|
||||
child->ch_status = status;
|
||||
}
|
||||
}
|
||||
task_exitstatus(ptcb->group, status);
|
||||
|
||||
#else /* CONFIG_SCHED_CHILD_STATUS */
|
||||
|
||||
@@ -506,14 +554,6 @@ void task_exithook(FAR _TCB *tcb, int status)
|
||||
group_leave(tcb);
|
||||
#endif
|
||||
|
||||
/* Free all file-related resources now. This gets called again
|
||||
* just be be certain when the TCB is delallocated. However, we
|
||||
* really need to close files as soon as possible while we still
|
||||
* have a functioning task.
|
||||
*/
|
||||
|
||||
(void)sched_releasefiles(tcb);
|
||||
|
||||
/* Deallocate anything left in the TCB's queues */
|
||||
|
||||
#ifndef CONFIG_DISABLE_SIGNALS
|
||||
|
||||
Reference in New Issue
Block a user