diff --git a/sched/task/task.h b/sched/task/task.h index aa656861e2..6ce8a3afcd 100644 --- a/sched/task/task.h +++ b/sched/task/task.h @@ -44,10 +44,16 @@ struct tcb_s; /* Forward reference */ /* Task start-up */ void nxtask_start(void); -int nxtask_setup_scheduler(FAR struct task_tcb_s *tcb, int priority, - start_t start, main_t main, uint8_t ttype); -int nxtask_setup_arguments(FAR struct task_tcb_s *tcb, - FAR const char *name, FAR char * const argv[]); +int nxtask_setup_stackargs(FAR struct task_tcb_s *tcb, + FAR const char *name, + FAR char * const argv[]); +int nxtask_setup_scheduler(FAR struct task_tcb_s *tcb, int priority, + start_t start, main_t main, uint8_t ttype); +#if CONFIG_TASK_NAME_SIZE > 0 +void nxtask_setup_name(FAR struct task_tcb_s *tcb, FAR const char *name); +#else +# define nxtask_setup_name(tcb, name) +#endif /* Task exit */ diff --git a/sched/task/task_fork.c b/sched/task/task_fork.c index 0cf22599ca..fca28ab802 100644 --- a/sched/task/task_fork.c +++ b/sched/task/task_fork.c @@ -188,6 +188,11 @@ FAR struct task_tcb_s *nxtask_setup_fork(start_t retaddr) goto errout_with_tcb; } + /* Set the task name */ + + argv = nxsched_get_stackargs(parent); + nxtask_setup_name(child, argv[0]); + /* Allocate the stack for the TCB */ stack_size = (uintptr_t)ptcb->stack_base_ptr - @@ -240,8 +245,7 @@ FAR struct task_tcb_s *nxtask_setup_fork(start_t retaddr) /* Setup to pass parameters to the new task */ - argv = nxsched_get_stackargs(parent); - ret = nxtask_setup_arguments(child, argv[0], &argv[1]); + ret = nxtask_setup_stackargs(child, argv[0], &argv[1]); if (ret < OK) { goto errout_with_tcb; diff --git a/sched/task/task_init.c b/sched/task/task_init.c index e4f71480df..bcf8b30811 100644 --- a/sched/task/task_init.c +++ b/sched/task/task_init.c @@ -142,6 +142,10 @@ int nxtask_init(FAR struct task_tcb_s *tcb, const char *name, int priority, goto errout_with_group; } + /* Set the task name */ + + nxtask_setup_name(tcb, name); + if (stack) { /* Use pre-allocated stack */ @@ -179,7 +183,7 @@ int nxtask_init(FAR struct task_tcb_s *tcb, const char *name, int priority, /* Setup to pass parameters to the new task */ - ret = nxtask_setup_arguments(tcb, name, argv); + ret = nxtask_setup_stackargs(tcb, name, argv); if (ret < OK) { goto errout_with_group; diff --git a/sched/task/task_setup.c b/sched/task/task_setup.c index c1e14681d2..fc4a38d41f 100644 --- a/sched/task/task_setup.c +++ b/sched/task/task_setup.c @@ -491,53 +491,14 @@ static int nxthread_setup_scheduler(FAR struct tcb_s *tcb, int priority, } /**************************************************************************** - * Name: nxtask_setup_name - * - * Description: - * Assign the task name. - * - * Input Parameters: - * tcb - Address of the new task's TCB - * name - Name of the new task - * - * Returned Value: - * None - * + * Public Functions ****************************************************************************/ -#if CONFIG_TASK_NAME_SIZE > 0 -static void nxtask_setup_name(FAR struct task_tcb_s *tcb, - FAR const char *name) -{ - FAR char *dst = tcb->cmn.name; - int i; - - /* Copy the name into the TCB */ - - for (i = 0; i < CONFIG_TASK_NAME_SIZE; i++) - { - char c = *name++; - - if (c == '\0') - { - break; - } - - *dst++ = isspace(c) ? '_' : c; - } - - *dst = '\0'; -} -#else -# define nxtask_setup_name(t,n) -#endif /* CONFIG_TASK_NAME_SIZE */ - /**************************************************************************** * Name: nxtask_setup_stackargs * * Description: - * This functions is called only from nxtask_setup_arguments() It will - * allocate space on the new task's stack and will copy the argv[] array + * Allocate space on the new task's stack and will copy the argv[] array * and all strings to the task's stack where it is readily accessible to * the task. Data on the stack, on the other hand, is guaranteed to be * accessible no matter what privilege mode the task runs in. @@ -554,9 +515,9 @@ static void nxtask_setup_name(FAR struct task_tcb_s *tcb, * ****************************************************************************/ -static int nxtask_setup_stackargs(FAR struct task_tcb_s *tcb, - FAR const char *name, - FAR char * const argv[]) +int nxtask_setup_stackargs(FAR struct task_tcb_s *tcb, + FAR const char *name, + FAR char * const argv[]) { FAR char **stackargv; FAR char *str; @@ -566,6 +527,13 @@ static int nxtask_setup_stackargs(FAR struct task_tcb_s *tcb, int argc; int i; + /* Give a name to the unnamed tasks */ + + if (!name) + { + name = (FAR char *)g_noname; + } + /* Get the size of the task name (including the NUL terminator) */ strtablen = (strlen(name) + 1); @@ -664,10 +632,6 @@ static int nxtask_setup_stackargs(FAR struct task_tcb_s *tcb, return OK; } -/**************************************************************************** - * Public Functions - ****************************************************************************/ - /**************************************************************************** * Name: nxtask_setup_scheduler * @@ -739,37 +703,26 @@ int pthread_setup_scheduler(FAR struct pthread_tcb_s *tcb, int priority, #endif /**************************************************************************** - * Name: nxtask_setup_arguments + * Name: nxtask_setup_name * * Description: - * This functions sets up parameters in the Task Control Block (TCB) in - * preparation for starting a new thread. - * - * nxtask_setup_arguments() is called only from nxtask_init() and - * nxtask_start() to create a new task. In the "normal" case, the argv[] - * array is a structure in the TCB, the arguments are cloned via strdup. - * - * In the kernel build case, the argv[] array and all strings are copied - * to the task's stack. This is done because the TCB (and kernel allocated - * strings) are only accessible in kernel-mode. Data on the stack, on the - * other hand, is guaranteed to be accessible no matter what mode the - * task runs in. + * Assign the task name. * * Input Parameters: * tcb - Address of the new task's TCB - * name - Name of the new task (not used) - * argv - A pointer to an array of input parameters. The array should be - * terminated with a NULL argv[] value. If no parameters are - * required, argv may be NULL. + * name - Name of the new task * * Returned Value: - * OK + * None * ****************************************************************************/ -int nxtask_setup_arguments(FAR struct task_tcb_s *tcb, - FAR const char *name, FAR char * const argv[]) +#if CONFIG_TASK_NAME_SIZE > 0 +void nxtask_setup_name(FAR struct task_tcb_s *tcb, FAR const char *name) { + FAR char *dst = tcb->cmn.name; + int i; + /* Give a name to the unnamed tasks */ if (!name) @@ -777,14 +730,20 @@ int nxtask_setup_arguments(FAR struct task_tcb_s *tcb, name = (FAR char *)g_noname; } - /* Setup the task name */ + /* Copy the name into the TCB */ - nxtask_setup_name(tcb, name); + for (i = 0; i < CONFIG_TASK_NAME_SIZE; i++) + { + char c = *name++; - /* Copy the argv[] array and all strings are to the task's stack. Data on - * the stack is guaranteed to be accessible by the ask no matter what - * privilege mode the task runs in. - */ + if (c == '\0') + { + break; + } - return nxtask_setup_stackargs(tcb, name, argv); + *dst++ = isspace(c) ? '_' : c; + } + + *dst = '\0'; } +#endif /* CONFIG_TASK_NAME_SIZE */