2008-01-29 01:50:16 +01:00
|
|
|
/****************************************************************************
|
2014-08-09 00:44:08 +02:00
|
|
|
* sched/task/task_create.c
|
2007-02-18 00:21:28 +01:00
|
|
|
*
|
2014-09-03 22:58:24 +02:00
|
|
|
* Copyright (C) 2007-2010, 2013-2014 Gregory Nutt. All rights reserved.
|
2012-07-14 21:30:31 +02:00
|
|
|
* Author: Gregory Nutt <gnutt@nuttx.org>
|
2007-02-18 00:21:28 +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.
|
2008-01-29 01:50:16 +01:00
|
|
|
* 3. Neither the name NuttX nor the names of its contributors may be
|
2007-02-18 00:21:28 +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.
|
|
|
|
*
|
2008-01-29 01:50:16 +01:00
|
|
|
****************************************************************************/
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2008-01-29 01:50:16 +01:00
|
|
|
/****************************************************************************
|
2007-02-18 00:21:28 +01:00
|
|
|
* Included Files
|
2008-01-29 01:50:16 +01:00
|
|
|
****************************************************************************/
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2007-03-11 23:19:01 +01:00
|
|
|
#include <nuttx/config.h>
|
2011-04-03 22:41:49 +02:00
|
|
|
|
2007-02-18 00:21:28 +01:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sched.h>
|
|
|
|
#include <errno.h>
|
2007-02-19 23:51:18 +01:00
|
|
|
#include <debug.h>
|
2011-04-03 22:41:49 +02:00
|
|
|
|
2007-02-18 00:21:28 +01:00
|
|
|
#include <nuttx/arch.h>
|
2011-04-03 22:41:49 +02:00
|
|
|
#include <nuttx/kmalloc.h>
|
2013-12-31 00:55:19 +01:00
|
|
|
#include <nuttx/kthread.h>
|
2011-04-03 22:41:49 +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-02-18 00:21:28 +01:00
|
|
|
|
2008-01-29 01:50:16 +01:00
|
|
|
/****************************************************************************
|
2015-04-08 14:47:36 +02:00
|
|
|
* Pre-processor Definitions
|
2008-01-29 01:50:16 +01:00
|
|
|
****************************************************************************/
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2008-01-29 01:50:16 +01:00
|
|
|
/****************************************************************************
|
2007-02-18 00:21:28 +01:00
|
|
|
* Private Type Declarations
|
2008-01-29 01:50:16 +01:00
|
|
|
****************************************************************************/
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2008-01-29 01:50:16 +01:00
|
|
|
/****************************************************************************
|
2007-02-18 00:21:28 +01:00
|
|
|
* Global Variables
|
2008-01-29 01:50:16 +01:00
|
|
|
****************************************************************************/
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2008-01-29 01:50:16 +01:00
|
|
|
/****************************************************************************
|
2007-02-18 00:21:28 +01:00
|
|
|
* Private Variables
|
2008-01-29 01:50:16 +01:00
|
|
|
****************************************************************************/
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2008-01-29 01:50:16 +01:00
|
|
|
/****************************************************************************
|
2007-02-18 00:21:28 +01:00
|
|
|
* Private Functions
|
2008-01-29 01:50:16 +01:00
|
|
|
****************************************************************************/
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2008-01-29 01:50:16 +01:00
|
|
|
/****************************************************************************
|
2011-04-07 16:39:55 +02:00
|
|
|
* Name: thread_create
|
2007-02-18 00:21:28 +01:00
|
|
|
*
|
|
|
|
* Description:
|
2011-04-07 16:39:55 +02:00
|
|
|
* This function creates and activates a new thread of the specified type
|
|
|
|
* with a specified priority and returns its system-assigned ID. It is the
|
2014-09-03 22:58:24 +02:00
|
|
|
* internal, common implementation of task_create() and kernel_thread().
|
2012-07-14 21:30:31 +02:00
|
|
|
* See comments with task_create() for further information.
|
2007-02-18 00:21:28 +01:00
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* name - Name of the new task
|
2013-01-23 23:23:46 +01:00
|
|
|
* ttype - Type of the new task
|
2007-02-18 00:21:28 +01:00
|
|
|
* priority - Priority of the new task
|
|
|
|
* stack_size - size (in bytes) of the stack needed
|
|
|
|
* entry - Entry point of a new task
|
2012-07-14 21:30:31 +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-02-18 00:21:28 +01:00
|
|
|
*
|
|
|
|
* Return Value:
|
2011-04-07 16:39:55 +02:00
|
|
|
* Returns the non-zero process ID of the new task or ERROR if memory is
|
|
|
|
* insufficient or the task cannot be created. The errno will be set to
|
|
|
|
* indicate the nature of the error (always ENOMEM).
|
2007-02-18 00:21:28 +01:00
|
|
|
*
|
2008-01-29 01:50:16 +01:00
|
|
|
****************************************************************************/
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2013-03-20 19:22:21 +01:00
|
|
|
static int thread_create(FAR const char *name, uint8_t ttype, int priority,
|
2013-02-02 20:31:30 +01:00
|
|
|
int stack_size, main_t entry, FAR char * const argv[])
|
2007-02-18 00:21:28 +01:00
|
|
|
{
|
2013-02-04 22:24:00 +01:00
|
|
|
FAR struct task_tcb_s *tcb;
|
2007-02-18 00:21:28 +01:00
|
|
|
pid_t pid;
|
2013-01-25 18:23:38 +01:00
|
|
|
int errcode;
|
2011-04-07 16:39:55 +02:00
|
|
|
int ret;
|
2007-02-18 00:21:28 +01:00
|
|
|
|
|
|
|
/* Allocate a TCB for the new task. */
|
|
|
|
|
2014-09-01 01:34:44 +02:00
|
|
|
tcb = (FAR struct task_tcb_s *)kmm_zalloc(sizeof(struct task_tcb_s));
|
2007-02-18 00:21:28 +01:00
|
|
|
if (!tcb)
|
|
|
|
{
|
2013-02-28 16:31:58 +01:00
|
|
|
sdbg("ERROR: Failed to allocate TCB\n");
|
2013-01-25 18:23:38 +01:00
|
|
|
errcode = ENOMEM;
|
2011-04-07 16:39:55 +02:00
|
|
|
goto errout;
|
2007-02-18 00:21:28 +01:00
|
|
|
}
|
|
|
|
|
2014-09-03 22:58:24 +02:00
|
|
|
/* Allocate a new task group with privileges appropriate for the parent
|
|
|
|
* thread type.
|
|
|
|
*/
|
2013-01-25 18:23:38 +01:00
|
|
|
|
|
|
|
#ifdef HAVE_TASK_GROUP
|
2014-09-03 22:58:24 +02:00
|
|
|
ret = group_allocate(tcb, ttype);
|
2013-01-25 18:23:38 +01:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
errcode = -ret;
|
|
|
|
goto errout_with_tcb;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2007-02-18 00:21:28 +01:00
|
|
|
/* Associate file descriptors with the new task */
|
|
|
|
|
2007-11-20 00:09:39 +01:00
|
|
|
#if CONFIG_NFILE_DESCRIPTORS > 0 || CONFIG_NSOCKET_DESCRIPTORS > 0
|
2013-01-27 00:49:02 +01:00
|
|
|
ret = group_setuptaskfiles(tcb);
|
2013-02-08 22:42:23 +01:00
|
|
|
if (ret < OK)
|
2007-02-18 00:21:28 +01:00
|
|
|
{
|
2013-01-25 18:23:38 +01:00
|
|
|
errcode = -ret;
|
2011-04-07 16:39:55 +02:00
|
|
|
goto errout_with_tcb;
|
2007-02-18 00:21:28 +01:00
|
|
|
}
|
2007-03-11 22:16:56 +01:00
|
|
|
#endif
|
2007-02-18 00:21:28 +01:00
|
|
|
|
|
|
|
/* Allocate the stack for the TCB */
|
|
|
|
|
2013-03-20 19:22:21 +01:00
|
|
|
ret = up_create_stack((FAR struct tcb_s *)tcb, stack_size, ttype);
|
2013-02-08 22:42:23 +01:00
|
|
|
if (ret < OK)
|
2007-02-18 00:21:28 +01:00
|
|
|
{
|
2013-01-25 18:23:38 +01:00
|
|
|
errcode = -ret;
|
2011-04-07 16:39:55 +02:00
|
|
|
goto errout_with_tcb;
|
2007-02-18 00:21:28 +01:00
|
|
|
}
|
|
|
|
|
2007-03-11 23:19:01 +01:00
|
|
|
/* Initialize the task control block */
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2013-01-23 23:23:46 +01:00
|
|
|
ret = task_schedsetup(tcb, priority, task_start, entry, ttype);
|
2013-01-25 20:15:05 +01:00
|
|
|
if (ret < OK)
|
2007-03-11 23:19:01 +01:00
|
|
|
{
|
2013-01-25 18:23:38 +01:00
|
|
|
errcode = -ret;
|
2011-04-07 16:39:55 +02:00
|
|
|
goto errout_with_tcb;
|
2007-03-11 23:19:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Setup to pass parameters to the new task */
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2007-03-14 00:03:12 +01:00
|
|
|
(void)task_argsetup(tcb, name, argv);
|
2007-02-18 00:21:28 +01:00
|
|
|
|
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(tcb);
|
2013-01-25 20:15:05 +01:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
errcode = -ret;
|
|
|
|
goto errout_with_tcb;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2007-03-11 23:19:01 +01:00
|
|
|
/* Get the assigned pid before we start the task */
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2013-02-04 22:24:00 +01:00
|
|
|
pid = (int)tcb->cmn.pid;
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2007-03-11 23:19:01 +01:00
|
|
|
/* Activate the task */
|
|
|
|
|
2013-02-04 22:24:00 +01:00
|
|
|
ret = task_activate((FAR struct tcb_s *)tcb);
|
2013-02-08 22:42:23 +01:00
|
|
|
if (ret < OK)
|
2007-02-18 00:21:28 +01:00
|
|
|
{
|
2013-01-25 18:23:38 +01:00
|
|
|
errcode = get_errno();
|
|
|
|
|
2013-01-07 20:35:47 +01:00
|
|
|
/* The TCB was added to the active task list by task_schedsetup() */
|
|
|
|
|
2007-03-20 17:51:12 +01:00
|
|
|
dq_rem((FAR dq_entry_t*)tcb, (dq_queue_t*)&g_inactivetasks);
|
2011-04-07 16:39:55 +02:00
|
|
|
goto errout_with_tcb;
|
2007-02-18 00:21:28 +01:00
|
|
|
}
|
|
|
|
|
2007-03-11 23:19:01 +01:00
|
|
|
return pid;
|
2011-04-07 16:39:55 +02:00
|
|
|
|
|
|
|
errout_with_tcb:
|
2013-03-20 19:22:21 +01:00
|
|
|
sched_releasetcb((FAR struct tcb_s *)tcb, ttype);
|
2011-04-07 16:39:55 +02:00
|
|
|
|
|
|
|
errout:
|
2013-01-25 18:23:38 +01:00
|
|
|
set_errno(errcode);
|
2011-04-07 16:39:55 +02:00
|
|
|
return ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: task_create
|
|
|
|
*
|
|
|
|
* Description:
|
2012-07-14 21:30:31 +02:00
|
|
|
* This function creates and activates a new task with a specified
|
|
|
|
* priority and returns its system-assigned ID.
|
2011-04-07 16:39:55 +02:00
|
|
|
*
|
2012-07-14 21:30:31 +02:00
|
|
|
* The entry address entry is the address of the "main" function of the
|
|
|
|
* task. This function will be called once the C environment has been
|
|
|
|
* set up. The specified function will be called with four arguments.
|
|
|
|
* Should the specified routine return, a call to exit() will
|
2011-04-07 16:39:55 +02:00
|
|
|
* automatically be made.
|
|
|
|
*
|
2012-07-14 21:30:31 +02:00
|
|
|
* Note that four (and only four) arguments must be passed for the spawned
|
|
|
|
* functions.
|
2011-04-07 16:39:55 +02:00
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* name - Name of the new task
|
|
|
|
* priority - Priority of the new task
|
|
|
|
* stack_size - size (in bytes) of the stack needed
|
|
|
|
* entry - Entry point of a new task
|
2012-07-14 21:30:31 +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.
|
2011-04-07 16:39:55 +02:00
|
|
|
*
|
|
|
|
* Return Value:
|
|
|
|
* Returns the non-zero process ID of the new task or ERROR if memory is
|
|
|
|
* insufficient or the task cannot be created. The errno will be set to
|
|
|
|
* indicate the nature of the error (always ENOMEM).
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2014-09-16 23:40:48 +02:00
|
|
|
#ifndef CONFIG_BUILD_KERNEL
|
2013-03-20 19:22:21 +01:00
|
|
|
int task_create(FAR const char *name, int priority,
|
2013-02-02 20:31:30 +01:00
|
|
|
int stack_size, main_t entry, FAR char * const argv[])
|
2011-04-07 16:39:55 +02:00
|
|
|
{
|
|
|
|
return thread_create(name, TCB_FLAG_TTYPE_TASK, priority, stack_size, entry, argv);
|
2007-02-18 00:21:28 +01:00
|
|
|
}
|
2014-09-14 16:22:21 +02:00
|
|
|
#endif
|
2011-04-07 16:39:55 +02:00
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: kernel_thread
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* This function creates and activates a kernel thread task with kernel-
|
|
|
|
* mode privileges. It is identical to task_create() except that it
|
|
|
|
* configures the newly started thread to run in kernel model.
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* (same as task_create())
|
|
|
|
*
|
|
|
|
* Return Value:
|
|
|
|
* (same as task_create())
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2013-03-20 19:22:21 +01:00
|
|
|
int kernel_thread(FAR const char *name, int priority,
|
2013-02-02 20:31:30 +01:00
|
|
|
int stack_size, main_t entry, FAR char * const argv[])
|
2011-04-07 16:39:55 +02:00
|
|
|
{
|
|
|
|
return thread_create(name, TCB_FLAG_TTYPE_KERNEL, priority, stack_size, entry, argv);
|
|
|
|
}
|