2009-06-17 22:25:27 +02:00
|
|
|
/****************************************************************************
|
2014-08-09 00:44:08 +02:00
|
|
|
* sched/task/task_init.c
|
2007-03-02 22:27:47 +01:00
|
|
|
*
|
2014-09-03 22:58:24 +02:00
|
|
|
* Copyright (C) 2007, 2009, 2013-2014 Gregory Nutt. All rights reserved.
|
2012-07-14 21:30:31 +02:00
|
|
|
* Author: Gregory Nutt <gnutt@nuttx.org>
|
2007-03-02 22:27:47 +01:00
|
|
|
*
|
|
|
|
* 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.
|
2009-06-17 22:25:27 +02:00
|
|
|
* 3. Neither the name NuttX nor the names of its contributors may be
|
2007-03-02 22:27:47 +01:00
|
|
|
* 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.
|
|
|
|
*
|
2009-06-17 22:25:27 +02:00
|
|
|
****************************************************************************/
|
2007-03-02 22:27:47 +01:00
|
|
|
|
2009-06-17 22:25:27 +02:00
|
|
|
/****************************************************************************
|
2007-03-02 22:27:47 +01:00
|
|
|
* Included Files
|
2009-06-17 22:25:27 +02:00
|
|
|
****************************************************************************/
|
2007-03-02 22:27:47 +01:00
|
|
|
|
2007-03-11 23:19:01 +01:00
|
|
|
#include <nuttx/config.h>
|
2009-12-14 19:39:29 +01:00
|
|
|
|
2007-03-02 22:27:47 +01:00
|
|
|
#include <sys/types.h>
|
2009-12-14 19:39:29 +01:00
|
|
|
#include <stdint.h>
|
2007-03-02 22:27:47 +01:00
|
|
|
#include <sched.h>
|
2013-01-25 18:23:38 +01:00
|
|
|
#include <errno.h>
|
|
|
|
|
2007-03-11 18:37:47 +01:00
|
|
|
#include <nuttx/arch.h>
|
2009-06-17 22:25:27 +02:00
|
|
|
|
2014-08-09 01:53:55 +02:00
|
|
|
#include "sched/sched.h"
|
2014-08-08 22:06:42 +02:00
|
|
|
#include "group/group.h"
|
2014-08-09 00:44:08 +02:00
|
|
|
#include "task/task.h"
|
2007-03-02 22:27:47 +01:00
|
|
|
|
2009-06-17 22:25:27 +02:00
|
|
|
/****************************************************************************
|
2007-03-02 22:27:47 +01:00
|
|
|
* Public Functions
|
2009-06-17 22:25:27 +02:00
|
|
|
****************************************************************************/
|
2007-03-02 22:27:47 +01:00
|
|
|
|
2009-06-17 22:25:27 +02:00
|
|
|
/****************************************************************************
|
2007-03-02 22:27:47 +01:00
|
|
|
* Name: task_init
|
|
|
|
*
|
|
|
|
* Description:
|
2009-06-17 22:25:27 +02:00
|
|
|
* This function initializes a Task Control Block (TCB) in preparation for
|
|
|
|
* starting a new thread. It performs a subset of the functionality of
|
|
|
|
* task_create()
|
2007-03-11 23:19:01 +01:00
|
|
|
*
|
2009-06-17 22:25:27 +02:00
|
|
|
* Unlike task_create():
|
|
|
|
* 1. Allocate the TCB. The pre-allocated TCB is passed in the arguments.
|
|
|
|
* 2. Allocate the stack. The pre-allocated stack is passed in the arguments.
|
|
|
|
* 3. Activate the task. This must be done by calling task_activate().
|
2007-03-02 22:27:47 +01:00
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* tcb - Address of the new task's TCB
|
|
|
|
* name - Name of the new task (not used)
|
|
|
|
* priority - Priority of the new task
|
2007-03-11 23:19:01 +01:00
|
|
|
* stack - Start of the pre-allocated stack
|
|
|
|
* stack_size - Size (in bytes) of the stack allocated
|
2007-03-02 22:27:47 +01:00
|
|
|
* entry - Application start point of the new task
|
2009-06-17 22:25:27 +02:00
|
|
|
* 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.
|
2007-03-02 22:27:47 +01:00
|
|
|
*
|
|
|
|
* Return Value:
|
2013-01-25 18:23:38 +01:00
|
|
|
* OK on success; ERROR on failure with errno set appropriately. (See
|
|
|
|
* task_schedsetup() for possible failure conditions). On failure, the
|
|
|
|
* caller is responsible for freeing the stack memory and for calling
|
|
|
|
* sched_releasetcb() to free the TCB (which could be in most any state).
|
2007-03-02 22:27:47 +01:00
|
|
|
*
|
2009-06-17 22:25:27 +02:00
|
|
|
****************************************************************************/
|
2007-03-02 22:27:47 +01:00
|
|
|
|
2013-02-04 19:46:28 +01:00
|
|
|
int task_init(FAR struct tcb_s *tcb, const char *name, int priority,
|
2009-12-14 19:39:29 +01:00
|
|
|
FAR uint32_t *stack, uint32_t stack_size,
|
2013-02-02 20:31:30 +01:00
|
|
|
main_t entry, FAR char * const argv[])
|
2007-03-02 22:27:47 +01:00
|
|
|
{
|
2013-02-04 22:24:00 +01:00
|
|
|
FAR struct task_tcb_s *ttcb = (FAR struct task_tcb_s *)tcb;
|
2013-01-25 18:23:38 +01:00
|
|
|
int errcode;
|
2009-12-13 19:01:46 +01:00
|
|
|
int ret;
|
2007-03-11 23:19:01 +01:00
|
|
|
|
2013-02-04 22:24:00 +01:00
|
|
|
/* Only tasks and kernel threads can be initialized in this way */
|
|
|
|
|
|
|
|
#ifndef CONFIG_DISABLE_PTHREAD
|
|
|
|
DEBUGASSERT(tcb &&
|
2017-08-15 01:19:27 +02:00
|
|
|
(tcb->flags & TCB_FLAG_TTYPE_MASK) != TCB_FLAG_TTYPE_PTHREAD);
|
2013-02-04 22:24:00 +01:00
|
|
|
#endif
|
|
|
|
|
2013-01-25 18:23:38 +01:00
|
|
|
/* Create a new task group */
|
|
|
|
|
|
|
|
#ifdef HAVE_TASK_GROUP
|
2014-09-03 22:58:24 +02:00
|
|
|
ret = group_allocate(ttcb, tcb->flags);
|
2013-01-25 18:23:38 +01:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
errcode = -ret;
|
|
|
|
goto errout;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2015-10-04 23:04:00 +02:00
|
|
|
/* Associate file descriptors with the new task */
|
2009-06-17 22:25:27 +02:00
|
|
|
|
|
|
|
#if CONFIG_NFILE_DESCRIPTORS > 0 || CONFIG_NSOCKET_DESCRIPTORS > 0
|
2013-02-04 22:24:00 +01:00
|
|
|
ret = group_setuptaskfiles(ttcb);
|
2013-01-25 18:23:38 +01:00
|
|
|
if (ret < 0)
|
2009-06-17 22:25:27 +02:00
|
|
|
{
|
2013-01-25 18:23:38 +01:00
|
|
|
errcode = -ret;
|
|
|
|
goto errout_with_group;
|
2009-06-17 22:25:27 +02:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2007-03-11 23:19:01 +01:00
|
|
|
/* Configure the user provided stack region */
|
|
|
|
|
|
|
|
up_use_stack(tcb, stack, stack_size);
|
|
|
|
|
|
|
|
/* Initialize the task control block */
|
|
|
|
|
2013-02-04 22:24:00 +01:00
|
|
|
ret = task_schedsetup(ttcb, priority, task_start, entry,
|
2013-01-23 23:23:46 +01:00
|
|
|
TCB_FLAG_TTYPE_TASK);
|
2013-01-25 18:23:38 +01:00
|
|
|
if (ret < OK)
|
2007-03-11 23:19:01 +01:00
|
|
|
{
|
2013-01-25 18:23:38 +01:00
|
|
|
errcode = -ret;
|
2013-01-26 00:21:27 +01:00
|
|
|
goto errout_with_group;
|
2007-03-11 23:19:01 +01:00
|
|
|
}
|
2012-07-14 21:30:31 +02:00
|
|
|
|
2013-01-25 18:23:38 +01:00
|
|
|
/* Setup to pass parameters to the new task */
|
|
|
|
|
2013-02-04 22:24:00 +01:00
|
|
|
(void)task_argsetup(ttcb, name, argv);
|
2013-01-25 20:15:05 +01:00
|
|
|
|
|
|
|
/* Now we have enough in place that we can join the group */
|
|
|
|
|
|
|
|
#ifdef HAVE_TASK_GROUP
|
2013-02-04 23:38:59 +01:00
|
|
|
ret = group_initialize(ttcb);
|
2013-01-25 20:15:05 +01:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
errcode = -ret;
|
2013-01-26 00:21:27 +01:00
|
|
|
goto errout_with_group;
|
2013-01-25 20:15:05 +01:00
|
|
|
}
|
|
|
|
#endif
|
2013-01-25 18:23:38 +01:00
|
|
|
return OK;
|
|
|
|
|
|
|
|
errout_with_group:
|
|
|
|
#ifdef HAVE_TASK_GROUP
|
|
|
|
group_leave(tcb);
|
|
|
|
|
|
|
|
errout:
|
|
|
|
#endif
|
|
|
|
set_errno(errcode);
|
|
|
|
return ERROR;
|
2009-06-17 22:25:27 +02:00
|
|
|
}
|
|
|
|
|