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
|
|
|
*
|
2020-03-26 17:40:10 +01:00
|
|
|
* Licensed to the Apache Software Foundation (ASF) under one or more
|
|
|
|
* contributor license agreements. See the NOTICE file distributed with
|
|
|
|
* this work for additional information regarding copyright ownership. The
|
|
|
|
* ASF licenses this file to you under the Apache License, Version 2.0 (the
|
|
|
|
* "License"); you may not use this file except in compliance with the
|
|
|
|
* License. You may obtain a copy of the License at
|
2007-02-18 00:21:28 +01:00
|
|
|
*
|
2020-03-26 17:40:10 +01:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2007-02-18 00:21:28 +01:00
|
|
|
*
|
2020-03-26 17:40:10 +01:00
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
|
|
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
|
|
* License for the specific language governing permissions and limitations
|
|
|
|
* under the License.
|
2007-02-18 00:21:28 +01:00
|
|
|
*
|
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>
|
2020-03-26 17:40:10 +01:00
|
|
|
#include <nuttx/sched.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
|
|
|
/****************************************************************************
|
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
|
|
|
/****************************************************************************
|
This commit renames all internal OS functions defined under sched/task so that they begin with the prefix. For example, nxtask_exit() vs. task_exit().
Squashed commit of the following:
Trivial, cosmetic
sched/, arch/, and include: Rename task_vforkstart() as nxtask_vforkstart()
sched/, arch/, and include: Rename task_vforkabort() as nxtask_vforkabort()
sched/, arch/, and include: Rename task_vforksetup() as nxtask_vfork_setup()
sched/: Rename notify_cancellation() as nxnotify_cancellation()
sched/: Rename task_recover() to nxtask_recover()
sched/task, sched/pthread/, Documentation/: Rename task_argsetup() and task_terminate() to nxtask_argsetup() and nxtask_terminate(), respectively.
sched/task: Rename task_schedsetup() to nxtask_schedsetup()
sched/ (plus some binfmt/, include/, and arch/): Rename task_start() and task_starthook() to nxtask_start() and nxtask_starthook().
arch/ and sched/: Rename task_exit() and task_exithook() to nxtask_exit() and nxtask_exithook(), respectively.
sched/task: Rename all internal, static, functions to begin with the nx prefix.
2019-02-04 20:42:51 +01:00
|
|
|
* Name: nxthread_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
|
2017-10-16 19:38:00 +02:00
|
|
|
* internal, common implementation of task_create() and kthread_create().
|
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
|
2018-08-09 01:06:46 +02:00
|
|
|
* arg - A pointer to an array of input parameters. The array
|
2012-07-14 21:30:31 +02:00
|
|
|
* 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
|
|
|
*
|
2018-02-01 17:00:02 +01:00
|
|
|
* Returned Value:
|
2018-01-31 23:11:54 +01:00
|
|
|
* Returns the positive, non-zero process ID of the new task or a negated
|
|
|
|
* errno value to indicate the nature of any failure. If memory is
|
|
|
|
* insufficient or the task cannot be created -ENOMEM will be returned.
|
2007-02-18 00:21:28 +01:00
|
|
|
*
|
2008-01-29 01:50:16 +01:00
|
|
|
****************************************************************************/
|
2007-02-18 00:21:28 +01:00
|
|
|
|
This commit renames all internal OS functions defined under sched/task so that they begin with the prefix. For example, nxtask_exit() vs. task_exit().
Squashed commit of the following:
Trivial, cosmetic
sched/, arch/, and include: Rename task_vforkstart() as nxtask_vforkstart()
sched/, arch/, and include: Rename task_vforkabort() as nxtask_vforkabort()
sched/, arch/, and include: Rename task_vforksetup() as nxtask_vfork_setup()
sched/: Rename notify_cancellation() as nxnotify_cancellation()
sched/: Rename task_recover() to nxtask_recover()
sched/task, sched/pthread/, Documentation/: Rename task_argsetup() and task_terminate() to nxtask_argsetup() and nxtask_terminate(), respectively.
sched/task: Rename task_schedsetup() to nxtask_schedsetup()
sched/ (plus some binfmt/, include/, and arch/): Rename task_start() and task_starthook() to nxtask_start() and nxtask_starthook().
arch/ and sched/: Rename task_exit() and task_exithook() to nxtask_exit() and nxtask_exithook(), respectively.
sched/task: Rename all internal, static, functions to begin with the nx prefix.
2019-02-04 20:42:51 +01:00
|
|
|
static int nxthread_create(FAR const char *name, uint8_t ttype,
|
|
|
|
int priority, 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;
|
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)
|
|
|
|
{
|
2016-06-11 23:50:49 +02:00
|
|
|
serr("ERROR: Failed to allocate TCB\n");
|
2018-01-31 23:11:54 +01:00
|
|
|
return -ENOMEM;
|
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
|
|
|
|
2014-09-03 22:58:24 +02:00
|
|
|
ret = group_allocate(tcb, ttype);
|
2013-01-25 18:23:38 +01:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
goto errout_with_tcb;
|
|
|
|
}
|
|
|
|
|
2016-04-19 02:55:36 +02:00
|
|
|
#if 0 /* No... there are side effects */
|
2016-04-14 18:14:38 +02:00
|
|
|
/* Associate file descriptors with the new task. Exclude kernel threads;
|
|
|
|
* kernel threads do not have file or socket descriptors. They must use
|
|
|
|
* SYSLOG for output and the low-level psock interfaces for network I/O.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (ttype != TCB_FLAG_TTYPE_KERNEL)
|
2016-04-19 02:55:36 +02:00
|
|
|
#endif
|
2007-02-18 00:21:28 +01:00
|
|
|
{
|
2016-04-14 18:14:38 +02:00
|
|
|
ret = group_setuptaskfiles(tcb);
|
|
|
|
if (ret < OK)
|
|
|
|
{
|
|
|
|
goto errout_with_tcb;
|
|
|
|
}
|
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
|
|
|
{
|
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
|
|
|
|
2020-05-16 17:06:29 +02:00
|
|
|
ret = nxtask_setup_scheduler(tcb, priority, nxtask_start, entry, ttype);
|
2013-01-25 20:15:05 +01:00
|
|
|
if (ret < OK)
|
2007-03-11 23:19:01 +01:00
|
|
|
{
|
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
|
|
|
|
2020-05-16 17:06:29 +02:00
|
|
|
nxtask_setup_arguments(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 */
|
|
|
|
|
2013-02-04 23:38:59 +01:00
|
|
|
ret = group_initialize(tcb);
|
2013-01-25 20:15:05 +01:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
2020-05-27 17:15:45 +02:00
|
|
|
goto errout_with_active;
|
2013-01-25 20:15:05 +01:00
|
|
|
}
|
|
|
|
|
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 */
|
|
|
|
|
2020-06-04 14:19:40 +02:00
|
|
|
nxtask_activate((FAR struct tcb_s *)tcb);
|
2007-03-11 23:19:01 +01:00
|
|
|
return pid;
|
2011-04-07 16:39:55 +02:00
|
|
|
|
2020-05-27 17:15:45 +02:00
|
|
|
errout_with_active:
|
|
|
|
/* The TCB was added to the inactive task list by
|
|
|
|
* nxtask_setup_scheduler().
|
|
|
|
*/
|
|
|
|
|
|
|
|
dq_rem((FAR dq_entry_t *)tcb, (FAR dq_queue_t *)&g_inactivetasks);
|
|
|
|
|
2011-04-07 16:39:55 +02:00
|
|
|
errout_with_tcb:
|
2020-05-09 16:04:45 +02:00
|
|
|
nxsched_release_tcb((FAR struct tcb_s *)tcb, ttype);
|
2018-01-31 23:11:54 +01:00
|
|
|
return ret;
|
2011-04-07 16:39:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
2018-01-31 23:11:54 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: nxtask_create
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* This function creates and activates a new task with a specified
|
|
|
|
* priority and returns its system-assigned ID.
|
|
|
|
*
|
|
|
|
* 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
|
|
|
|
* automatically be made.
|
|
|
|
*
|
|
|
|
* Note that four (and only four) arguments must be passed for the spawned
|
|
|
|
* functions.
|
|
|
|
*
|
|
|
|
* nxtask_create() is identical to the function task_create(), differing
|
|
|
|
* only in its return value: This function does not modify the errno
|
|
|
|
* variable. This is a non-standard, internal OS function and is not
|
|
|
|
* intended for use by application logic. Applications should use
|
|
|
|
* task_create().
|
|
|
|
*
|
|
|
|
* 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
|
2018-08-09 01:06:46 +02:00
|
|
|
* arg - A pointer to an array of input parameters. The array
|
2018-01-31 23:11:54 +01:00
|
|
|
* should be terminated with a NULL argv[] value. If no
|
|
|
|
* parameters are required, argv may be NULL.
|
|
|
|
*
|
2018-02-01 17:00:02 +01:00
|
|
|
* Returned Value:
|
2018-01-31 23:11:54 +01:00
|
|
|
* Returns the positive, non-zero process ID of the new task or a negated
|
|
|
|
* errno value to indicate the nature of any failure. If memory is
|
|
|
|
* insufficient or the task cannot be created -ENOMEM will be returned.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
int nxtask_create(FAR const char *name, int priority,
|
|
|
|
int stack_size, main_t entry, FAR char * const argv[])
|
|
|
|
{
|
This commit renames all internal OS functions defined under sched/task so that they begin with the prefix. For example, nxtask_exit() vs. task_exit().
Squashed commit of the following:
Trivial, cosmetic
sched/, arch/, and include: Rename task_vforkstart() as nxtask_vforkstart()
sched/, arch/, and include: Rename task_vforkabort() as nxtask_vforkabort()
sched/, arch/, and include: Rename task_vforksetup() as nxtask_vfork_setup()
sched/: Rename notify_cancellation() as nxnotify_cancellation()
sched/: Rename task_recover() to nxtask_recover()
sched/task, sched/pthread/, Documentation/: Rename task_argsetup() and task_terminate() to nxtask_argsetup() and nxtask_terminate(), respectively.
sched/task: Rename task_schedsetup() to nxtask_schedsetup()
sched/ (plus some binfmt/, include/, and arch/): Rename task_start() and task_starthook() to nxtask_start() and nxtask_starthook().
arch/ and sched/: Rename task_exit() and task_exithook() to nxtask_exit() and nxtask_exithook(), respectively.
sched/task: Rename all internal, static, functions to begin with the nx prefix.
2019-02-04 20:42:51 +01:00
|
|
|
return nxthread_create(name, TCB_FLAG_TTYPE_TASK, priority, stack_size,
|
|
|
|
entry, argv);
|
2018-01-31 23:11:54 +01:00
|
|
|
}
|
|
|
|
|
2011-04-07 16:39:55 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* 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
|
2018-08-09 01:06:46 +02:00
|
|
|
* arg - A pointer to an array of input parameters. The array
|
2012-07-14 21:30:31 +02:00
|
|
|
* 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
|
|
|
*
|
2018-02-01 17:00:02 +01:00
|
|
|
* Returned Value:
|
2011-04-07 16:39:55 +02:00
|
|
|
* Returns the non-zero process ID of the new task or ERROR if memory is
|
2018-01-31 23:11:54 +01:00
|
|
|
* insufficient or the task cannot be created. The errno will be set in
|
|
|
|
* the failure case to indicate the nature of the error.
|
2011-04-07 16:39:55 +02:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
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
|
|
|
{
|
2018-01-31 23:11:54 +01:00
|
|
|
int ret = nxtask_create(name, priority, stack_size, entry, argv);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
set_errno(-ret);
|
|
|
|
ret = ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
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
|
|
|
|
|
|
|
/****************************************************************************
|
2017-10-16 19:38:00 +02:00
|
|
|
* Name: kthread_create
|
2011-04-07 16:39:55 +02:00
|
|
|
*
|
|
|
|
* 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:
|
2018-01-31 23:11:54 +01:00
|
|
|
* 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
|
2018-08-09 01:06:46 +02:00
|
|
|
* arg - A pointer to an array of input parameters. The array
|
2018-01-31 23:11:54 +01:00
|
|
|
* 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
|
|
|
*
|
2018-02-01 17:00:02 +01:00
|
|
|
* Returned Value:
|
2018-01-31 23:11:54 +01:00
|
|
|
* Returns the positive, non-zero process ID of the new task or a negated
|
|
|
|
* errno value to indicate the nature of any failure. If memory is
|
|
|
|
* insufficient or the task cannot be created -ENOMEM will be returned.
|
2011-04-07 16:39:55 +02:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2017-10-16 19:38:00 +02:00
|
|
|
int kthread_create(FAR const char *name, int priority,
|
2020-05-06 11:24:42 +02:00
|
|
|
int stack_size, main_t entry, FAR char * const argv[])
|
2011-04-07 16:39:55 +02:00
|
|
|
{
|
This commit renames all internal OS functions defined under sched/task so that they begin with the prefix. For example, nxtask_exit() vs. task_exit().
Squashed commit of the following:
Trivial, cosmetic
sched/, arch/, and include: Rename task_vforkstart() as nxtask_vforkstart()
sched/, arch/, and include: Rename task_vforkabort() as nxtask_vforkabort()
sched/, arch/, and include: Rename task_vforksetup() as nxtask_vfork_setup()
sched/: Rename notify_cancellation() as nxnotify_cancellation()
sched/: Rename task_recover() to nxtask_recover()
sched/task, sched/pthread/, Documentation/: Rename task_argsetup() and task_terminate() to nxtask_argsetup() and nxtask_terminate(), respectively.
sched/task: Rename task_schedsetup() to nxtask_schedsetup()
sched/ (plus some binfmt/, include/, and arch/): Rename task_start() and task_starthook() to nxtask_start() and nxtask_starthook().
arch/ and sched/: Rename task_exit() and task_exithook() to nxtask_exit() and nxtask_exithook(), respectively.
sched/task: Rename all internal, static, functions to begin with the nx prefix.
2019-02-04 20:42:51 +01:00
|
|
|
return nxthread_create(name, TCB_FLAG_TTYPE_KERNEL, priority, stack_size,
|
|
|
|
entry, argv);
|
2011-04-07 16:39:55 +02:00
|
|
|
}
|