From 47b94bafa5045532f239ea57a3610873b1a71368 Mon Sep 17 00:00:00 2001 From: patacongo Date: Sat, 26 Jan 2013 23:49:02 +0000 Subject: Move socket data from TCB to task group structure. git-svn-id: http://svn.code.sf.net/p/nuttx/code/trunk@5570 42af7a65-404d-4744-a932-0658087f49c3 --- nuttx/ChangeLog | 6 +- nuttx/include/nuttx/net/net.h | 8 +- nuttx/include/nuttx/sched.h | 13 +- nuttx/net/net_sockets.c | 100 +++----------- nuttx/sched/Makefile | 6 +- nuttx/sched/env_clearenv.c | 5 +- nuttx/sched/env_dup.c | 12 +- nuttx/sched/env_internal.h | 8 +- nuttx/sched/env_release.c | 11 +- nuttx/sched/group_create.c | 2 +- nuttx/sched/group_internal.h | 8 +- nuttx/sched/group_leave.c | 34 +++-- nuttx/sched/group_releasefiles.c | 112 --------------- nuttx/sched/group_setupidlefiles.c | 147 ++++++++++++++++++++ nuttx/sched/group_setupstreams.c | 97 +++++++++++++ nuttx/sched/group_setuptaskfiles.c | 245 +++++++++++++++++++++++++++++++++ nuttx/sched/os_internal.h | 17 --- nuttx/sched/os_start.c | 2 +- nuttx/sched/pthread_create.c | 9 -- nuttx/sched/sched_getsockets.c | 7 +- nuttx/sched/sched_setupidlefiles.c | 151 --------------------- nuttx/sched/sched_setuppthreadfiles.c | 91 ------------- nuttx/sched/sched_setupstreams.c | 97 ------------- nuttx/sched/sched_setuptaskfiles.c | 248 ---------------------------------- nuttx/sched/task_create.c | 2 +- nuttx/sched/task_init.c | 2 +- nuttx/sched/task_vfork.c | 2 +- 27 files changed, 583 insertions(+), 859 deletions(-) delete mode 100644 nuttx/sched/group_releasefiles.c create mode 100644 nuttx/sched/group_setupidlefiles.c create mode 100644 nuttx/sched/group_setupstreams.c create mode 100644 nuttx/sched/group_setuptaskfiles.c delete mode 100644 nuttx/sched/sched_setupidlefiles.c delete mode 100644 nuttx/sched/sched_setuppthreadfiles.c delete mode 100644 nuttx/sched/sched_setupstreams.c delete mode 100644 nuttx/sched/sched_setuptaskfiles.c diff --git a/nuttx/ChangeLog b/nuttx/ChangeLog index b5933f620..255d69a3d 100644 --- a/nuttx/ChangeLog +++ b/nuttx/ChangeLog @@ -4035,6 +4035,8 @@ task ID in the child task's TCB. Instead, keep the parent task group IN the child task's task group. * fs/, sched/, include/nuttx/sched.h, and include/nutts/fs/fs.h: - Move file data from TCB to task group structure. + Move file data from the TCB to the task group structure. * libc/stdio/, sched/, include/nuttx/lib.h, and include/nutts/fs/fs.h: - Move stream data from TCB to task group structure. + Move stream data from the TCB to the task group structure. + * net/, sched/, and include/nuttx/net/net.h: Move socket data + from the TCB to the task group structure. diff --git a/nuttx/include/nuttx/net/net.h b/nuttx/include/nuttx/net/net.h index b625b2fbe..d23fb8796 100644 --- a/nuttx/include/nuttx/net/net.h +++ b/nuttx/include/nuttx/net/net.h @@ -1,7 +1,7 @@ /**************************************************************************** * include/nuttx/net/net.h * - * Copyright (C) 2007, 2009-2012 Gregory Nutt. All rights reserved. + * Copyright (C) 2007, 2009-2013 Gregory Nutt. All rights reserved. * Author: Gregory Nutt * * Redistribution and use in source and binary forms, with or without @@ -106,7 +106,6 @@ struct socket struct socketlist { sem_t sl_sem; /* Manage access to the socket list */ - int16_t sl_crefs; /* Reference count */ struct socket sl_sockets[CONFIG_NSOCKET_DESCRIPTORS]; }; #endif @@ -145,9 +144,8 @@ int net_checksd(int fd, int oflags); */ void weak_function net_initialize(void); -FAR struct socketlist *net_alloclist(void); -int net_addreflist(FAR struct socketlist *list); -int net_releaselist(FAR struct socketlist *list); +void net_initlist(FAR struct socketlist *list); +void net_releaselist(FAR struct socketlist *list); /* Given a socket descriptor, return the underly NuttX-specific socket * structure. diff --git a/nuttx/include/nuttx/sched.h b/nuttx/include/nuttx/sched.h index a084d50ba..244455cd4 100644 --- a/nuttx/include/nuttx/sched.h +++ b/nuttx/include/nuttx/sched.h @@ -83,6 +83,8 @@ # define HAVE_TASK_GROUP 1 # elif CONFIG_NFILE_STREAMS > 0 # define HAVE_TASK_GROUP 1 +# elif CONFIG_NSOCKET_DESCRIPTORS > 0 +# define HAVE_TASK_GROUP 1 # endif #endif @@ -311,7 +313,10 @@ struct task_group_s #endif /* CONFIG_NFILE_STREAMS */ /* Sockets ********************************************************************/ - /* Not yet (see struct socketlist) */ + +#if CONFIG_NSOCKET_DESCRIPTORS > 0 + struct socketlist tg_socketlist; /* Maps socket descriptor to socket */ +#endif }; #endif @@ -450,12 +455,6 @@ struct _TCB int pterrno; /* Current per-thread errno */ - /* Network socket *************************************************************/ - -#if CONFIG_NSOCKET_DESCRIPTORS > 0 - FAR struct socketlist *sockets; /* Maps file descriptor to file */ -#endif - /* State save areas ***********************************************************/ /* The form and content of these fields are processor-specific. */ diff --git a/nuttx/net/net_sockets.c b/nuttx/net/net_sockets.c index 81e48c121..ddb54c98c 100644 --- a/nuttx/net/net_sockets.c +++ b/nuttx/net/net_sockets.c @@ -116,99 +116,37 @@ void net_initialize(void) #if CONFIG_NSOCKET_DESCRIPTORS > 0 -/* Allocate a list of files for a new task */ +/* Initialize a list of sockets for a new task */ -FAR struct socketlist *net_alloclist(void) +void net_initlist(FAR struct socketlist *list) { - FAR struct socketlist *list; - list = (FAR struct socketlist*)kzalloc(sizeof(struct socketlist)); - if (list) - { - /* Start with a reference count of one */ - - list->sl_crefs = 1; - - /* Initialize the list access mutex */ + /* Initialize the list access mutex */ - (void)sem_init(&list->sl_sem, 0, 1); - } - return list; + (void)sem_init(&list->sl_sem, 0, 1); } -/* Increase the reference count on a file list */ +/* Release release resources held by the socket list */ -int net_addreflist(FAR struct socketlist *list) +void net_releaselist(FAR struct socketlist *list) { - if (list) - { - /* Increment the reference count on the list. - * NOTE: that we disable interrupts to do this - * (vs. taking the list semaphore). We do this - * because file cleanup operations often must be - * done from the IDLE task which cannot wait - * on semaphores. - */ - - register uip_lock_t flags = uip_lock(); - list->sl_crefs++; - uip_unlock(flags); - } - return OK; -} + int ndx; -/* Release a reference to the file list */ + DEBUGASSERT(list); -int net_releaselist(FAR struct socketlist *list) -{ - int crefs; - int ndx; + /* Close each open socket in the list. */ - if (list) + for (ndx = 0; ndx < CONFIG_NSOCKET_DESCRIPTORS; ndx++) { - /* Decrement the reference count on the list. - * NOTE: that we disable interrupts to do this - * (vs. taking the list semaphore). We do this - * because file cleanup operations often must be - * done from the IDLE task which cannot wait - * on semaphores. - */ - - uip_lock_t flags = uip_lock(); - crefs = --(list->sl_crefs); - uip_unlock(flags); - - /* If the count decrements to zero, then there is no reference - * to the structure and it should be deallocated. Since there - * are references, it would be an error if any task still held - * a reference to the list's semaphore. - */ - - if (crefs <= 0) - { - /* Close each open socket in the list - * REVISIT: psock_close() will attempt to use semaphores. - * If we actually are in the IDLE thread, then could this cause - * problems? Probably not, if the task has exited and crefs is - * zero, then there probably could not be a contender for the - * semaphore. - */ - - for (ndx = 0; ndx < CONFIG_NSOCKET_DESCRIPTORS; ndx++) - { - FAR struct socket *psock = &list->sl_sockets[ndx]; - if (psock->s_crefs > 0) - { - (void)psock_close(psock); - } - } - - /* Destroy the semaphore and release the filelist */ - - (void)sem_destroy(&list->sl_sem); - sched_free(list); - } + FAR struct socket *psock = &list->sl_sockets[ndx]; + if (psock->s_crefs > 0) + { + (void)psock_close(psock); + } } - return OK; + + /* Destroy the semaphore */ + + (void)sem_destroy(&list->sl_sem); } int sockfd_allocate(int minsd) diff --git a/nuttx/sched/Makefile b/nuttx/sched/Makefile index c8fe7cf69..7710ae058 100644 --- a/nuttx/sched/Makefile +++ b/nuttx/sched/Makefile @@ -39,9 +39,7 @@ ASRCS = AOBJS = $(ASRCS:.S=$(OBJEXT)) MISC_SRCS = os_start.c os_bringup.c errno_getptr.c errno_get.c errno_set.c -MISC_SRCS += sched_garbage.c sched_setupstreams.c sched_getfiles.c sched_getsockets.c -MISC_SRCS += sched_getstreams.c sched_setupidlefiles.c sched_setuptaskfiles.c -MISC_SRCS += sched_setuppthreadfiles.c +MISC_SRCS += sched_garbage.c sched_getfiles.c sched_getsockets.c sched_getstreams.c TSK_SRCS = prctl.c task_create.c task_init.c task_setup.c task_activate.c TSK_SRCS += task_start.c task_delete.c task_deletecurrent.c task_exithook.c @@ -81,7 +79,7 @@ endif endif GRP_SRCS = group_create.c group_join.c group_leave.c group_find.c -GRP_SRCS += group_releasefiles.c +GRP_SRCS += group_setupstreams.c group_setupidlefiles.c group_setuptaskfiles.c ifeq ($(CONFIG_SCHED_HAVE_PARENT),y) GRP_SRCS += task_reparent.c diff --git a/nuttx/sched/env_clearenv.c b/nuttx/sched/env_clearenv.c index a9e9f5efd..062fd60ed 100644 --- a/nuttx/sched/env_clearenv.c +++ b/nuttx/sched/env_clearenv.c @@ -74,7 +74,10 @@ int clearenv(void) { - env_release((FAR _TCB*)g_readytorun.head); + FAR _TCB *tcb = (FAR _TCB*)g_readytorun.head; + DEBUGASSERT(tcb->group); + + env_release(tcb->group); return OK; } diff --git a/nuttx/sched/env_dup.c b/nuttx/sched/env_dup.c index 479f7cae7..3b653b010 100644 --- a/nuttx/sched/env_dup.c +++ b/nuttx/sched/env_dup.c @@ -68,8 +68,8 @@ * exact duplicate of the parent task's environment. * * Parameters: - * ctcb The child tcb to receive the newly allocated copy of the parent - * TCB's environment structure with reference count equal to one + * group The child task group to receive the newly allocated copy of the + * parent task groups environment structure. * * Return Value: * zero on success @@ -79,14 +79,14 @@ * ****************************************************************************/ -int env_dup(FAR _TCB *ctcb) +int env_dup(FAR struct task_group_s *group) { FAR _TCB *ptcb = (FAR _TCB*)g_readytorun.head; FAR char *envp = NULL; size_t envlen; int ret = OK; - DEBUGASSERT(ctcb && ptcb && ctcb->group && ptcb->group); + DEBUGASSERT(group && ptcb && ptcb->group); /* Pre-emption must be disabled throughout the following because the * environment may be shared. @@ -108,8 +108,8 @@ int env_dup(FAR _TCB *ctcb) } else { - ctcb->group->tg_envsize = envlen; - ctcb->group->tg_envp = envp; + group->tg_envsize = envlen; + group->tg_envp = envp; memcpy(envp, ptcb->group->tg_envp, envlen); } } diff --git a/nuttx/sched/env_internal.h b/nuttx/sched/env_internal.h index 6f1097c0b..e02bf289d 100644 --- a/nuttx/sched/env_internal.h +++ b/nuttx/sched/env_internal.h @@ -49,8 +49,8 @@ ****************************************************************************/ #ifdef CONFIG_DISABLE_ENVIRON -# define env_dup(ptcb) (0) -# define env_release(ptcb) (0) +# define env_dup(group) (0) +# define env_release(group) (0) #else /**************************************************************************** @@ -75,8 +75,8 @@ extern "C" /* Functions used by the task/pthread creation and destruction logic */ -int env_dup(FAR _TCB *ctcb); -void env_release(FAR _TCB *tcb); +int env_dup(FAR struct task_group_s *group); +void env_release(FAR struct task_group_s *group); /* Functions used internally by the environment handling logic */ diff --git a/nuttx/sched/env_release.c b/nuttx/sched/env_release.c index 4de55c388..aebb1f7e8 100644 --- a/nuttx/sched/env_release.c +++ b/nuttx/sched/env_release.c @@ -64,8 +64,8 @@ * environ to NULL. * * Parameters: - * tcb Identifies the TCB containing the environment structure to be - * released. + * group Identifies the task group containing the environment structure + * to be released. * * Return Value: * None @@ -75,12 +75,9 @@ * ****************************************************************************/ -void env_release(FAR _TCB *tcb) +void env_release(FAR struct task_group_s *group) { - FAR struct task_group_s *group; - - DEBUGASSERT(tcb && tcb->group); - group = tcb->group; + DEBUGASSERT(group); /* Free any allocate environment strings */ diff --git a/nuttx/sched/group_create.c b/nuttx/sched/group_create.c index 768641be1..24f6923aa 100644 --- a/nuttx/sched/group_create.c +++ b/nuttx/sched/group_create.c @@ -198,7 +198,7 @@ int group_allocate(FAR _TCB *tcb) /* Duplicate the parent tasks envionment */ - ret = env_dup(tcb); + ret = env_dup(tcb->group); if (ret < 0) { kfree(tcb->group); diff --git a/nuttx/sched/group_internal.h b/nuttx/sched/group_internal.h index e6e0dfd16..ca6aacff7 100644 --- a/nuttx/sched/group_internal.h +++ b/nuttx/sched/group_internal.h @@ -113,10 +113,14 @@ void group_removechildren(FAR struct task_group_s *group); #endif /* CONFIG_SCHED_CHILD_STATUS */ #endif /* CONFIG_SCHED_HAVE_PARENT */ -/* File/network resources */ +/* Group data resource configuration */ #if CONFIG_NFILE_DESCRIPTORS > 0 || CONFIG_NSOCKET_DESCRIPTORS > 0 -int group_releasefiles(FAR _TCB *tcb); +int group_setupidlefiles(FAR _TCB *tcb); +int group_setuptaskfiles(FAR _TCB *tcb); +#if CONFIG_NFILE_STREAMS > 0 +int group_setupstreams(FAR _TCB *tcb); +#endif #endif #endif /* __SCHED_GROUP_INERNAL_H */ diff --git a/nuttx/sched/group_leave.c b/nuttx/sched/group_leave.c index 70ef93666..4dec30633 100644 --- a/nuttx/sched/group_leave.c +++ b/nuttx/sched/group_leave.c @@ -44,6 +44,10 @@ #include #include +#include +#include +#include + #include "group_internal.h" #include "env_internal.h" @@ -142,8 +146,7 @@ void group_remove(FAR struct task_group_s *group) * *****************************************************************************/ -static inline void group_release(FAR _TCB *tcb, - FAR struct task_group_s *group) +static inline void group_release(FAR struct task_group_s *group) { /* Free all un-reaped child exit status */ @@ -155,14 +158,29 @@ static inline void group_release(FAR _TCB *tcb, * soon as possible while we still have a functioning task. */ -#if CONFIG_NFILE_DESCRIPTORS > 0 || CONFIG_NSOCKET_DESCRIPTORS > 0 - (void)group_releasefiles(tcb); -#endif +#if CONFIG_NFILE_DESCRIPTORS > 0 + /* Free resources held by the file descriptor list */ + + files_releaselist(&group->tg_filelist); + +#if CONFIG_NFILE_STREAMS > 0 + /* Free resource held by the stream list */ + + lib_releaselist(&group->tg_streamlist); + +#endif /* CONFIG_NFILE_STREAMS */ +#endif /* CONFIG_NFILE_DESCRIPTORS */ + +#if CONFIG_NSOCKET_DESCRIPTORS > 0 + /* Free resource held by the socket list */ + + net_releaselist(&group->tg_socketlist); +#endif /* CONFIG_NSOCKET_DESCRIPTORS */ /* Release all shared environment variables */ #ifndef CONFIG_DISABLE_ENVIRON - env_release(tcb); + env_release(group); #endif #ifdef HAVE_GROUP_MEMBERS @@ -232,7 +250,7 @@ void group_leave(FAR _TCB *tcb) { /* Release all of the resource held by the task group */ - group_release(tcb, group); + group_release(group); } /* In any event, we can detach the group from the TCB so that we won't @@ -271,7 +289,7 @@ void group_leave(FAR _TCB *tcb) { /* Release all of the resource held by the task group */ - group_release(tcb, group); + group_release(group); } /* In any event, we can detach the group from the TCB so we won't do diff --git a/nuttx/sched/group_releasefiles.c b/nuttx/sched/group_releasefiles.c deleted file mode 100644 index b33415c76..000000000 --- a/nuttx/sched/group_releasefiles.c +++ /dev/null @@ -1,112 +0,0 @@ -/**************************************************************************** - * sched/group_releasefiles.c - * - * Copyright (C) 2007, 2008, 2012-2013 Gregory Nutt. All rights reserved. - * Author: Gregory Nutt - * - * 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. - * - ****************************************************************************/ - -/**************************************************************************** - * Included Files - ****************************************************************************/ - -#include - -#include -#include -#include -#include - -#if CONFIG_NFILE_DESCRIPTORS > 0 || CONFIG_NSOCKET_DESCRIPTORS > 0 - -/**************************************************************************** - * Private Functions - ****************************************************************************/ - -/**************************************************************************** - * Public Functions - ****************************************************************************/ - -/**************************************************************************** - * Name: group_releasefiles - * - * Description: - * Release file resources attached to a TCB. This file may be called - * multiple times as a task exists. It will be called as early as possible - * to support proper closing of complex drivers that may need to wait - * on external events. - * - * Parameters: - * tcb - tcb of the new task. - * - * Return Value: - * None - * - * Assumptions: - * - ****************************************************************************/ - -int group_releasefiles(_TCB *tcb) -{ - if (tcb) - { -#if CONFIG_NFILE_DESCRIPTORS > 0 - FAR struct task_group_s *group = tcb->group; - DEBUGASSERT(group); -#endif - -#if CONFIG_NFILE_DESCRIPTORS > 0 - /* Free resources used by the file descriptor list */ - - files_releaselist(&group->tg_filelist); - -#if CONFIG_NFILE_STREAMS > 0 - /* Free the stream list */ - - lib_releaselist(&group->tg_streamlist); - -#endif /* CONFIG_NFILE_STREAMS */ -#endif /* CONFIG_NFILE_DESCRIPTORS */ - -#if CONFIG_NSOCKET_DESCRIPTORS > 0 - /* Free the file descriptor list */ - - if (tcb->sockets) - { - net_releaselist(tcb->sockets); - tcb->sockets = NULL; - } -#endif /* CONFIG_NSOCKET_DESCRIPTORS */ - } - - return OK; -} - -#endif /* CONFIG_NFILE_DESCRIPTORS || CONFIG_NSOCKET_DESCRIPTORS */ diff --git a/nuttx/sched/group_setupidlefiles.c b/nuttx/sched/group_setupidlefiles.c new file mode 100644 index 000000000..98cc7885e --- /dev/null +++ b/nuttx/sched/group_setupidlefiles.c @@ -0,0 +1,147 @@ +/**************************************************************************** + * sched/group_setupidlefiles.c + * + * Copyright (C) 2007-2010, 2012-2013 Gregory Nutt. All rights reserved. + * Author: Gregory Nutt + * + * 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. + * + ****************************************************************************/ + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include + +#include +#include +#include +#include +#include +#include + +#include +#include + +#include "os_internal.h" + +#if CONFIG_NFILE_DESCRIPTORS > 0 || CONFIG_NSOCKET_DESCRIPTORS > 0 + +/**************************************************************************** + * Private Functions + ****************************************************************************/ + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: group_setupidlefiles + * + * Description: + * Configure the idle thread's TCB. + * + * Parameters: + * tcb - tcb of the idle task. + * + * Return Value: + * None + * + * Assumptions: + * + ****************************************************************************/ + +int group_setupidlefiles(FAR _TCB *tcb) +{ +#if CONFIG_NFILE_DESCRIPTORS > 0 || 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 + +#if CONFIG_NFILE_DESCRIPTORS > 0 || CONFIG_NFILE_DESCRIPTORS > 0 + DEBUGASSERT(group); +#endif + +#if CONFIG_NFILE_DESCRIPTORS > 0 + /* Initialize file descriptors for the TCB */ + + files_initlist(&group->tg_filelist); +#endif + +#if CONFIG_NSOCKET_DESCRIPTORS > 0 + /* Allocate socket descriptors for the TCB */ + + net_initlist(&group->tg_socketlist); +#endif + + /* Open stdin, dup to get stdout and stderr. This should always + * be the first file opened and, hence, should always get file + * descriptor 0. + */ + +#if CONFIG_NFILE_DESCRIPTORS > 0 && defined(CONFIG_DEV_CONSOLE) + fd = open("/dev/console", O_RDWR); + if (fd == 0) + { + /* Successfully opened /dev/console as stdin (fd == 0) */ + + (void)file_dup2(0, 1); + (void)file_dup2(0, 2); + } + else + { + /* We failed to open /dev/console OR for some reason, we opened + * it and got some file descriptor other than 0. + */ + + if (fd > 0) + { + slldbg("Open /dev/console fd: %d\n", fd); + (void)close(fd); + } + else + { + slldbg("Failed to open /dev/console: %d\n", errno); + } + return -ENFILE; + } +#endif + + /* Allocate file/socket streams for the TCB */ + +#if CONFIG_NFILE_STREAMS > 0 + return group_setupstreams(tcb); +#else + return OK; +#endif +} + +#endif /* CONFIG_NFILE_DESCRIPTORS || CONFIG_NSOCKET_DESCRIPTORS */ diff --git a/nuttx/sched/group_setupstreams.c b/nuttx/sched/group_setupstreams.c new file mode 100644 index 000000000..88e266280 --- /dev/null +++ b/nuttx/sched/group_setupstreams.c @@ -0,0 +1,97 @@ +/**************************************************************************** + * group_setupstreams.c + * + * Copyright (C) 2007-2008, 2010-2013 Gregory Nutt. All rights reserved. + * Author: Gregory Nutt + * + * 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. + * + ****************************************************************************/ + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include + +#include +#include + +#include +#include +#include + +/* Make sure that there are file or socket descriptors in the system and + * that some number of streams have been configured. + */ + +#if CONFIG_NFILE_DESCRIPTORS > 0 || CONFIG_NSOCKET_DESCRIPTORS > 0 +#if CONFIG_NFILE_STREAMS > 0 + +/**************************************************************************** + * Private Functions + ****************************************************************************/ + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: group_setupstreams + * + * Description: + * Setup streams data structures that may be used for standard C buffered + * I/O with underlying socket or file desciptors + * + ****************************************************************************/ + +int group_setupstreams(FAR _TCB *tcb) +{ + DEBUGASSERT(tcb && tcb->group); + + /* Initialize file streams for the task group */ + + lib_streaminit(&tcb->group->tg_streamlist); + + /* fdopen to get the stdin, stdout and stderr streams. The following logic + * depends on the fact that the library layer will allocate FILEs in order. + * + * fd = 0 is stdin (read-only) + * fd = 1 is stdout (write-only, append) + * fd = 2 is stderr (write-only, append) + */ + + (void)fs_fdopen(0, O_RDONLY, tcb); + (void)fs_fdopen(1, O_WROK|O_CREAT, tcb); + (void)fs_fdopen(2, O_WROK|O_CREAT, tcb); + + return OK; +} + +#endif /* CONFIG_NFILE_STREAMS > 0 */ +#endif /* CONFIG_NFILE_DESCRIPTORS > 0 || CONFIG_NSOCKET_DESCRIPTORS > 0*/ diff --git a/nuttx/sched/group_setuptaskfiles.c b/nuttx/sched/group_setuptaskfiles.c new file mode 100644 index 000000000..d52adcfee --- /dev/null +++ b/nuttx/sched/group_setuptaskfiles.c @@ -0,0 +1,245 @@ +/**************************************************************************** + * sched/group_setuptaskfiles.c + * + * Copyright (C) 2007-2008, 2010, 2012-2013 Gregory Nutt. All rights reserved. + * Author: Gregory Nutt + * + * 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. + * + ****************************************************************************/ + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include + +#include +#include + +#include +#include + +#include "os_internal.h" + +#if CONFIG_NFILE_DESCRIPTORS > 0 || CONFIG_NSOCKET_DESCRIPTORS > 0 + +/**************************************************************************** + * Pre-processor Definitions + ****************************************************************************/ + +/* Determine how many file descriptors to clone. If CONFIG_FDCLONE_DISABLE + * is set, no file descriptors will be cloned. If CONFIG_FDCLONE_STDIO is + * set, only the first three descriptors (stdin, stdout, and stderr) will + * be cloned. Otherwise all file descriptors will be cloned. + */ + +#if defined(CONFIG_FDCLONE_STDIO) && CONFIG_NFILE_DESCRIPTORS > 3 +# define NFDS_TOCLONE 3 +#else +# define NFDS_TOCLONE CONFIG_NFILE_DESCRIPTORS +#endif + +/**************************************************************************** + * Private Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: sched_dupfiles + * + * Description: + * Duplicate parent task's file descriptors. + * + * Input Parameters: + * tcb - tcb of the new task. + * + * Return Value: + * None + * + ****************************************************************************/ + +#if CONFIG_NFILE_DESCRIPTORS > 0 && !defined(CONFIG_FDCLONE_DISABLE) +static inline void sched_dupfiles(FAR _TCB *tcb) +{ + /* The parent task is the one at the head of the ready-to-run list */ + + FAR _TCB *rtcb = (FAR _TCB*)g_readytorun.head; + FAR struct file *parent; + 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. + */ + + /* 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++) + { + /* 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. + */ + + if (parent[i].f_inode) + { + /* Yes... duplicate it for the child */ + + (void)files_dup(&parent[i], &child[i]); + } + } +} +#else /* CONFIG_NFILE_DESCRIPTORS && !CONFIG_FDCLONE_DISABLE */ +# define sched_dupfiles(tcb) +#endif /* CONFIG_NFILE_DESCRIPTORS && !CONFIG_FDCLONE_DISABLE */ + +/**************************************************************************** + * Name: sched_dupsockets + * + * Description: + * Duplicate the parent task's socket descriptors. + * + * Input Parameters: + * tcb - tcb of the new task. + * + * Return Value: + * None + * + ****************************************************************************/ + +#if CONFIG_NSOCKET_DESCRIPTORS > 0 && !defined(CONFIG_SDCLONE_DISABLE) +static inline void sched_dupsockets(FAR _TCB *tcb) +{ + /* The parent task is the one at the head of the ready-to-run list */ + + FAR _TCB *rtcb = (FAR _TCB*)g_readytorun.head; + FAR struct socket *parent; + FAR struct socket *child; + int i; + + /* Duplicate the socket descriptors of all sockets opened by the parent + * task. + */ + + DEBUGASSERT(tcb && tcb->group && rtcb->group); + + /* Get pointers to the parent and child task socket lists */ + + parent = rtcb->group->tg_sockets->sl_sockets; + child = tcb->group->tg_sockets->sl_sockets; + + /* Check each socket in the parent socket list */ + + for (i = 0; i < CONFIG_NSOCKET_DESCRIPTORS; i++) + { + /* Check if this parent socket is allocated. We can tell if the + * socket is allocated because it will have a positive, non-zero + * reference count. + */ + + if (parent[i].s_crefs > 0) + { + /* Yes... duplicate it for the child */ + + (void)net_clone(&parent[i], &child[i]); + } + } +} +#else /* CONFIG_NSOCKET_DESCRIPTORS && !CONFIG_SDCLONE_DISABLE */ +# define sched_dupsockets(tcb) +#endif /* CONFIG_NSOCKET_DESCRIPTORS && !CONFIG_SDCLONE_DISABLE */ + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: group_setuptaskfiles + * + * Description: + * Configure a newly allocated TCB so that it will inherit + * file descriptors and streams from the parent task. + * + * Parameters: + * tcb - tcb of the new task. + * + * Return Value: + * Zero (OK) is returned on success; A negated errno value is returned on + * failure. + * + * Assumptions: + * + ****************************************************************************/ + +int group_setuptaskfiles(FAR _TCB *tcb) +{ +#if CONFIG_NFILE_DESCRIPTORS > 0 || CONFIG_NSOCKET_DESCRIPTORS > 0 + FAR struct task_group_s *group = tcb->group; + + DEBUGASSERT(group); +#endif + +#if CONFIG_NFILE_DESCRIPTORS > 0 + /* Initialize file descriptors for the TCB */ + + files_initlist(&group->tg_filelist); +#endif + +#if CONFIG_NSOCKET_DESCRIPTORS > 0 + /* Allocate socket descriptors for the TCB */ + + net_initlist(&group->tg_socketlist); +#endif + + /* Duplicate the parent task's file descriptors */ + + sched_dupfiles(tcb); + + /* Duplicate the parent task's socket descriptors */ + + sched_dupsockets(tcb); + + /* Allocate file/socket streams for the new TCB */ + +#if CONFIG_NFILE_STREAMS > 0 + return group_setupstreams(tcb); +#else + return OK; +#endif +} + +#endif /* CONFIG_NFILE_DESCRIPTORS > 0 || CONFIG_NSOCKET_DESCRIPTORS > 0*/ diff --git a/nuttx/sched/os_internal.h b/nuttx/sched/os_internal.h index 5aa6487d0..262a40ccc 100644 --- a/nuttx/sched/os_internal.h +++ b/nuttx/sched/os_internal.h @@ -99,14 +99,6 @@ enum os_crash_codes_e #define MAX_TASKS_MASK (CONFIG_MAX_TASKS-1) #define PIDHASH(pid) ((pid) & MAX_TASKS_MASK) -/* Stubs used when there are no file descriptors */ - -#if CONFIG_NFILE_DESCRIPTORS <= 0 && CONFIG_NSOCKET_DESCRIPTORS <= 0 -# define sched_setupidlefiles(t) (OK) -# define sched_setuptaskfiles(t) (OK) -# define sched_setuppthreadfiles(t) (OK) -#endif - /* One processor family supported by NuttX has a single, fixed hardware stack. * That is the 8051 family. So for that family only, there is a variant form * of kernel_thread() that does not take a stack size parameter. The following @@ -293,15 +285,6 @@ int sched_reprioritize(FAR _TCB *tcb, int sched_priority); FAR _TCB *sched_gettcb(pid_t pid); bool sched_verifytcb(FAR _TCB *tcb); -#if CONFIG_NFILE_DESCRIPTORS > 0 || CONFIG_NSOCKET_DESCRIPTORS > 0 -int sched_setupidlefiles(FAR _TCB *tcb); -int sched_setuptaskfiles(FAR _TCB *tcb); -int sched_setuppthreadfiles(FAR _TCB *tcb); -#if CONFIG_NFILE_STREAMS > 0 -int sched_setupstreams(FAR _TCB *tcb); -#endif -#endif - int sched_releasetcb(FAR _TCB *tcb); void sched_garbagecollection(void); diff --git a/nuttx/sched/os_start.c b/nuttx/sched/os_start.c index b2551d2a3..5e6eaa858 100644 --- a/nuttx/sched/os_start.c +++ b/nuttx/sched/os_start.c @@ -451,7 +451,7 @@ void os_start(void) * inherited by all of the threads created by the IDLE task. */ - (void)sched_setupidlefiles(&g_idletcb); + (void)group_setupidlefiles(&g_idletcb); /* Create initial tasks and bring-up the system */ diff --git a/nuttx/sched/pthread_create.c b/nuttx/sched/pthread_create.c index 9fd6a4a61..48a0788a6 100644 --- a/nuttx/sched/pthread_create.c +++ b/nuttx/sched/pthread_create.c @@ -296,15 +296,6 @@ int pthread_create(FAR pthread_t *thread, FAR pthread_attr_t *attr, } #endif - /* Associate file descriptors with the new task */ - - ret = sched_setuppthreadfiles(ptcb); - if (ret != OK) - { - errcode = ret; - goto errout_with_tcb; - } - /* Allocate a detachable structure to support pthread_join logic */ pjoin = (FAR join_t*)kzalloc(sizeof(join_t)); diff --git a/nuttx/sched/sched_getsockets.c b/nuttx/sched/sched_getsockets.c index cd499420f..ea988d6ff 100644 --- a/nuttx/sched/sched_getsockets.c +++ b/nuttx/sched/sched_getsockets.c @@ -1,7 +1,7 @@ /************************************************************************ * sched/sched_getsockets.c * - * Copyright (C) 2007, 2009 Gregory Nutt. All rights reserved. + * Copyright (C) 2007, 2009, 2013 Gregory Nutt. All rights reserved. * Author: Gregory Nutt * * Redistribution and use in source and binary forms, with or without @@ -71,7 +71,10 @@ FAR struct socketlist *sched_getsockets(void) { FAR _TCB *rtcb = (FAR _TCB*)g_readytorun.head; - return rtcb->sockets; + FAR struct task_group_s *group = rtcb->group; + + DEBUGASSERT(group); + return &group->tg_socketlist; } #endif /* CONFIG_NSOCKET_DESCRIPTORS */ diff --git a/nuttx/sched/sched_setupidlefiles.c b/nuttx/sched/sched_setupidlefiles.c deleted file mode 100644 index 4bbd4d3b7..000000000 --- a/nuttx/sched/sched_setupidlefiles.c +++ /dev/null @@ -1,151 +0,0 @@ -/**************************************************************************** - * sched/sched_setupidlefiles.c - * - * Copyright (C) 2007-2010, 2012 Gregory Nutt. All rights reserved. - * Author: Gregory Nutt - * - * 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. - * - ****************************************************************************/ - -/**************************************************************************** - * Included Files - ****************************************************************************/ - -#include - -#include -#include -#include -#include -#include -#include - -#include -#include - -#include "os_internal.h" - -#if CONFIG_NFILE_DESCRIPTORS > 0 || CONFIG_NSOCKET_DESCRIPTORS > 0 - -/**************************************************************************** - * Private Functions - ****************************************************************************/ - -/**************************************************************************** - * Public Functions - ****************************************************************************/ - -/**************************************************************************** - * Name: sched_setupidlefiles - * - * Description: - * Configure the idle thread's TCB. - * - * Parameters: - * tcb - tcb of the idle task. - * - * Return Value: - * None - * - * Assumptions: - * - ****************************************************************************/ - -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 - -#if CONFIG_NFILE_DESCRIPTORS > 0 - DEBUGASSERT(group); -#endif - - /* Initialize file descriptors for the TCB */ - -#if CONFIG_NFILE_DESCRIPTORS > 0 - files_initlist(&group->tg_filelist); -#endif - - /* Allocate socket descriptors for the TCB */ - -#if CONFIG_NSOCKET_DESCRIPTORS > 0 - tcb->sockets = net_alloclist(); - if (!tcb->sockets) - { - return -ENOMEM; - } -#endif - - /* Open stdin, dup to get stdout and stderr. This should always - * be the first file opened and, hence, should always get file - * descriptor 0. - */ - -#if CONFIG_NFILE_DESCRIPTORS > 0 && defined(CONFIG_DEV_CONSOLE) - fd = open("/dev/console", O_RDWR); - if (fd == 0) - { - /* Successfully opened /dev/console as stdin (fd == 0) */ - - (void)file_dup2(0, 1); - (void)file_dup2(0, 2); - } - else - { - /* We failed to open /dev/console OR for some reason, we opened - * it and got some file descriptor other than 0. - */ - - if (fd > 0) - { - slldbg("Open /dev/console fd: %d\n", fd); - (void)close(fd); - } - else - { - slldbg("Failed to open /dev/console: %d\n", errno); - } - return -ENFILE; - } -#endif - - /* Allocate file/socket streams for the TCB */ - -#if CONFIG_NFILE_STREAMS > 0 - return sched_setupstreams(tcb); -#else - return OK; -#endif -} - -#endif /* CONFIG_NFILE_DESCRIPTORS || CONFIG_NSOCKET_DESCRIPTORS */ diff --git a/nuttx/sched/sched_setuppthreadfiles.c b/nuttx/sched/sched_setuppthreadfiles.c deleted file mode 100644 index 91d72fa7f..000000000 --- a/nuttx/sched/sched_setuppthreadfiles.c +++ /dev/null @@ -1,91 +0,0 @@ -/**************************************************************************** - * sched_setuppthreadfiles.c - * - * Copyright (C) 2007, 2009, 2012 Gregory Nutt. All rights reserved. - * Author: Gregory Nutt - * - * 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. - * - ****************************************************************************/ - -/**************************************************************************** - * Included Files - ****************************************************************************/ - -#include - -#include - -#include -#include -#include - -#include "os_internal.h" - -#if CONFIG_NFILE_DESCRIPTORS > 0 || CONFIG_NSOCKET_DESCRIPTORS > 0 - -/**************************************************************************** - * Private Functions - ****************************************************************************/ - -/**************************************************************************** - * Public Functions - ****************************************************************************/ - -/**************************************************************************** - * Name: sched_setuppthreadfiles - * - * Description: - * Configure a newly allocated TCB so that it will inherit file - * descriptors and streams from the parent pthread. - * - * Parameters: - * tcb - tcb of the new task. - * - * Return Value: - * OK (if an error were returned, it would need to be a non-negated - * errno value). - * - * Assumptions: - * - ****************************************************************************/ - -int sched_setuppthreadfiles(FAR _TCB *tcb) -{ -#if CONFIG_NSOCKET_DESCRIPTORS > 0 - /* The child thread inherits the parent socket descriptors */ - - tcb->sockets = rtcb->sockets; - net_addreflist(tcb->sockets); - -#endif /* CONFIG_NSOCKET_DESCRIPTORS */ - - return OK; -} - -#endif /* CONFIG_NFILE_DESCRIPTORS || CONFIG_NSOCKET_DESCRIPTORS */ diff --git a/nuttx/sched/sched_setupstreams.c b/nuttx/sched/sched_setupstreams.c deleted file mode 100644 index fb2e4d0be..000000000 --- a/nuttx/sched/sched_setupstreams.c +++ /dev/null @@ -1,97 +0,0 @@ -/**************************************************************************** - * sched_setupstreams.c - * - * Copyright (C) 2007-2008, 2010-2012 Gregory Nutt. All rights reserved. - * Author: Gregory Nutt - * - * 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. - * - ****************************************************************************/ - -/**************************************************************************** - * Included Files - ****************************************************************************/ - -#include - -#include -#include - -#include -#include -#include - -/* Make sure that there are file or socket descriptors in the system and - * that some number of streams have been configured. - */ - -#if CONFIG_NFILE_DESCRIPTORS > 0 || CONFIG_NSOCKET_DESCRIPTORS > 0 -#if CONFIG_NFILE_STREAMS > 0 - -/**************************************************************************** - * Private Functions - ****************************************************************************/ - -/**************************************************************************** - * Public Functions - ****************************************************************************/ - -/**************************************************************************** - * Name: sched_setupstreams - * - * Description: - * Setup streams data structures that may be used for standard C buffered - * I/O with underlying socket or file desciptors - * - ****************************************************************************/ - -int sched_setupstreams(FAR _TCB *tcb) -{ - DEBUGASSERT(tcb && tcb->group); - - /* Initialize file streams for the task group */ - - lib_streaminit(&tcb->group->tg_streamlist); - - /* fdopen to get the stdin, stdout and stderr streams. The following logic - * depends on the fact that the library layer will allocate FILEs in order. - * - * fd = 0 is stdin (read-only) - * fd = 1 is stdout (write-only, append) - * fd = 2 is stderr (write-only, append) - */ - - (void)fs_fdopen(0, O_RDONLY, tcb); - (void)fs_fdopen(1, O_WROK|O_CREAT, tcb); - (void)fs_fdopen(2, O_WROK|O_CREAT, tcb); - - return OK; -} - -#endif /* CONFIG_NFILE_STREAMS > 0 */ -#endif /* CONFIG_NFILE_DESCRIPTORS > 0 || CONFIG_NSOCKET_DESCRIPTORS > 0*/ diff --git a/nuttx/sched/sched_setuptaskfiles.c b/nuttx/sched/sched_setuptaskfiles.c deleted file mode 100644 index 9e44147e9..000000000 --- a/nuttx/sched/sched_setuptaskfiles.c +++ /dev/null @@ -1,248 +0,0 @@ -/**************************************************************************** - * sched/sched_setuptaskfiles.c - * - * Copyright (C) 2007-2008, 2010, 2012-2013 Gregory Nutt. All rights reserved. - * Author: Gregory Nutt - * - * 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. - * - ****************************************************************************/ - -/**************************************************************************** - * Included Files - ****************************************************************************/ - -#include - -#include -#include - -#include -#include - -#include "os_internal.h" - -#if CONFIG_NFILE_DESCRIPTORS > 0 || CONFIG_NSOCKET_DESCRIPTORS > 0 - -/**************************************************************************** - * Pre-processor Definitions - ****************************************************************************/ - -/* Determine how many file descriptors to clone. If CONFIG_FDCLONE_DISABLE - * is set, no file descriptors will be cloned. If CONFIG_FDCLONE_STDIO is - * set, only the first three descriptors (stdin, stdout, and stderr) will - * be cloned. Otherwise all file descriptors will be cloned. - */ - -#if defined(CONFIG_FDCLONE_STDIO) && CONFIG_NFILE_DESCRIPTORS > 3 -# define NFDS_TOCLONE 3 -#else -# define NFDS_TOCLONE CONFIG_NFILE_DESCRIPTORS -#endif - -/**************************************************************************** - * Private Functions - ****************************************************************************/ - -/**************************************************************************** - * Name: sched_dupfiles - * - * Description: - * Duplicate parent task's file descriptors. - * - * Input Parameters: - * tcb - tcb of the new task. - * - * Return Value: - * None - * - ****************************************************************************/ - -#if CONFIG_NFILE_DESCRIPTORS > 0 && !defined(CONFIG_FDCLONE_DISABLE) -static inline void sched_dupfiles(FAR _TCB *tcb) -{ - /* The parent task is the one at the head of the ready-to-run list */ - - FAR _TCB *rtcb = (FAR _TCB*)g_readytorun.head; - FAR struct file *parent; - 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. - */ - - /* 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++) - { - /* 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. - */ - - if (parent[i].f_inode) - { - /* Yes... duplicate it for the child */ - - (void)files_dup(&parent[i], &child[i]); - } - } -} -#else /* CONFIG_NFILE_DESCRIPTORS && !CONFIG_FDCLONE_DISABLE */ -# define sched_dupfiles(tcb) -#endif /* CONFIG_NFILE_DESCRIPTORS && !CONFIG_FDCLONE_DISABLE */ - -/**************************************************************************** - * Name: sched_dupsockets - * - * Description: - * Duplicate the parent task's socket descriptors. - * - * Input Parameters: - * tcb - tcb of the new task. - * - * Return Value: - * None - * - ****************************************************************************/ - -#if CONFIG_NSOCKET_DESCRIPTORS > 0 && !defined(CONFIG_SDCLONE_DISABLE) -static inline void sched_dupsockets(FAR _TCB *tcb) -{ - /* The parent task is the one at the head of the ready-to-run list */ - - FAR _TCB *rtcb = (FAR _TCB*)g_readytorun.head; - FAR struct socket *parent; - FAR struct socket *child; - int i; - - /* Duplicate the socket descriptors of all sockets opened by the parent - * task. - */ - - if (rtcb->sockets) - { - /* Get pointers to the parent and child task socket lists */ - - parent = rtcb->sockets->sl_sockets; - child = tcb->sockets->sl_sockets; - - /* Check each socket in the parent socket list */ - - for (i = 0; i < CONFIG_NSOCKET_DESCRIPTORS; i++) - { - /* Check if this parent socket is allocated. We can tell if the - * socket is allocated because it will have a positive, non-zero - * reference count. - */ - - if (parent[i].s_crefs > 0) - { - /* Yes... duplicate it for the child */ - - (void)net_clone(&parent[i], &child[i]); - } - } - } -} -#else /* CONFIG_NSOCKET_DESCRIPTORS && !CONFIG_SDCLONE_DISABLE */ -# define sched_dupsockets(tcb) -#endif /* CONFIG_NSOCKET_DESCRIPTORS && !CONFIG_SDCLONE_DISABLE */ - -/**************************************************************************** - * Public Functions - ****************************************************************************/ - -/**************************************************************************** - * Name: sched_setuptaskfiles - * - * Description: - * Configure a newly allocated TCB so that it will inherit - * file descriptors and streams from the parent task. - * - * Parameters: - * tcb - tcb of the new task. - * - * Return Value: - * Zero (OK) is returned on success; A negated errno value is returned on - * failure. - * - * Assumptions: - * - ****************************************************************************/ - -int sched_setuptaskfiles(FAR _TCB *tcb) -{ -#if CONFIG_NFILE_DESCRIPTORS > 0 - 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 */ - -#if CONFIG_NSOCKET_DESCRIPTORS > 0 - tcb->sockets = net_alloclist(); - if (!tcb->sockets) - { - return -ENOMEM; - } -#endif - - /* Duplicate the parent task's file descriptors */ - - sched_dupfiles(tcb); - - /* Duplicate the parent task's socket descriptors */ - - sched_dupsockets(tcb); - - /* Allocate file/socket streams for the new TCB */ - -#if CONFIG_NFILE_STREAMS > 0 - return sched_setupstreams(tcb); -#else - return OK; -#endif -} - -#endif /* CONFIG_NFILE_DESCRIPTORS > 0 || CONFIG_NSOCKET_DESCRIPTORS > 0*/ diff --git a/nuttx/sched/task_create.c b/nuttx/sched/task_create.c index 85c0f5e92..944743200 100644 --- a/nuttx/sched/task_create.c +++ b/nuttx/sched/task_create.c @@ -134,7 +134,7 @@ static int thread_create(const char *name, uint8_t ttype, int priority, /* Associate file descriptors with the new task */ #if CONFIG_NFILE_DESCRIPTORS > 0 || CONFIG_NSOCKET_DESCRIPTORS > 0 - ret = sched_setuptaskfiles(tcb); + ret = group_setuptaskfiles(tcb); if (ret != OK) { errcode = -ret; diff --git a/nuttx/sched/task_init.c b/nuttx/sched/task_init.c index 8dfd8b7f6..78f35bc2a 100644 --- a/nuttx/sched/task_init.c +++ b/nuttx/sched/task_init.c @@ -137,7 +137,7 @@ int task_init(FAR _TCB *tcb, const char *name, int priority, /* Associate file descriptors with the new task */ #if CONFIG_NFILE_DESCRIPTORS > 0 || CONFIG_NSOCKET_DESCRIPTORS > 0 - ret = sched_setuptaskfiles(tcb); + ret = group_setuptaskfiles(tcb); if (ret < 0) { errcode = -ret; diff --git a/nuttx/sched/task_vfork.c b/nuttx/sched/task_vfork.c index 3f058bdec..5b42a1e55 100644 --- a/nuttx/sched/task_vfork.c +++ b/nuttx/sched/task_vfork.c @@ -125,7 +125,7 @@ FAR _TCB *task_vforksetup(start_t retaddr) /* Associate file descriptors with the new task */ #if CONFIG_NFILE_DESCRIPTORS > 0 || CONFIG_NSOCKET_DESCRIPTORS > 0 - ret = sched_setuptaskfiles(child); + ret = group_setuptaskfiles(child); if (ret != OK) { goto errout_with_tcb; -- cgit v1.2.3