From dc5ec457e96f2c2e874f0626e0d8399ce136a4b3 Mon Sep 17 00:00:00 2001 From: patacongo Date: Sun, 11 Mar 2007 22:19:01 +0000 Subject: Divided _task_init() in several smaller functions that take fewer paramters. This was necessary to reduce the stack usage for the 8051/2 which has a tiny, 256 byte stack git-svn-id: svn://svn.code.sf.net/p/nuttx/code/trunk@58 42af7a65-404d-4744-a932-0658087f49c3 --- nuttx/sched/task_create.c | 351 ++++------------------------------------------ 1 file changed, 25 insertions(+), 326 deletions(-) (limited to 'nuttx/sched/task_create.c') diff --git a/nuttx/sched/task_create.c b/nuttx/sched/task_create.c index 07a6cab22..c3d1b453b 100644 --- a/nuttx/sched/task_create.c +++ b/nuttx/sched/task_create.c @@ -37,13 +37,12 @@ * Included Files ************************************************************/ +#include #include #include -#include #include #include #include -#include #include "os_internal.h" /************************************************************ @@ -62,323 +61,14 @@ * Private Variables ************************************************************/ -/* This is the name for un-named tasks */ - -static FAR char g_noname[] = "no name"; - /************************************************************ * Private Function Prototypes ************************************************************/ -static STATUS task_assignpid(FAR _TCB* tcb); - /************************************************************ * Private Functions ************************************************************/ -/************************************************************ - * Name: task_assignpid - * - * Description: - * This function assigns the next unique task ID to a task. - * - * Inputs: - * tcb - TCB of task - * - * Return: - * OK on success; ERROR on failure (errno is not set) - * - ************************************************************/ - -static STATUS task_assignpid(FAR _TCB *tcb) -{ - pid_t next_pid; - int hash_ndx; - int tries = 0; - - /* Disable pre-emption. This should provide sufficient protection - * for the following operation. - */ - - (void)sched_lock(); - - /* We'll try every allowable pid */ - - for (tries = 0; tries < CONFIG_MAX_TASKS; tries++) - { - /* Get the next process ID candidate */ - - next_pid = ++g_lastpid; - - /* Verify that the next_pid is in the valid range */ - - if (next_pid <= 0) - { - g_lastpid = 1; - next_pid = 1; - } - - /* Get the hash_ndx associated with the next_pid */ - - hash_ndx = PIDHASH(next_pid); - - /* Check if there is a (potential) duplicate of this pid */ - - if (!g_pidhash[hash_ndx].tcb) - { - g_pidhash[hash_ndx].tcb = tcb; - g_pidhash[hash_ndx].pid = next_pid; - tcb->pid = next_pid; - (void)sched_unlock(); - return OK; - } - } - - /* If we get here, then the g_pidhash[] table is completely full. - * We cannot allow another task to be started. - */ - - (void)sched_unlock(); - return ERROR; -} - -/************************************************************ - * Public Functions - ************************************************************/ - -/************************************************************ - * Name: task_start - * - * Description: - * This function is the low level entry point - * into the main thread of execution of a task. It receives - * initial control when the task is started and calls main - * entry point of the newly started task. - * - * Inputs: - * None - * - * Return: - * None - * - ************************************************************/ - -void task_start(void) -{ - FAR _TCB *tcb = (FAR _TCB*)g_readytorun.head; - int argc; - - /* Count how many non-null arguments we are passing */ - - for (argc = 1; argc <= CONFIG_MAX_TASK_ARGS; argc++) - { - /* The first non-null argument terminates the list */ - - if (!tcb->argv[argc]) - { - break; - } - } - - /* Call the 'main' entry point passing argc and argv. If/when - * the task returns, */ - - exit(tcb->entry.main(argc, tcb->argv)); -} - -/************************************************************ - * Name: _task_init - * - * Description: - * This functions initializes a Task Control Block (TCB) - * in preparation for starting a new thread. _task_init() - * is an internal version of the task_init() function that - * has some additional control arguments and task_init() - * is a wrapper function that creates a VxWorks-like user - * API. task_init() is, otherwise, not used by the OS. - * - * _task_init() is called from task_init() and task_start().\ - * It is also called from pthread_create() to create a - * a pthread (distinguished by the pthread argument). - * - * Unlike task_create(), task_init() does not activate the - * task. This must be done by calling task_activate() - * afterward. - * - * Input Parameters: - * tcb - Address of the new task's TCB - * name - Name of the new task (not used) - * priority - Priority of the new task - * entry - Entry point of a new task - * main - Application start point of the new task - * pthread - TRUE is the task emulates pthread behavior - * argv - A pointer to an array of input parameters. - * Up to CONFIG_MAX_TASK_ARG parameters may be - * provided. If fewer than CONFIG_MAX_TASK_ARG - * parameters are passed, the list should be - * terminated with a NULL argv[] value. - * If no parameters are required, argv may be NULL. - * - * Return Value: - * OK on success; ERROR on failure. - * - * This function can only failure is it is unable to assign - * a new, unique task ID to the TCB (errno is not set). - * - ************************************************************/ - -STATUS _task_init(FAR _TCB *tcb, const char *name, int priority, - start_t start, main_t main, boolean pthread, - char *argv[]) -{ - STATUS ret; - int i; - - /* Assign a unique task ID to the task. */ - - ret = task_assignpid(tcb); - if (ret == OK) - { - /* Save task priority and entry point in the TCB */ - - tcb->init_priority = (ubyte)priority; - tcb->sched_priority = (ubyte)priority; - tcb->start = start; - tcb->entry.main = main; - -#if CONFIG_TASK_NAME_SIZE > 0 - /* Give a name to the unnamed threads */ - - if (!name) - { - name = g_noname; - } - - /* copy the name into the TCB */ - - strncpy(tcb->name, name, CONFIG_TASK_NAME_SIZE); -#endif /* CONFIG_TASK_NAME_SIZE */ - - /* Save the arguments in the TCB */ - -#if CONFIG_TASK_NAME_SIZE > 0 - tcb->argv[0] = tcb->name; -#else - tcb->argv[0] = g_noname; -#endif - - /* For pthreads, args are strictly pass-by-value; the char* - * arguments wrap some unknown value cast to char*. However, - * for tasks, the life of the argument must be as long as - * the life of the task and the arguments must be strings. - * So for tasks, we have to to dup the strings. - */ - - if (!pthread) - { - /* The first NULL argument terminates the list of - * arguments. The argv pointer may be NULL if no - * parameters are passed. - */ - - i = 1; - if (argv) - { - for (; i < CONFIG_MAX_TASK_ARGS+1 && argv[i-1]; i++) - { - tcb->argv[i] = strdup(argv[i-1]); - } - } - } - else - { - /* Mark this task as a pthread */ - - tcb->flags |= TCB_FLAG_PTHREAD; - - /* And just copy the argument. For pthreads, there - * is really only a single argument, argv[0]. It is - * copy as a value -- NOT duplicated. - */ - - i = 2; - tcb->argv[1] = argv[0]; - } - - /* Nullify any unused argument storage */ - - for (; i < CONFIG_MAX_TASK_ARGS+1; i++) - { - tcb->argv[i] = NULL; - } - - /* Initialize other (non-zero) elements of the TCB */ - -#ifndef CONFIG_DISABLE_SIGNALS - tcb->sigprocmask = ALL_SIGNAL_SET; -#endif - tcb->task_state = TSTATE_TASK_INVALID; - - /* Initialize the processor-specific portion of the TCB */ - - up_initial_state(tcb); - - /* Add the task to the inactive task list */ - - sched_lock(); - dq_addfirst((FAR dq_entry_t*)tcb, &g_inactivetasks); - tcb->task_state = TSTATE_TASK_INACTIVE; - sched_unlock(); - } - - return ret; -} - -/************************************************************ - * Name: task_activate - * - * Description: - * This function activates tasks initialized by _task_init(). - * Without activation, a task is ineligible for execution - * by the scheduler. - * - * Input Parameters: - * tcb - The TCB for the task for the task (same as the - * task_init argument. - * - * Return Value: - * Always returns OK - * - ************************************************************/ - -STATUS task_activate(FAR _TCB *tcb) -{ -#ifdef CONFIG_SCHED_INSTRUMENTATION - irqstate_t flags = irqsave(); - - /* Check if this is really a re-start */ - - if (tcb->task_state != TSTATE_TASK_INACTIVE) - { - /* Inform the instrumentation layer that the task - * has stopped - */ - - sched_note_stop(tcb); - } - - /* Inform the instrumentation layer that the task - * has started - */ - - sched_note_start(tcb); - irqrestore(flags); -#endif - - up_unblock_task(tcb); - return OK; -} - /************************************************************ * Name: task_create * @@ -401,7 +91,13 @@ STATUS task_activate(FAR _TCB *tcb) * priority - Priority of the new task * stack_size - size (in bytes) of the stack needed * entry - Entry point of a new task - * arg* - Ten required task arguments to pass to func + * arg - A pointer to an array of input parameters. + * Up to CONFIG_MAX_TASK_ARG parameters may + * be provided. If fewer than CONFIG_MAX_TASK_ARG + * parameters are passed, the list should be + * terminated with a NULL argv[] value. + * If no parameters are required, argv may be + * NULL. * * Return Value: * Returns the non-zero process ID of the new task or @@ -452,29 +148,32 @@ int task_create(const char *name, int priority, } #endif - /* Initialize the task control block */ + /* Initialize the task control block */ - status = _task_init(tcb, name, priority, task_start, entry, - FALSE, argv); - if (status != OK) - { - sched_releasetcb(tcb); - return ERROR; - } + status = task_schedsetup(tcb, priority, task_start, entry); + if (status != OK) + { + sched_releasetcb(tcb); + return ERROR; + } + + /* Setup to pass parameters to the new task */ - /* Get the assigned pid before we start the task */ + (void)task_argsetup(tcb, name, FALSE, argv); - pid = (int)tcb->pid; + /* Get the assigned pid before we start the task */ - /* Activate the task */ + pid = (int)tcb->pid; - status = task_activate(tcb); - if (status != OK) + /* Activate the task */ + + status = task_activate(tcb); + if (status != OK) { dq_rem((FAR dq_entry_t*)tcb, &g_inactivetasks); sched_releasetcb(tcb); return ERROR; } - return pid; + return pid; } -- cgit v1.2.3